[Resolved] Moving Clouds Problem

Hi Community

In my game I have function which is moving clouds - like the one in Ghost Vs. Monsters sample app. But when I switch screens with Director, I get the following error: “attempt to perform arithmetic on field ‘x’ (a nil value)”. I do remove the Runtime Enterframe listener with the “clean function - integrated in Director”, but I still get the error. I can’t get rid of that error, which is very frustrating.

Here is my code:

[lua]local clouds1
local clouds2
local Background

local drawBackground = function()

Background = display.newImageRect(“Bagground3.png”, 480, 320 )
Background.x = 240
Background.y = 160
– CLOUDS
clouds1 = display.newImageRect( “clouds-left.png”, 480, 320 )
clouds1.x = 240; clouds1.y = 160

clouds2 = display.newImageRect( “clouds-right.png”, 480, 320 )
clouds2.x = 720; clouds2.y = 160

LevelGroup:insert(Background)
LevelGroup:insert(clouds1)
LevelGroup:insert(clouds2)

end

– Main enterFrame Listener
local gameLoop = function()
if gameIsActive then

– MOVE CLOUDS SLOWLY
local cloudMoveSpeed = 0.5

clouds1.x = clouds1.x - cloudMoveSpeed
clouds2.x = clouds2.x - cloudMoveSpeed

if clouds1.x <= -240 then
clouds1.x = 1680
end

if clouds2.x <= -240 then
clouds2.x = 1680
end

end
end

local gameInit = function()

local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 9.8 )

drawBackground()
–createGround()

–createLevel()
–drawHUD()

Runtime:addEventListener(“touch”, tapIt)
Runtime:addEventListener( “enterFrame”, gameLoop )
Runtime:addEventListener( “collision”, onLocalCollision )

local Checktimer = timer.performWithDelay(1000, CheckFunction, 0 )

end

unloadMe = function()

physics.stop()

stopwatch = nil
Runtime:removeEventListener( “enterFrame”, gameLoop )
Shapes[i]:removeEventListener( “touch”, dragBody )
Runtime:removeEventListener( “collision”, onLocalCollision )

Runtime:removeEventListener(“touch”, tapIt)

Check:dispose()

if Checktimer then timer.cancel( Checktimer ); end
Checktimer = nil

end

gameInit()[/lua]
Thank you for the help - it is very appreciated :slight_smile: [import]uid: 122802 topic_id: 26707 reply_id: 326707[/import]

I’m sure I’m overlooking something in the above as you are obviously removing it - is there any chance of you uploading a sample I could run that shows the issue? If so I would be happy to take a look. [import]uid: 52491 topic_id: 26707 reply_id: 108285[/import]

Hi Peach

Thank you for your quick reply. Here is an app showcasing the issue:

http://min.us/mQLTTzQyB

Thank you for the help in advance. [import]uid: 122802 topic_id: 26707 reply_id: 108300[/import]

I am not used to work with director but the unload function is never called, is this a part of director?

If the game is not playing you could set it like this:

[code]

gameIsActive = false;
director:changeScene( “menu”, “fade” )

[/code] [import]uid: 81188 topic_id: 26707 reply_id: 108371[/import]

Hi

That’s odd, because in my other scenes the clean/unload function seems to work fine. And the clean function should be a part of Director. Thank you for your input :slight_smile: [import]uid: 122802 topic_id: 26707 reply_id: 108434[/import]

Line 46, add;
[lua]unloadMe()[/lua]

I think that should kill the error :slight_smile:

Peach [import]uid: 52491 topic_id: 26707 reply_id: 108444[/import]

Hi Peach

Thank you, but if i add unloadMe() the clouds dosen’t move at all :(.

Thank you in advance for the help.

EDIT: I almost got it to work properly, now when I swich screens the animations on my menu screen dosen’t work. I will try getting it solved, but if I don’t can, I hope it’s okay that I post the problem here? thank you very much for the help! Very appreciated :slight_smile: [import]uid: 122802 topic_id: 26707 reply_id: 108475[/import]

If you add that to line 46 in level1.lua (immediately above the line; [lua]director:changeScene( “menu”, “fade” )[/lua]) then the clouds don’t stop moving until you click the menu button and no error message now.

Is that not what you wanted? If I’ve misunderstood please clarify for me.

As to posting further issues in this thread, sure, if related to this that’s totally fine :slight_smile: (Though if is totally different problem a new thread is a good idea ;))

Peach [import]uid: 52491 topic_id: 26707 reply_id: 108610[/import]

Hi Peach

You have not misunderstood anything :slight_smile: - it was just me that thought that I needed to add the call to the unloadme() function at line 46 in the code I have posted above - that will say inside the enterframe function. After I posted the reply to this thread, I tried to put the call to the function at line 46 in the sample app I have sent you and obviously that worked :slight_smile: I just don’t understand why I need to call the function - when Director should call the clean function right before I change scene.

I have one more question: Do I need to set the clouds to nil and remove them manually + other objects in my clean function, when I am using Director?

Thank you very much for the help :slight_smile: [import]uid: 122802 topic_id: 26707 reply_id: 108659[/import]

Not a problem; I can’t comment on Director specifics RE clean up as I’m not actually sure of the answer, though I imagine there are some notes on it on Ricardo’s website.

RE the clouds and nil - no - not if they are in localGroup.

Peach :slight_smile: [import]uid: 52491 topic_id: 26707 reply_id: 108797[/import]