"enemy" moving between two points (patroling) - how?

I want to make an enemy patrol between two points on the screen.

I made this:

function moveghost(event) ghost1.y = ghost1.y +1 if ghost1.y == 162 then ghost1.y = ghost1.y -1 end end Runtime:addEventListener( "enterFrame", moveghost)

When I start the scene, the ghost moves down to y162 and stays there.

How can i make it, that it moves permanently between starting point and my defined point?

The movement is also not really nice. It’s somehow jerky. Is there a solution, so the movement gets more fluent?

Thanks in advance! [import]uid: 6587 topic_id: 5695 reply_id: 305695[/import]

Have you thought about using transition.to to move your enemies instead of an event listener? You could set an onComplete function at the end of your transition.to statements that moves it from one position back to the other.

I could post an example if you like.

-Clark [import]uid: 5786 topic_id: 5695 reply_id: 19500[/import]

I am a beginner in programming, so I would absolutely appreciate a sample code.

That would be cool!

Thanks! [import]uid: 6587 topic_id: 5695 reply_id: 19501[/import]

I solved this. Not really nice probably, because I have more enemies from the same type and I just added all of them one by one in the code, but anyway, it works.

[code]wallunder = 192
wallabove = 64
local xdirection,ydirection = 0,1
local ghost1xpos,ghost1ypos = 112,64
local ghost1 = display.newImage (“images/ghost.png” ,ghost1xpos, ghost1ypos);

local function animate(event)
ghost1ypos = ghost1ypos + ( 5 * ydirection );

if (ghost1ypos > wallunder or ghost1ypos < wallabove) then
ydirection = ydirection * -1;
end

ghost1:translate( ghost1xpos - ghost1.x, ghost1ypos - ghost1.y)

end
Runtime:addEventListener( “enterFrame”, animate );[/code] [import]uid: 6587 topic_id: 5695 reply_id: 19547[/import]

object = display.newRect( 0, 0, 50, 50)
object.x = 100

function goLeft ()
transition.to( object, { time=1500, x=(object.x - 50), onComplete=goRight } )
end

function goRight ()
transition.to( object, { time=1500, x=(object.x + 50), onComplete=goLeft } )
end

goLeft()

This is probably how i would do it =) [import]uid: 9577 topic_id: 5695 reply_id: 19555[/import]

That’s cool!

Thanks!

It’s shorter and simpler :slight_smile: [import]uid: 6587 topic_id: 5695 reply_id: 19565[/import]