Accelerometer Error

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…

-- 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  

and this is the error…

Runtime error
…nykilkenny/Desktop/Today’s Tutorials/acc_ball/L1.lua:55: attempt to perform arithmetic on field ‘x’ (a nil value)
stack traceback:
[C]: ?
…nykilkenny/Desktop/Today’s Tutorials/acc_ball/L1.lua:55: in function <…nykilkenny tutorials>
?: in function <?:215>
This keeps going on forever. Please help me stop it :smiley:

Thank you,
Danny

[import]uid: 59140 topic_id: 16772 reply_id: 316772[/import] </…nykilkenny>

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
[import]uid: 67217 topic_id: 16772 reply_id: 62785[/import]

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? [import]uid: 23649 topic_id: 16772 reply_id: 62790[/import]

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 [import]uid: 67217 topic_id: 16772 reply_id: 62792[/import]

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…

local rock = display.newImage("rock.png")  
Rock.x = 240  
Rock.y = 160  

I’m not very well versed in nil variables so I’m afraid I don’t understand. Are you saying that I should have the image code first then the moveRock function then the onAccelerate function in that order? [import]uid: 59140 topic_id: 16772 reply_id: 62794[/import]

OH SHOOT. I just realized what you need that I didn’t post.

 Motionx = 0   

I let that out sorry about that. When I tried to put in a number above 0 it would throw up an error, I’ll try to get that error posted in the morning. Unless you have a better solution?
Thanks again. [import]uid: 59140 topic_id: 16772 reply_id: 62798[/import]

Uncaptilize Rock.x and Rock.y and you should be set. [import]uid: 23649 topic_id: 16772 reply_id: 62805[/import]

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 [import]uid: 59140 topic_id: 16772 reply_id: 62942[/import]

Bump [import]uid: 59140 topic_id: 16772 reply_id: 63303[/import]

Try declaring motion before the function if you haven’t already. [import]uid: 23649 topic_id: 16772 reply_id: 63316[/import]

I’ve tried every which combo I can think of but nothing is working. I’m considering just reworking the system if I can’t get this figured out. Do you know of any really good tutorials that do a great job of a sliding maneuver? Also do you think this could be a bug or just something wrong with my code? I’d be happy to post more code if anyone can think of an area that might be throwing this off, and I’m open to questions.

Thanks,
Danny [import]uid: 59140 topic_id: 16772 reply_id: 63699[/import]

I don’t mean to be pushy but I’m really desperate to figure this out as I’m very close to releasing and it’s one of the many bugs I’m trying to stomp with a steel foot. Any one want to chime in? [import]uid: 59140 topic_id: 16772 reply_id: 63817[/import]

When you calculate the new value of motionx (line 4), what are you expecting? Should this fall within a certain range? I would put a print statement right after that and print it out to make sure it’s what you expect.

I would combine moverock and wrapit into a single function.

What type of cleanup are you doing before you call director to change a scene?

[import]uid: 67839 topic_id: 16772 reply_id: 63832[/import]

Could you please post the full code for this module and I’ll try to help you when I get a chance.

Thanks. [import]uid: 23649 topic_id: 16772 reply_id: 63870[/import]

I’m not doing any clean up…maybe I should look into that. Although I think I tried but something wasn’t working and it wasn’t critical at the time. I’m not looking for any specific range it’s just what was in the game I found but the results were great. Thanks for the response!

Jeremy: I get nervous about posting code to the forums, not that I don’t want to help out. I’ll try to post some code to help people later when everything is working right… My email is dannylego@ameritech.net. Would you be ok with sending me yours. Thanks for coming back to the forums to help me.

Danny [import]uid: 59140 topic_id: 16772 reply_id: 63874[/import]

I just emailed you, but my email is my username@gmail.com [import]uid: 23649 topic_id: 16772 reply_id: 63875[/import]

have you got any results?
i’ve got a similar problem with director, with accelerometer.
When i change scene, it don’t work anymore, saying: “attempt to call method ‘applyLinearImpulse’ (a nil value)”

here is the code:
[lua]local function onAccelerate( event )
if event.isShake == true then
sangle = ((math.random (-5, 5))/100)
ball_f:applyLinearImpulse( sangle, -0.3, ball_f.x, ball_f.y )
print (sangle)
end
end
Runtime:addEventListener (“accelerometer”, onAccelerate)[/lua]

i clean the listener when i exit:
[lua]local function menugame()
Runtime:removeEventListener(“accelerometer”,onAccelerate)
Runtime:removeEventListener(“enterFrame”, followFunction)
Runtime:removeEventListener(“enterFrame”, gameLoop)
TextCandy.CleanUp()
director:changeScene(“menu”)
end[/lua]
but it don’t work… :frowning:

it works when i insert the “Runtime:removeEventListener(“accelerometer”,onAccelerate)” in the onAccelerate function, but then i can “bump” my ball only once, so it’s not the point… [import]uid: 44010 topic_id: 16772 reply_id: 64612[/import]

My problem was I had “curly” quotes, instead of straight quotes. So if anything in quotes is not highlighted in pink check the quotes.
How is menuGame() triggered is it a button press, after something happened, or other? [import]uid: 59140 topic_id: 16772 reply_id: 64617[/import]

menuGame is trigerred when you press a button. [import]uid: 44010 topic_id: 16772 reply_id: 64620[/import]

solved, but i don’t know if it’s right:
i removed the “local” from the function “onAccelerate”.
But doesn’t this make the function global?
What is the difference between “local function” and “function”? [import]uid: 44010 topic_id: 16772 reply_id: 64625[/import]