Animation (solved)

I have a screen that has animation on it, like a rotating gear. When I leave and go to other screens then come back the animation starts back working, which is what I want.

I just made another screen. All it has on it is a background and a backbitten. When I leave my animated screen, go to this screen and come back, my animation doesn't work and then the screen freezes. What is in the code on this new screen that is causing a problem?

Also when I go to this screen that only has a background and a button, I get a ton of runtime errors in the terminal. where as when I go to other screens nothing. I can't figure it out.

This is the screen with the animation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
module(..., package.seeall)
 
function new()
local localGroup = display.newGroup()
--> This is how we start every single file or "screen" in our folder, except for main.lua
        -- and director.lua
        --> director.lua is NEVER modified, while only one line in main.lua changes, described in that file
------------------------------------------------------------------------------
------------------------------------------------------------------------------
local ui = require("ui")
 
 
 
local menu = display.newImage ("menu.png")
localGroup:insert(menu)
--> This sets the menu
 
local about = display.newImage ("about.png")
about.x = 100
about.y = 300
about.xScale = .5
about.yScale = .5
localGroup:insert(about)
 
local function touchedAbout (event)
        if ("ended" == event.phase) then
                director:changeScene ("about")
                media.playEventSound( "click_x.caf" )
                        
        end
end
 
about:addEventListener ("touch", touchedAbout)
 
local credits1 = display.newImage ("credits1.png")
credits1.x = 165
credits1.y = 375
credits1.xScale = .5
credits1.yScale = .5
localGroup:insert(credits1)
 
local rateit = display.newImage ("rateit.png")
rateit.x = 225
rateit.y = 300
rateit.xScale = .5
rateit.yScale = .5
localGroup:insert(rateit)
-->This places the rateit button
 
local function doRating(event)
if event.phase == "ended" then
local url = "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa"
url = url .. "/wa/viewContentsUserReviews?"
url = url .. "type=Purple+Software&id="
url = url .. "453641732"
 
system.openURL(url)
end
end
 
rateit:addEventListener("touch", doRating)
--> Adds the Rate It functionality. Be sure to change the APP ID to your OWN;
 
local brain = display.newImage ("brain.png")
brain.x = 155
brain.y = 185
brain.xScale = .6
brain.yScale = .6
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)
    about:removeEventListener ("touch", touchedAbout)
end     
 
local function touchedBrain (event)
    if ("ended" == event.phase) then
        media.playEventSound( "click_x.caf" )
        director:changeScene("titlescreen") -- 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

The runtime errors are likely the problem.

Possibly try moving your cleanUp function to above the director:changeScene() in your code, rather than below it.

Let me know if that makes a difference.

No when I do that, the director stops working.

If you upload your project I might be able to take a closer look at your errors; no guarantees but it would work a lot easier than trying to do so here.

Peach:)

Can you give me an email and I will send it to you.

This is the runtime error, i'm getting over and over.

Runtime error
/Users/ebay/Desktop/MindTapper/menu.lua:93: attempt to perform arithmetic on field 'rotation' (a nil value)
stack traceback:
[C]: ?
/Users/ebay/Desktop/MindTapper/menu.lua:93: in function
?: in function <?:215>

You might need to make sure "brain" still exists before rotating it in the animate function in your first file. There's probably a more elegant way to do it but try replacing the function with this and see if you still get the error:

1
2
3
4
5
local function animate( event )
    if brain.x ~= nil then
        brain.rotation = brain.rotation + 1
    end
end

WOOOOOW It worked!!! What did that do? I have been working on this for 2 days trying to figure out what was going on...

Can you explain to me what that did?

Again thank you so much!!!

BTW that code can stay there in the final product right? or do I need to put something else?

The cause of the error is that when director change scene brain object is removed but runtime event listener is still running trying to rotate an object which is not there(just for a few seconds before the clean up is called). what peach suggested is possible solution for this problem but it might have broken something else.that is why you got an error.

so u can just add the below line
Runtime:removeEventListener( "enterFrame", animate );
before the changeScene in touchedBrain function of the first file. this will actually clear the issue.

one more thing,not the cause of the error but a suggestion.you don't have to manually remove the event listener attached to a display object if it is added to the localGroup (returned as part of director). You need to take care of the Runtime event listeners only.

Looking at your code again and from what renvis said you could also try putting "cleanup()" (line 92) ABOVE the director:changeScene("titlescreen") line. If that fixes your error then that would be a better solution than what I previously wrote.

But since you asked, basically what the "if brain.x ~= nil then..." function is doing is verifying, on every frame, that "brain" exists as a display object before rotating it. If it doesn't exist then it won't do anything.

Your error message was the clue. If an object's .rotation property is 'nil' then it's no longer a display object and likely has been removed completely. Performing the director transition first BEFORE removing the "EnterFrame" listener in the cleanup function causes there to be a fraction of a second where the listener is trying to rotate an object that no longer exists, which is an error condition.

@XenonBL well explained !! :)

views:1656 update:2011/10/5 8:48:05
corona forums © 2003-2011