So I have been using the following code to control a rock on the screen and it works great without errors but when I switch screens (I'm using the director class) I get a repetitive error and parts of the level stops working
This is the code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | -- This is line 47 and below is 48 and so on... local function onAccelerate( event ) system.setAccelerometerInterval(50) motionx = 30 * event.xGravity end Runtime:addEventListener ("accelerometer", onAccelerate) local function moverock (event) rock.x = rock.x + motionx end Runtime:addEventListener("enterFrame", moverock) local function wrapit (event) if rock.x < -36 then rock.x = 356 elseif rock.x > 356 then rock.x = -36 end end Runtime:addEventListener("enterFrame", wrapit) --...this is line 68 and above is 67. Just thought it would help figure out the error |
Danny, it looks like the variable motionx is mentioned twice. One in the function onAccelerate and another in the function moverock.
I could be wrong, but I'm guessing that if the function moverock is called before onAccelerate is called, motionx would return nil, causing an error in line 9 of the above posted code.
Naomi
Is rock or rather rock.x declared at before line 55? Also are you removing your old runtime listeners before you switch scenes? Is rock global or local?
Yeah, Jeremy, that's a good one too.
Danny, based on the portion of code you posted and the runtime error, all we can tell is that one of the variables that appear on line #9 (presumably it's the line #55 of your code) is nil, meaning, it's never being declared/defined before it is called.
You need to figure out which variable is nil.
Naomi
Thanks for the responses! Are you are looking for it what "rock.x" is? The rock is the image, and rock.x is the movement along the x axis. I don't have rock.y because I just want it moving side to side. The image/code is posted before the sample code I gave to you.
The code above it looks like this...
1 2 3 | local rock = display.newImage("rock.png") Rock.x = 240 Rock.y = 160 |
OH SHOOT. I just realized what you need that I didn't post.
1 | Motionx = 0 |
Uncaptilize Rock.x and Rock.y and you should be set.
So I did a little bit more work on my app to today and I noticed that it wasn't throwing up the error because of "Motionx." The .x at the end of the rock.x is what's causing it. I got rid of the functions "moverock" and "wrapit" and the error stopped coming up but I wasn't able to tilt the rock side to side.
Any ideas on how to fix this?
Danny
Oh and I had just accidentally capitalized "rock" before but thank you because I had made a mistake like that earlier in my code