Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Won't Set Position of My Object after Release — Gideros Forum

Won't Set Position of My Object after Release

QuasarCreatorQuasarCreator Member
edited July 2012 in General questions
So, I working on the this problem for the good part of the day and can't seem to get it to work so I am asking for your help. ^:)^ In my game the user will be able to drag an object across the screen and release it in the desired position. I want to make it so if the user puts the object at coordinates (20, 20) then the object will be translated to (40, 40). I hope that makes sense.

Here is my code that I was able to put together.
 
	--getting the x and y coordinates when the object is pressed
	local function onMouseDown(self, event)
		if self:hitTestPoint(event.x, event.y) then
			self.isFocus = true
 
			self.x0 = event.x
			self.y0 = event.y
 
			event:stopPropagation()
		end
	end
 
 
	--calculating the x and y coordinates when the object is moved
	local function onMouseMove(self, event)
		if self.isFocus then
			local dx = event.x - self.x0
			local dy = event.y - self.y0
 
			self:setX(self:getX() + dx)
			self:setY(self:getY() + dy)
 
			self.x0 = event.x
			self.y0 = event.y
 
			event:stopPropagation()
		end
	end
 
 
	--x and y coordinates when the object is released
	local function onMouseUp(self, event)
		print(event.x, event.y)
		if self.isFocus then
			self.isFocus = false
			event:stopPropagation()
 
		end
 
		local a
		local b
 
		if (event.x < 40 and event.x > 40) and (event.y < 40 and event.y > 40) then
			a = 40 - event.x
			b = 40 - event.y
			self.x = event.x + a
			self.y = event.y + b
			self:setPosition(self.x, self.y)
 
		end 
 
	end
Here is what I am trying to do with the code:

I am using the gideros dragging example to move the object and I tried to add and if statement within the onMouseUp function. I am trying to find out if the objects new x and y coordinates is less than 40 but greater than 0, then translate the object to (40, 40).


I am able to drag the object but when I release the object the object won't translate.

How can I fix this or how can I make this more efficient?

Thank you in advance!!
I am a programming noobie, so thanks in advance for helping me out and answering my questions!

