Animation

I have an object that I am just rotating like a gear on my screen. The screens work fine before I put the animation on but after I put on the animation which does animate the object, it seems to pause my other screens. Meaning when I click the rotating object to go to the next screen, my buttons on the other screen don’t work. They are just regular buttons like a back button or a next screen button.

Is there something I need to do to the other screens in the code or in a UI file or something to ensure the animation continues to work and not mess the other screens up? I basically want this object to rotate anytime the screen is launched. This is the code below which does work to animate the object.

[lua]local function animate( event )
block.rotation = block.rotation + 1
end

Runtime:addEventListener( “enterFrame”, animate );[/lua] [import]uid: 72372 topic_id: 13218 reply_id: 313218[/import]

Are you using Director to switch screens? Could you post the screen transition code? Do you have animation code for all the buttons on all screens? [import]uid: 27965 topic_id: 13218 reply_id: 48531[/import]

Are you removing the Runtime listener when you switch scenes? Are you getting any errors? It seems to be the most common issue when using Director :wink: [import]uid: 52491 topic_id: 13218 reply_id: 48534[/import]

I am using director to change the screen. I can’t post the code at the moment. Im typing from my phone right now.

i am not removing the listener. i didnt know i had to. how do i do that and where do i put the code. [import]uid: 72372 topic_id: 13218 reply_id: 48540[/import]

In the function where you change scenes put this line of code;

[lua]Runtime:removeEventListener( “enterFrame”, animate )[/lua]

That will remove it and kill that error :slight_smile:

Peach [import]uid: 52491 topic_id: 13218 reply_id: 48553[/import]

Peach,

I tried that but it didn’t work. I also tried the code listed below and it’s not working either.

[lua]local brain = display.newImage (“brain.png”)
brain.x = 240
brain.y = 220
brain.xScale = .5
brain.yScale = .5
localGroup:insert(brain)
–>This places the brain

local function touchedBrain (event)
if (“ended” == event.phase) then
director:changeScene (“screen2”)
media.playEventSound( “click_x.caf” )
end
end

brain:addEventListener (“touch”, touchedBrain)

local function animate( event )
brain.rotation = brain.rotation + 1
end

Runtime:addEventListener( “enterFrame”, animate );

–>adds Animation to brain

function brain:tap( event )
if brain.isPaused then – initially nil which is false anyways
Runtime:removeEventListener( “enterFrame”, self )
else
Runtime:addEventListener( “enterFrame”, self )
end
return true – we handled the event so don’t propagate
end

brain:addEventListener( “tap”, brain )

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment
–REMOVE THE FOLLOWING LINE:

return localGroup

end[/lua] [import]uid: 72372 topic_id: 13218 reply_id: 48559[/import]

In the code you posted your not removing any of the eventListeners and the brain:tap function is trying to remove a Runtime listener that doesn’t exist and add a Runtime listener that calls a function that doesn’t exist. With Director it is easiest to have a clean up function that you can call on scene change because it makes your code more organized and easier to track down problems. I have revised your code and commented on the changes[lua]local brain = display.newImage (“brain.png”)
brain.x = 240
brain.y = 220
brain.xScale = .5
brain.yScale = .5
localGroup:insert(brain)
–>This places the brain

local function animate( event )
brain.rotation = brain.rotation + 1
end

Runtime:addEventListener( “enterFrame”, animate );

–>adds Animation to brain

– This cleanUp function will remove all listeners from the scene since
– Director can’t remove listeners itself
local cleanUp = function()
brain:removeEventListener(“touch”, touchedBrain)
Runtime:removeEventListener(“enterFrame”, animate)
end

local function touchedBrain (event)
if (“ended” == event.phase) then
media.playEventSound( “click_x.caf” )
cleanUp() – Call the cleanUp function to remove what director can’t
director:changeScene(“screen2”) – director should clean up the rest
end
end

brain:addEventListener (“touch”, touchedBrain)

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment
–REMOVE THE FOLLOWING LINE:

return localGroup

end[/lua] [import]uid: 27965 topic_id: 13218 reply_id: 48595[/import]

calebr2048,

I tried your code and the animation worked but the screens would not transition. I then moved the director change scene line right above the clean up line and the screens started transitioning for two screens then stopped and but would not go back. So I still have the same problem I had before. I posted the code again below with the one change I did.

[lua]local brain = display.newImage (“brain.png”)
brain.x = 240
brain.y = 220
brain.xScale = .5
brain.yScale = .5
localGroup:insert(brain)
–>This places the brain

local function animate( event )
brain.rotation = brain.rotation + 1
end

Runtime:addEventListener( “enterFrame”, animate );
–>adds Animation to brain

– This cleanUp function will remove all listeners from the scene since
– Director can’t remove listeners itself
local cleanUp = function()
brain:removeEventListener(“touch”, touchedBrain)
Runtime:removeEventListener(“enterFrame”, animate)
end

local function touchedBrain (event)
if (“ended” == event.phase) then
media.playEventSound( “click_x.caf” )
director:changeScene(“screen2”) – director should clean up the rest
cleanUp() – Call the cleanUp function to remove what director can’t

end
end

brain:addEventListener (“touch”, touchedBrain)

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment

return localGroup

end[/lua] [import]uid: 72372 topic_id: 13218 reply_id: 48598[/import]

Not sure what I just did but it works now!!!

[lua]local brain = display.newImage (“brain.png”)
brain.x = 240
brain.y = 220
brain.xScale = .5
brain.yScale = .5
localGroup:insert(brain)
–>This places the brain

local function animate( event )
brain.rotation = brain.rotation + 1
end

Runtime:addEventListener( “enterFrame”, animate );
–>adds Animation to brain

– This cleanUp function will remove all listeners from the scene since
– Director can’t remove listeners itself

local cleanUp = function()
Runtime:removeEventListener(“enterFrame”, animate)
brain:removeEventListener(“touch”, touchedBrain)
end

local function touchedBrain (event)
if (“ended” == event.phase) then
media.playEventSound( “click_x.caf” )
director:changeScene(“screen2”) – director should clean up the rest
cleanUp() – Call the cleanUp function to remove what director can’t

end
end

brain:addEventListener (“touch”, touchedBrain)

–>MUST return a display.newGroup()


–> This is how we end every file except for director and main, as mentioned in my first comment

return localGroup

end[/lua] [import]uid: 72372 topic_id: 13218 reply_id: 48600[/import]