It looks like you're new here. If you want to get involved, click one of these buttons!
------------ -- main.lua ------------ -- Create multiple blocks. for i = 0, 5 do -- The block image. local blockSprite = Bitmap.new(Texture.new("3D_Block3.png")) -- Create new block. local block = Block.new(blockSprite) -- Place blocks in the bottom screen side by side. block:setPosition((i * 150) + 32, 540) stage:addChild(block) end ------------ --block.lua ------------ Block = Core.class(Sprite) function Block:init(sprite) -- Register touch events. self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self) self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self) self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self) self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self) self:addChild(sprite) end -- If block is on focus, stop propagation of touch events function Block:onTouchesBegin(event) if self.id == nil then self.id = event.touch.id print("BEGIN") -- Get start postion. self.x0 = event.touch.x self.y0 = event.touch.y end end -- If block is on focus, stop propagation of touch events. function Block:onTouchesMove(event) if self.id == event.touch.id then print("MOVE") -- Calculate delta value. local deltaX = event.touch.x - self.x0 local deltaY = event.touch.y - self.y0 -- Update new postion. self:setX(self:getX() + deltaX) self:setY(self:getY() + deltaX) -- Store the new postion. self.x0 = event.touch.x self.y0 = event.touch.y end end -- If block is on focus, stop propagation of touch events. function Block:onTouchesEnd(event) if self.id - event.touch.id then print("END") self.id = nil end end -- If touches are cancelled, reset the state of the button. function Block:onTouchesCancel(event) if self.focus then self.focus = false; self:updateVisualState(false) event:stopPropagation() end end |
Comments
And there is a small bug on onTouchesMove. The line
And on onTouchesEnd the line
And finally you should implement onTouchesCancel same as onTouchesEnd
Marc
1-In your code each block is an object. So actually each of these blocks have some pointer like identifier in Gideros. (memory id like every person has only one tax code in real life, or Social security number in America vs..)
2-And each of these block also has event listener registered.
3-When you touch your screen Gideros studio should have to match your touch with the block's id. (which one you touched) and after it it should start to move that specific block.
4-If you will not do this matching this move will be common for or your blocks hence you have event listener registered for all your blocks.
Of course i am no pro but it seems to me my explanation is legit
But from more techy point:
You may have multiple objects with assigned event listeners
When you touch the screen a touch event is dispatched to all of them (starting from the top ones and to the bottom)
Inside event listener you put hitTestPoint, to test was the area of current object touched. It basically checks if touch point is inside the object.
If it is then user touched this point and you know it, you may now do what you wanted to do.
If you do not use hitTestPoint then all code from all touch/mouse listeners will be executed for all objects you've assigned them to. There will be no telling, which object did the user touched.
Next thing to understand, that there also may be a situation, when there a couple object one on the other. So if hitTestPoint only checks x and y coordinates, then it can not determine which of the stacked objects was actually touched and execute code for all of them. This is true.
In this situation, event goes from the top visible objects to the other object under them. So if you check for a hitTestPoint and received the positive answer, it is a good practice to stop propagating event, so others under it won't receive same event (unless of course you want them to).
So basic model of using touch/mouse events could look like this:
But i also was close