enemy move towards player

i was making changes to the star explorer template,i want to change the asteroids and make them enemies so i need them to move towards the player.

i tried to use transition.moveBy(x=ship.x,y=ship.y,{time=5000})

my problem is that it sends them to the player’s position and leaves them there but i wanted the asteroids/enemies to continue the movement and get out of the screen.

The asteroids are physics bodies. The best way to move them is using physics. If you look at line 108 in chapter 3 (http://docs.coronalabs.com/guide/programming/03/index.html) you will see:

newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

Simply change that to player.x, player.y to compute the velocity. You may not be able to use those value directly since setLinearVelocity needs a speed value and just a distance to the player could make the asteroids move too fast.

Rob

thanks! 

how about that? it worked.

local function gameLoop()

– Create new asteroid

createAsteroid()

– Remove asteroids which have drifted off screen

for i = #asteroidsTable, 1, -1 do

local thisAsteroid = asteroidsTable[i]

transition.to( thisAsteroid, { x=ship.x,y=display.contentHeight+200, time=5000})

and i am going to use the same for the enemies laser

i am learning so much! but last night i got a big headache trying to solve it.

the best tip ever in every aspect of life is : sleep over it.

i am not sure i understand you answer here.

newAsteroid:setLinearVelocity( player.x,player.y) will just make the speed of the asteroid equivalent to the value of the ship position,where is the logic in that?

one of us did not understand the other?..

anyway it was solved.

thanks rob ! you are always very responsive. 

don’t work too much!

I have a small snippet of code somewhere on my mac at home (I’m at work at the moment unfortunately) that does exactly what you’re after.  I can look it up tonight if that’s any help (assuming noone else jumps in first).

Alternatively, @roaminggamer has some brilliant actions in his (highly recommended) SSK2 library that would fit the bill as well - LINK.

I said you may not be able to use those values directly. setLinearVelocity sets a speed to move on the X-axis and a speed to move on the Y-axis. It currently sets it to a random amount of X movement and a random amount of Y movement. 

But using the player.x, player.y values you can get a speed to use along the X-axis and Y-axis that should move the object toward the player where ever the player is.

Here is a quick modification I made:

local function createAsteroid() local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 ) table.insert( asteroidsTable, newAsteroid ) physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) newAsteroid.myName = "asteroid" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newAsteroid.x = -60 newAsteroid.y = math.random( 500 ) -- newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) newAsteroid:setLinearVelocity( ship.x \* 0.25, ship.y \* 0.25 ) elseif ( whereFrom == 2 ) then -- From the top newAsteroid.x = math.random( display.contentWidth ) newAsteroid.y = -60 -- newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) newAsteroid:setLinearVelocity( ship.x \* 0.25 \* -1, ship.y \* 0.25 ) elseif ( whereFrom == 3 ) then -- From the right newAsteroid.x = display.contentWidth + 60 newAsteroid.y = math.random( 500 ) -- newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) newAsteroid:setLinearVelocity( ship.x \* 0.25 \* -1, ship.y \* 0.25 ) end newAsteroid:applyTorque( math.random( -6,6 ) ) end

It doesn’t perfectly aim the asteroids at the ship, but it does in a general manner. Also one set of the three asteroids are not being computed correctly, but it should get you started. You may need to use some basic triangle math to calculate the velocities. Hopefully, the community who has more experience at this can chime in.

Because the asteroids are physics objects, you should use physics to move them. When you use transition.to() you’re fighting the physics engine. 

Rob

i love u.

the ssk2 is wow! it’s a must !

@roaminggamer is a god!

…it’s not working :frowning:

this one works for me,i am posting it in case some new noob wants it.

the only problem with that may be the speed of the asteroids but i like it.

local function createAsteroid()

if (ship.isBodyActive==true)then

local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

table.insert( asteroidsTable, newAsteroid )

physics.addBody( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } )

newAsteroid.myName = “asteroid”

local whereFrom = math.random( 3 )

if ( whereFrom == 1 ) then

– From the left

newAsteroid.x = -60

newAsteroid.y = math.random( 250 )

– newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

newAsteroid:setLinearVelocity( (ship.x - newAsteroid.x) , ship.y - newAsteroid.y )

elseif ( whereFrom == 2 ) then

– From the top

newAsteroid.x = math.random( display.contentWidth )

newAsteroid.y = -60

– newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) )

newAsteroid:setLinearVelocity( (ship.x - newAsteroid.x), ship.y - newAsteroid.y)

elseif ( whereFrom == 3 ) then

– From the right

newAsteroid.x = display.contentWidth + 60

newAsteroid.y = math.random( 250 )

– newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )

newAsteroid:setLinearVelocity( (ship.x - newAsteroid.x) , ship.y - newAsteroid.y)

end

newAsteroid:applyTorque( math.random( -6,6 ) )

end

end

The asteroids are physics bodies. The best way to move them is using physics. If you look at line 108 in chapter 3 (http://docs.coronalabs.com/guide/programming/03/index.html) you will see:

newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

Simply change that to player.x, player.y to compute the velocity. You may not be able to use those value directly since setLinearVelocity needs a speed value and just a distance to the player could make the asteroids move too fast.

Rob

thanks! 

how about that? it worked.

local function gameLoop()

– Create new asteroid

createAsteroid()

– Remove asteroids which have drifted off screen

for i = #asteroidsTable, 1, -1 do

local thisAsteroid = asteroidsTable[i]

transition.to( thisAsteroid, { x=ship.x,y=display.contentHeight+200, time=5000})

and i am going to use the same for the enemies laser

i am learning so much! but last night i got a big headache trying to solve it.

the best tip ever in every aspect of life is : sleep over it.

i am not sure i understand you answer here.

newAsteroid:setLinearVelocity( player.x,player.y) will just make the speed of the asteroid equivalent to the value of the ship position,where is the logic in that?

one of us did not understand the other?..

anyway it was solved.

thanks rob ! you are always very responsive. 

don’t work too much!

I have a small snippet of code somewhere on my mac at home (I’m at work at the moment unfortunately) that does exactly what you’re after.  I can look it up tonight if that’s any help (assuming noone else jumps in first).

Alternatively, @roaminggamer has some brilliant actions in his (highly recommended) SSK2 library that would fit the bill as well - LINK.

I said you may not be able to use those values directly. setLinearVelocity sets a speed to move on the X-axis and a speed to move on the Y-axis. It currently sets it to a random amount of X movement and a random amount of Y movement. 

But using the player.x, player.y values you can get a speed to use along the X-axis and Y-axis that should move the object toward the player where ever the player is.

Here is a quick modification I made:

local function createAsteroid() local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 ) table.insert( asteroidsTable, newAsteroid ) physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) newAsteroid.myName = "asteroid" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newAsteroid.x = -60 newAsteroid.y = math.random( 500 ) -- newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) newAsteroid:setLinearVelocity( ship.x \* 0.25, ship.y \* 0.25 ) elseif ( whereFrom == 2 ) then -- From the top newAsteroid.x = math.random( display.contentWidth ) newAsteroid.y = -60 -- newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) newAsteroid:setLinearVelocity( ship.x \* 0.25 \* -1, ship.y \* 0.25 ) elseif ( whereFrom == 3 ) then -- From the right newAsteroid.x = display.contentWidth + 60 newAsteroid.y = math.random( 500 ) -- newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) newAsteroid:setLinearVelocity( ship.x \* 0.25 \* -1, ship.y \* 0.25 ) end newAsteroid:applyTorque( math.random( -6,6 ) ) end

It doesn’t perfectly aim the asteroids at the ship, but it does in a general manner. Also one set of the three asteroids are not being computed correctly, but it should get you started. You may need to use some basic triangle math to calculate the velocities. Hopefully, the community who has more experience at this can chime in.

Because the asteroids are physics objects, you should use physics to move them. When you use transition.to() you’re fighting the physics engine. 

Rob

i love u.

the ssk2 is wow! it’s a must !

@roaminggamer is a god!

…it’s not working :frowning:

this one works for me,i am posting it in case some new noob wants it.

the only problem with that may be the speed of the asteroids but i like it.

local function createAsteroid()

if (ship.isBodyActive==true)then

local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

table.insert( asteroidsTable, newAsteroid )

physics.addBody( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } )

newAsteroid.myName = “asteroid”

local whereFrom = math.random( 3 )

if ( whereFrom == 1 ) then

– From the left

newAsteroid.x = -60

newAsteroid.y = math.random( 250 )

– newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

newAsteroid:setLinearVelocity( (ship.x - newAsteroid.x) , ship.y - newAsteroid.y )

elseif ( whereFrom == 2 ) then

– From the top

newAsteroid.x = math.random( display.contentWidth )

newAsteroid.y = -60

– newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) )

newAsteroid:setLinearVelocity( (ship.x - newAsteroid.x), ship.y - newAsteroid.y)

elseif ( whereFrom == 3 ) then

– From the right

newAsteroid.x = display.contentWidth + 60

newAsteroid.y = math.random( 250 )

– newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )

newAsteroid:setLinearVelocity( (ship.x - newAsteroid.x) , ship.y - newAsteroid.y)

end

newAsteroid:applyTorque( math.random( -6,6 ) )

end

end