Comments

  • You are writing code the way you would with BeerSDK.

    Anyways, to resolve your problem, I faced this while I was trying to write the compatibility layer for BeerSDK code to be used with Gideros. The solution.... get rid of the event:stopPropagation()

    If that does not resolve your issue, will have to take a detailed look into the same.

    Likes: QuasarCreator

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • AlexRuAlexRu Member
    edited July 2012
    First:
    (event.x < 40 and event.x > 40) and (event.y < 40 and event.y > 40) is always False, meybe
    (event.x < 40 and event.x > 0) and (event.y < 40 and event.y > 0) ??

    Second:
    a = 40 - event.x
    b = 40 - event.y
    self.x = event.x + a = event.x + 40 - event.x = 40
    self.y = event.y + b = event.y + 40 - event.y = 40
    ;)

    And I think it's better to use event:stopPropagation() after your logic in onMouseUp():
     
    	--x and y coordinates when the object is released
    	local function onMouseUp(self, event)
     
                    local a
    		local b
     
    		if (event.x < 40 and event.x > 0) and (event.y < 40 and event.y > 0) then
    			a = 40 - event.x
    			b = 40 - event.y
    			self.x = event.x + a
    			self.y = event.y + b
    			self:setPosition(self.x, self.y)
     
    		end 
     
                    print(event.x, event.y)
     
                    if self.isFocus then
    			self.isFocus = false
    			event:stopPropagation()
     
    		end
     
     
     
    	end

    Likes: QuasarCreator

    +1 -1 (+1 / -0 )Share on Facebook
  • AlexRuAlexRu Member
    As i understood you want to put yor sprites in this grid:
    http://www.giderosmobile.com/forum/discussion/1255/making-a-square-grid

    If I'm rigth then I think it's better to use two-dimensional table of sprites for bitmaps and use Sprite:hitTestPoint(event.x, event.y) in For statement for every element of grid.

    Something like this:
    --make your grid 2d table
    grid = {}
     
    for i = 1, 20 do
    	table.insert(grid,{})
    	for j = 1, 20 do
    		table.insert(grid[i],j)
    		grid[i][j] = Sprite.new()
    		grid[i][j]:addChild(Bitmap.new(Texture.new("square.png")))
    		grid[i][j]:setPosition((i-1)*40, (j-1)*40)
    		stage:addChild(grid[i][j])
    		--or self:addChild(grid[i][j]) if you use grid in your sprite (not in main.lua)
    	end
    end
     
    ---
     
    ---some code
     
    ---
     
     
    --x and y coordinates when the object is released
    local function onMouseUp(self, event)
    	print(event.x, event.y)
    	local break_for_flag = false
    	for i = 1, #grid do
    		for j = 1, #grid do
    			if grid[i][j]:hitTestPoint(event.x, event.y) then
    				self:setPosition(grid[i][j]:getPosition())
    				break_for_flag = true
    				break
    			end
    		end
     
    		if break_for_flag then
    			break
    		end
    	end
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
     	end
     end
    If you release sprite, it's coordinates will equal to the coordinates of the grid cell in which the mouse up.

    But if you use grid in your own sprite (not in main.lua) don't forget to release memory then destroy this sprite. Something like this:
    function onRemoveFromStage(event)
    	--release memory of grid table
    	for i = 1, #grid do
    		for j = 1, #grid do
    			grid[i][j] = nil
    		end
    	end
    	grid = {}
    	grid = nil
    end

    Likes: QuasarCreator

    +1 -1 (+1 / -0 )Share on Facebook
  • QuasarCreatorQuasarCreator Member
    edited July 2012
    @AlexRu - Thank you so much for spending the time to answer my question and providing all of the code with a little explanation !! ^:)^

    However, there is one little thing that won't work now. When I release my sprite or shape everything works fine, however, if I spawn another sprite and release it the other shape will move towards the other sprite. I am making a puzzle app so there will be multiple sprites on the screen that the user can control. Other than that everything works as I want. Again, I have to say thanks because this is a big part of my app and I was having a hard time getting this logic down. I guess I need to learn more about my lua tables.

    I was going to make an if statement for each square. ~X( :-))
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
  • @QasarCreator, have you had a look at some of the articles on my blog http://howto.oz-apps.com, there are some earlier ones (though written for the BeerSDK, they are still relevant to Gideros) explain a few Lua concepts.

    Likes: QuasarCreator

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • QuasarCreatorQuasarCreator Member
    edited July 2012
    @OZApps - Thanks for the link I will definitely look at it and go through the articles! :D

    Edit: Wow, I was looking at your blog and you got a lot of articles that would be very useful. Again thanks for sharing.

    I was looking at your article of the SpriteEditor demo where you create a nice grid that you can interact with. Could I put my sprites or shapes in the grid made by your Sprite Editor?
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
  • AlexRuAlexRu Member
    @QuasarCreator, you are wellcome.
    I thought you want to use a lot of if statements. But gideros api can do it better ;) I like lua tables and I use it for ai in my game. And don't forget about memory then you use tables. I got a memory leak and try to catch it about week :)
  • I agree tables are lua's most powerful feature, they are so flexible and can be used for 1000's of different things. Mastering lua really comes down to mastering lua tables.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • @techdojo, Tables are rather amazing and really pretty easy to use as well. I am still getting used to some of the specific ways they are being used in programming with Gideros, but I really like it :)
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • I will definitely study my tables some more. Right now I think of doing things using if statements and it seems tables are really the best way to go.
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
  • I wondered why there was no switch statement in Lua and then realised you can do the same thing easily with tables, I use them for objects, general data structures, indexs, data tables, passing complex parameters to functions, returning data from functions - the list is endless.

    Likes: ar2rsawseen

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    @techdojo, wow good thought about switch statement.
Sign In or Register to comment.