Hello all,
I was wondering if someone could help me with some drag and touch code I'm trying to figure out.
I've just started to learn Gideros and I've written some code that basically drags a ball.png using touch events.
Everything is working, I'm able to drag the ball.png, but I find that there's a slight delay from when I drag the ball.png to where I move my finger when I run the code on the Android Gideros player.
I think I need to update the ball.png xy location in the Enterframe event, but I'm not sure how to do this;
Could someone please take a look at my code and let me know how I update the code so the ball.png is update to match the exact location where I touch drag the screen on my android device?
Here's my code:
--[[
Drag the shapes around with your mouse or fingers
This code is MIT licensed, see
http://www.opensource.org/licenses/mit-license.php(C) 2010 - 2011 Gideros Mobile
]]
local ball = Bitmap.new(Texture.new("ball.png"))
local function onTouchesBegin(self, event)
if self:hitTestPoint(event.touch.x, event.touch.y) then
self.isFocus = true
self.x0 = event.touch.x
self.y0 = event.touch.y
event:stopPropagation()
end
end
local function onTouchesMove(self, event)
if self.isFocus then
local dx = event.touch.x - self.x0
local dy = event.touch.y - self.y0
self:setX(self:getX() + dx)
self:setY(self:getY() + dy)
self.x0 = event.touch.x
self.y0 = event.touch.y
event:stopPropagation()
end
end
local function onTouchesEnd(self, event)
if self.isFocus then
self.isFocus = false
event:stopPropagation()
end
end
local function onTouchesCancel(event)
end
-- before entering each frame, we update the position of the ball
function onEnterFrame(event)
end
-- create a ball bitmap object (Bitmap class inherits from Sprite class)
ball:addEventListener(Event.TOUCHES_BEGIN, onTouchesBegin, ball)
ball:addEventListener(Event.TOUCHES_MOVE, onTouchesMove, ball)
ball:addEventListener(Event.TOUCHES_END, onTouchesEnd, ball)
stage:addChild(ball)
local info = TextField.new(nil, "drag the shapes around with your mouse or fingers")
info:setPosition(23, 50)
stage:addChild(info)
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
Thanks,
Stephen T.
Comments
if not go with that there are some improvements in touch
if yes then it is normal because touch event are firing almost 100 times a second but screen refresh rate is usually locked at 60 or even 30 so you will always see your sprite is little bit behind your finger while moving.
Well if the touch updates a 100 times then maybe I can set the onenterframe to match?
Stephen
Thats the main problem over here onenterframe event can fire at max 60 times a second so whatever you do you will be slightly behind the touch position depends upon how fast your finger is moving
@stephentt I'd actually used same method as you and did not had any noticeable problems.
So again do you use latest Gideros version?
Is there anything else inside on touch event?
Is there anything else in your app?
What phone are you testing it on?
Does it happens only on Gideros player?
Likes: hgvyas123