Hey there !
I’ve got an issue with managing sprites and “timing performances”. I’m using the latest daily build (which should be the 2015.8.27)
I’ve got a sprite, on which I’m applying a sprite listener. Once the animation is finished, it’s supposed to call a function. But “sometimes”, it seems to cause some errors. Here is a simple exemple (download the exemple here or see the attachement file).
local mySprite local sheetData\_item = {width=80, height=80, numFrames=10, sheetContentWidth=400, sheetContentHeight=160} local mySheet\_item = graphics.newImageSheet("spritesheet\_paint.png", sheetData\_item) local sequenceData\_item ={{name = "normalRun", start=1, count=10, time=1000, loopCount=1}} -- Creating and placing the sprite mySprite = display.newSprite(mySheet\_item, sequenceData\_item) mySprite.x = display.contentWidth\*0.5 mySprite.y = display.contentHeight\*0.5 -- Callback function local function callbackFunction() print("callbackFunction()") end -- Callback function local function callbackFunction2() print("callbackFunction2()") end -- Dealing with the animation local function spriteListener(event) if event.phase == "ended" then -- Calling the callback function print("ended") callbackFunction() end end mySprite:addEventListener("sprite", spriteListener) mySprite:play(); -- Timer timer.performWithDelay(1001, callbackFunction2)
This should run fine : once launched, this script should play the animation, and in the console, it should (will) print this :
callbackFunction() callbackFunction2()
The problem is when the devices is slow (because lot of stuff might be running in the app, or because it’s simply too old), the animation doesn’t match the timing. The animation seems to skip frames, which means the spriteListener is triggered too early compared to the timer.
And on the simulator, there is actually one way to test this : launch the previous script and pause immediatly the simulator (Ctrl + Shift + Down). Wait for like 2 seconds and unpause the simulator and you’ll see that the animation will be instantly finished. While the timer will end correctly a little bit later.
On device, this becomes a huge problem when a lot of things are running (physics, enterFrame, or other stuff running in the background).
In the end, it seems that sprite listener aren’t that reliable. I really need to know if an animation has ended in order to call other functions and it breaks a lot of things. Is there an other way ? Am I doing something wrong ? Is this a bug ?
).