Problems with Director Class

Hi,

I’m writing an app which uses the Director class by Ricardo Rauber.

It’s working great with one exception.

When I switch to a screen which has a rotating image I get errors when I leave that screen.
When the director makes the switch to “mainscreen” the background image remains in place.
Eventually the emulator throws this error

file.lua:31: attempt to perform arithmetic on field ‘rotation’ (a nil value)
stack traceback:
I’ve tried a bunch of things with no luck. Here’s the current code.

[lua]-- Main menu

module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()

local localGroup = display.newGroup()

local spinBg = display.newImage( “correctBg1.png”, true )
local face = display.newImage( “smileyFace.png”, true )

change = function ()
–spinBg:removeSelf()
–face:removeSelf()
Runtime:removeEventListener( “enterFrame”, animate )
director:changeScene( “mainscreen”);

end

localGroup:insert(spinBg)
spinBg.x = display.contentWidth / 2
spinBg.y = display.contentHeight / 2

face.x = display.contentWidth / 2
face.y = display.contentHeight / 2
localGroup:insert(face)

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

Runtime:addEventListener( “enterFrame”, animate );
timer.performWithDelay( 5000, change );
return localGroup
end[/lua]
Any suggestions?
[import]uid: 24363 topic_id: 11612 reply_id: 311612[/import]

–EDIT–Just looked at your code, and saw that you already have something like this. I posted this b/c I had the same problem and this worked for me…

Visible objects are removed in Director Class, but EventListeners are not.

Try this every time you leave a scene that has your
“Runtime:addEventListener( “enterFrame”, animate )”

local function gotomenu (event)  
Runtime:removeEventListener( "enterFrame", animate )  
  
director:changeScene("menu")  
end  

Obviously, switch “menu” with whatever your next screen is called. [import]uid: 18951 topic_id: 11612 reply_id: 42214[/import]

Why is “change” not a local function? That’s the only thing that sticks out to me. [import]uid: 18951 topic_id: 11612 reply_id: 42217[/import]

I changed it to local but the same thing happens.

The screen changes as expected, but the error below starts to be thrown.
The second time the application comes back to this screen it crashes. It just displays
unscaled spinBg image which does not spin.

/file.lua:31: attempt to perform arithmetic on field 'rotation' (a nil value)
stack traceback:
 [C]: ?
 file.lua:31: in function 
 ?: in function <?:214>

Looks like this statement continues to run
spinBg.rotation = spinBg.rotation + 1 [import]uid: 24363 topic_id: 11612 reply_id: 42222[/import]

Not nearly as simple as yours, but this is how I did it. I’ve noted the variables you might want to change.

[code]

– Main menu

module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()

local localGroup = display.newGroup()

local function change( event )
–spinBg:removeSelf()
–face:removeSelf()
Runtime:removeEventListener( “enterFrame”, stars )
director:changeScene( “mainscreen”)

end

local function newStar()

– need initial segment to start
local star = display.newImage( “correctBg1.png”, true )
localGroup:insert( star )

return star
end
– Create star
local stars = {}

for i = 1, 1 do

local myStar = newStar()

—SET POSITION HERE—

myStar.x = -30
myStar.y = 400
myStar.rotation = 300

myStar.xScale = .6
myStar.yScale = .6

myStar:setReferencePoint( display.CenterReferencePoint )

local dr = 1, 1 —CHANGE FIRST NUMBER TO CONTROL SPINNING SPEED
myStar.dr = dr
if ( 1.5 ) then
myStar.dr = -dr
end

table.insert( stars, myStar )
end
function stars:enterFrame( event )

for i,v in ipairs( self ) do
v.rotation = v.rotation + v.dr
end
end

Runtime:addEventListener( “enterFrame”, stars )

local face = display.newImage( “smileyFace.png”, true )

face.x = display.contentWidth / 2
face.y = display.contentHeight / 2
localGroup:insert(face)

timer.performWithDelay( 5000, change );

return localGroup
end

[/code] [import]uid: 18951 topic_id: 11612 reply_id: 42276[/import]

Also note that “animate” has become “stars” - obviously in my game, it’s stars that are spinning…

Let me know if it works! [import]uid: 18951 topic_id: 11612 reply_id: 42277[/import]

I am getting pretty much the same error.

 test.lua:60: attempt to perform arithmetic on field 'rotation' (a nil value)
stack traceback:
 [C]: ?
 /Users/mkrasucki/Desktop/app/apptest/test.lua:60: in function 
 ?: in function <?:214>

Looks like it’s the same kind or error.

Maybe it’s an issue with the emulator?
I’m using version 2011.484 (2011.4.12) [import]uid: 24363 topic_id: 11612 reply_id: 42301[/import]

That’s odd. Are you performing the same functions on the following screen? I’m using the same version, and I plugged this code into one of my projects and it worked well. With your original code, I get an error.

I’ll create a sample project using this code and email it to you to see if you still get an error. I’ll need your email address if you want to try this. [import]uid: 18951 topic_id: 11612 reply_id: 42303[/import]

No. The screen after this one doesn’t have any animations.
The problem with mine as well as your code is that this method continues to
run even after function “change” is called.
[lua] function stars:enterFrame( event )

for i,v in ipairs( self ) do
v.rotation = v.rotation + v.dr
–print(“rotate”)
end
end[/lua]

If I comment out the rotation call and uncomment the print function the app doesn’t
crash but it does continue to print “rotate” until I restart the app.
[import]uid: 24363 topic_id: 11612 reply_id: 42315[/import]

It doesn’t make any sense to me but I got it to work now.
In the code you have posted I moved the “change” function to line 65 (right after “stars” function)
and it started working.

I also went back to my code and after making the same change it started working as well.

Thank you for your help, I really appreciate it. [import]uid: 24363 topic_id: 11612 reply_id: 42317[/import]

Great! Glad I could help!

Jeff [import]uid: 18951 topic_id: 11612 reply_id: 42335[/import]