[SOLVED] Moving Clouds.

I have an issue. I am trying to get some clouds that I have designed scrolling in the background on all of my menu pages. I found in the sample code the following piece of code and tried it in my menu.lua file and it works but when i touch a button on the screen plenty of errors keep coming up.

-- a bunch of clouds  
-- change the y value on tree[1].x = 20; tree[1].y = 110 to set height  
local tree = {}  
tree[1] = display.newImage( "images/cloud 6.png" )  
tree[1].xScale = 0.7; tree[1].yScale = 0.7  
tree[1]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[1].x = 20; tree[1].y = 110  
tree[1].dx = 0.1   
tree[2] = display.newImage( "images/cloud 3.png" )  
tree[2].xScale = 0.6; tree[2].yScale = 0.6  
tree[2]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[2].x = 120; tree[2].y = 140  
tree[2].dx = 0.2  
tree[3] = display.newImage( "images/cloud 1.png" )  
tree[3].xScale = 0.8; tree[3].yScale = 0.8  
tree[3]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[3].x = 200; tree[3].y = 160  
tree[3].dx = 0.1  
tree[4] = display.newImage( "images/cloud 2.png" )  
tree[4].xScale = 0.5; tree[4].yScale = 0.5  
tree[4]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[4].x = baseline; tree[4].y = 130  
tree[4].dx = 0.4  
tree[5] = display.newImage( "images/cloud 2\_2.png" )  
tree[5].xScale = 0.6; tree[5].yScale = 0.8  
tree[5]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[5].x = 300; tree[5].y = 140  
tree[5].dx = 0.2  
tree[6] = display.newImage( "images/cloud 4.png" )  
tree[6].xScale = 0.9; tree[5].yScale = 0.5  
tree[6]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[6].x = 320; tree[6].y = 130  
tree[6].dx = 0.6  
tree[7] = display.newImage( "images/cloud 5.png" )  
tree[7].xScale = 0.4; tree[7].yScale = 0.4  
tree[7]:setReferencePoint( display.BottomCenterReferencePoint )  
tree[7].x = 380; tree[7].y = 160  
tree[7].dx = 0.7  
  
-- A per-frame event to move the elements  
local tPrevious = system.getTimer()  
local function move(event)  
 local tDelta = event.time - tPrevious  
 tPrevious = event.time  
  
 local xOffset = ( 0.2 \* tDelta )  
  
 local i  
 for i = 1, #tree, 1 do  
 tree[i].x = tree[i].x - tree[i].dx \* tDelta \* 0.2  
 if (tree[i].x + tree[i].contentWidth) \< 0 then  
 tree[i]:translate( 480 + tree[i].contentWidth \* 2, 0 )  
 end  
 end  
end  
  
-- Start everything moving  
Runtime:addEventListener( "enterFrame", move );  
  

I error I get says that there is an error in line 95 which is the following line. “tree[i].x = tree[i].x - tree[i].dx * tDelta * 0.2”
but i have tried everything.

Is there a way to stop this when i tap the button. I have been told to stop a runtime and they gave me a piece of code to try but it just didn’t work.

If there is no way of stopping the erros popping up (i will post the error below) is there something else i an do to get some clouds to move in the background?

The error i get back is:

\document path//corona projectRuntime Error
\document path//menu.lua:95: attempt to perform arithmetic on field ‘x’ stack traceback

This error keeps repeating itself.

I really need some help. I am so close to finishing this project and this is one of the last things i need to do.

Thanks
Ryan [import]uid: 111430 topic_id: 26579 reply_id: 326579[/import]

I have the exact same issue… help to solve this would be very appreciated :smiley: [import]uid: 122802 topic_id: 26579 reply_id: 107774[/import]

I think the first error may relate to this line

tree[4].x = baseline;
If baseline is nil, then the maths won’t work. I don’t see it defined in your code anywhere.

And for stopping the animation, add a runtime listener for touch
When it fires, remove the enterframe listener
[import]uid: 108660 topic_id: 26579 reply_id: 107784[/import]

I know it is not the baseline as I use the baseline as the level of the ground below. I use the following line of code for that

local baseline = 280  

This works really well and it saves time on getting everything lined up with the same values.

I get what you mean about stopping the animation. What piece of code would I have to use?

Thanks
Ryan [import]uid: 111430 topic_id: 26579 reply_id: 107786[/import]

see if all the elements are non nil by using

if tree[i] then

–your stuff
else

print (“one of the elements is nil!”)
end

stopping the animation:

function onTouch(event)  
 Runtime:removeEventListener( "enterFrame", move )  
end  
  
--add a listener  
Runtime:addEventListener( "touch", onTouch)  

[import]uid: 108660 topic_id: 26579 reply_id: 107794[/import]

Thank you so much for your help. I have solved it. The line you posted:

Runtime:removeEventListener( "enterFrame", move )  

I inserted this to my code where when the player touches the button it stops the animation and then it will go to the other scene.

the code now looks like this.

--credits button function  
  
menuDisplay:insert(closingButton)  
  
local function buttonListener( event )  
Runtime:removeEventListener( "enterFrame", move )  
director:changeScene( "closing", "fade" )  
return true  
end  
  
closingButton:addEventListener("touch", buttonListener )  

it has worked and Thank you for your help.
Now I can continue with the development.

Thanks
Ryan [import]uid: 111430 topic_id: 26579 reply_id: 107797[/import]