Getting crash from movement loop

So I have no problem getting my character to move one direction. What this code is supposed to do is loop him back and forth between x cords. of 1 and 400. But it is causing a crash. I think it may be from declaring moveRight being False then calling it right toward the end.

If anyone can tell me what I’m doing wrong here I would really appreciate it, I’ve been working on it all day.

[code]
local moveRight = true
local function move (event) – Moves and updates the Player’s movement
if moveRight == true then
if turtle.x < 400 then
turtle.applyForce( 8, 0)
elseif turtle.x>400 then
moveRight = false
end
elseif moveRight == false then
if turtle.x>1 then
turtle.applyForce( -8, 0 )
elseif turtle.x<1 then
moveRight = true
end

end

end

Runtime:addEventListener(“enterFrame”, move)

[/code] [import]uid: 42079 topic_id: 8094 reply_id: 308094[/import]

Have you tried transition?

[lua]if turtletween then transition.cancel(turtletween) end

local moveTurtle function()
local backMove function()
transition.to(turtle, {time = 5000, x = 1, onComplete=moveTurtle})
end
turtletween = transition.to(turtle, {time=5000, x=400, onComplete=backMove})

end

moveTurtle()
– code has not been tested [import]uid: 12455 topic_id: 8094 reply_id: 28872[/import]

It will crash when trying to call physics methods if the object is not a physics object and physics is not turn on.

Also, you are potentially calling ‘applyForce’ very frequently. I realise this is probably not the case here, but if you were to call applyForce on an object for every frame, it would probably crash - it’s simply excessive. [import]uid: 8271 topic_id: 8094 reply_id: 28922[/import]

Thanks for the replies.

I first did use transition to but that is not a real physic’s movement and therefor doesn’t respond “naturally” to friction of surface which I need.

It is a dynamic physical object and I don’t turn it off. The only one I turn off is a platform during the drag, but that works fine.

Apply force doesn’t crash it at all, I will check memory usage though, but in this case it is the loop to bring it back causing the crash. Apply force works great without the return loop.

Any other ideas would be super helpful, thanks. [import]uid: 42079 topic_id: 8094 reply_id: 28965[/import]

if you were to call applyForce on an object for every frame, it would probably crash - it’s simply excessive

Actually that is precisely how you are supposed to use applyForce; keep calling applyForce as long as the force should be applied. You’re probably getting it confused with applyImpulse which is a sudden jolt rather than a continuous force. [import]uid: 12108 topic_id: 8094 reply_id: 28970[/import]

When I put your code into my own test case, I got this:

[lua]local physics = require(“physics”)

physics.start()

local turtle = display.newCircle( 200,50,50 )
physics.addBody( turtle, “dynamic” )

local moveRight = true
local function move (event) – Moves and updates the Player’s movement
if moveRight == true then
if turtle.x < 400 then
turtle:applyForce( 8, 0)
elseif turtle.x>400 then
moveRight = false
end
elseif moveRight == false then
if turtle.x>1 then
turtle:applyForce( -8, 0 )
elseif turtle.x<1 then
moveRight = true
end

end

end

Runtime:addEventListener(“enterFrame”, move)[/lua]

And so the issue, in mine at least, shows that it is the .applyForce should actually be :applyForce

Does that help?

m [import]uid: 8271 topic_id: 8094 reply_id: 28976[/import]

ah good spot. And for fidelispeer this is a great example of why you need to pay attention to the exact error message in Terminal and not simply say “darn it crashed.” The Terminal would have been pointing out exactly which line has a syntax error on it. [import]uid: 12108 topic_id: 8094 reply_id: 28977[/import]

Hey thanks so much guys, that made it go back perfectly. Then it just stopped. So I looked at the two blocks that dealt with the applyForce movement and pulled the parenthesis back toward the zero and it works great now.

I have been using the terminal but I should have caught that, since I kinda wrote that code myself I thought there was some bigger problem than just a simple syntax, ye of little faith.

Again thank you so much, huge help! [import]uid: 42079 topic_id: 8094 reply_id: 29002[/import]