Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
A very basic feature requset, run an action on a sprite — Gideros Forum

A very basic feature requset, run an action on a sprite

alexzhengalexzheng Guru
edited August 2012 in General questions
Since @atilim told me MovieClip is not suitable for running actions on a sprite,
I began turning to GTween again, however, it was full of bugs, and the performance was not acceptable.
The result of the code will not be the same as expected,the larger number of loops, the worse the result was.
local sprite
local gtween
local counter = 0
for i = 1, 100 do
sprite = Bitmap.new(Texture.new("box.png"))
stage:addChild(sprite)
 
gtween = GTween.new(sprite, 0.1, {x = 240}, {delay = 0.1 * i, ease = easing.outBounce, repeatCount = 1, reflect = true})
gtween:addEventListener("complete", function(data) counter = counter + 1 print(data, counter) end, i)
gtween.dispatchEvents = true
end

Likes: Ghando, arcadia, Leehan

+1 -1 (+3 / -0 )Share on Facebook

Comments

  • john26john26 Maintainer
    edited August 2012
    I've been using GTween extensively and haven't come across any bugs. I don't know why you are using addEventListener here. To run a function on completion, you set the onComplete table entry. So in your case
    gtween = GTween.new(sprite, 0.1, {x = 240}, {delay = 0.1 * i, 
    ease = easing.outBounce, repeatCount = 1, reflect = true, onComplete=myfunction})
    I also don't know why you are using gtween.dispatchEvents. I have never used this.
  • @alexzhang, I am surprised that you found GTween buggy, I have been using GTween not only for some of the pure gideros samples but also for the Compatibility layer and it works like a charm, even after adding eventListeners.

    Though I had one question, if I nil the gtween handle, do I need to remove the eventListeners or would they automatically get removed as they are associated with the handle? To be on the safer side I do remove them, but Lua makes for some lazy programming, and this seems like work ;)
    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
  • for some reason the code you have looks fine but at the same time since you are overwriting the values of sprite and gtween it doesn't look right. Try to create an array and hold these values and see if there is any improvement.
    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
  • alexzhengalexzheng Guru
    edited August 2012
    You have not noticed this bug just because you have not created enough gtweens at the same time.

    And another situation is running multiple actions one by one on a sprite in short interval, it also will not work.
  • hnimhnim Member
    edited August 2012
    @alexzheng: i think in your code, you run each action on difference sprite, not only one!?
  • You have not noticed this bug just because you have not created enough gtweens at the same time.

    And another situation is running multiple actions one by one on a sprite in short interval, it also will not work.
    That would enter the optimisation territory, where you would run one GTween and update the 100 items in the same tween as they all have the same tween applied. or have a central loop and modify each of the sprites accordingly, like a particle emitter.

    and yes, maybe there is an issue with those many tweens thereby creating those many timers etc.
    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
  • atilimatilim Maintainer
    edited August 2012
    When you specify a small duration (e.g. 0.01) and set "repeatCount = 1" and "reflect = true", "complete" event is not dispatched sometimes. And this bug exists with original GTween http://gskinner.com/libraries/gtween/ also. Because this bug also exists with original GTween, honestly I don't know how to fix it :)

    I can recommend removing "repeatCount = 1, reflect = true" from your code.

    Also from our previous discussion, this code works well:
    local sprite = Bitmap.new(Texture.new("box.png"))
    sprite:setAnchorPoint(0.5, 0.5)
    stage:addChild(sprite)
     
    local tween = nil
    stage:addEventListener(Event.MOUSE_DOWN, function(e)
    	if tween then
    		tween:setPaused(true)
    	end
    	tween = GTween.new(sprite, 1, {x = e.x, y = e.y}, {ease = easing.outBounce})
    end)
  • atilimatilim Maintainer
    @OZApps I usually don't remove any event listeners. When an object is GCed, all its event listeners are removed also.
  • alexzhengalexzheng Guru
    edited August 2012


    I can recommend removing "repeatCount = 1, reflect = true" from your code.

    This trick works for this code
    All boxes can move to the destination now.
    local sprite
    local gtween
    local counter = 0
    for i = 1, 100 do
    sprite = Bitmap.new(Texture.new("box.png"))
    stage:addChild(sprite)
     
    gtween = GTween.new(sprite, 0.1, {x = 240}, {delay = 0.1 * i, ease = easing.outBounce, repeatCount = 1, reflect = true})
    gtween:addEventListener("complete", function(data) counter = counter + 1 print(data, counter) end, i)
    gtween.dispatchEvents = true
    end

  • @OZApps I usually don't remove any event listeners. When an object is GCed, all its event listeners are removed also.
    I'm with @OZApps on this one - I'm never sure exactly what's removed so I always make it a point to remove any event listeners manually (guess it's the C programmer in me :) )

    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
  • atilimatilim Maintainer
    edited August 2012
    Then I'll try to add removeAllEventListeners function as soon as possible as @nat12 suggested.

    Likes: Mells

    +1 -1 (+1 / -0 )Share on Facebook
  • That would be great, although could it take a string as well, so we can remove all event listeners of a specific type also?
  • atilimatilim Maintainer
    @zvardin I was thinking exactly this way.

    Likes: zvardin

    +1 -1 (+1 / -0 )Share on Facebook
  • @Atilim, removing the EventListeners based on a criteria would be nice. The criteria could be an Event Type, attached to an object. That would be really nice.

    Likes: atilim

    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
Sign In or Register to comment.