Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Gtween glitch? - Gideros Forum

Gtween glitch?

Hello

I used a tween to move a movieclip. The tween is generated on touch.

Everything works great as long as I am gentle, and don't click/touch too fast. When I go fast (like I expect a player to be), I get some glitches: most often it is a teleport instead of a smooth animation.

Here is my code:
local function followTween(ship, event)
 
	local xship, yship = ship:getPosition()
 
	print ("Ship: ", xship," x ", yship)
	--print(ship:getPosition())
	print ("touch!")
	local x = event.touch.x
	local y = event.touch.y
	print (x," x ", y)
 
	-- constant speed
	local dist = math.sqrt((xship-x)^2+(yship-y)^2) 
	local speed = 400
	local duration = dist/speed	
	-- gtween props
	local animate ={}
	animate.x = x
	animate.y = y
	local properties = {}
	properties.delay = 0
	properties.ease = easing.linear
	-- gtween call
	local tween = GTween.new(ship, duration, animate, properties)
 
end
This function is called via
mapb:addEventListener(Event.TOUCHES_END, followTween, rmship)
I am a beginner so I guess my code might be lacking some useful subtleties. What should I do?

Comments

  • hgy29hgy29 Maintainer
    I think you need to cancel the previous tween if it wasn't complete when the player touches the screen again, otherwise both tweens will be running concurrently for a while
  • rrraptorrrraptor Member
    edited June 15
    Instead of creating new GTween instance every time, you can reuse the old one like that:
    local function NULL() end
    -- t (table):
    --	duration, values, ease, callback
    function Sprite:animate(t)
    	t.duration = t.duration or 0
    	t.ease = t.ease or easing.linear
    	t.callback = t.callback or NULL
     
    	if (self.gt) then 
    		self.gt.duration = t.duration
    		self.gt.ease = t.ease
    		self.gt:setValues(t.values)
    	else
    		self.gt = GTween.new(self, t.duration, t.values, {ease = t.ease})
    		self.gt:addEventListener("complete", t.callback, self)
    	end
    	self.gt:setPosition(0)
    	self.gt:setPaused(false)
    end
  • DafbDafb Member
    Thank you so much!

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.