[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## enemy move towards player Started by alberto.diego.chisilevich Nov 08 2018 02:15 PM

7 replies to this topic

Best Answer alberto.diego.chisilevich , 10 November 2018 - 12:43 AM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
• Observer

• 28 posts
• Corona SDK

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.

[TOPIC: post.html]
#2

### Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
• Moderator

• 25,373 posts
• Enterprise

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

[TOPIC: post.html]
#3

### alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
• Observer

• 28 posts
• Corona SDK

thanks!

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.

[TOPIC: post.html]
#4

### alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
• Observer

• 28 posts
• Corona SDK

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

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!

[TOPIC: post.html]
#5

### Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
• Contributor

• 427 posts
• Corona SDK

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.

• alberto.diego.chisilevich likes this

[TOPIC: post.html]
#6

### Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
• Moderator

• 25,373 posts
• Enterprise

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

• alberto.diego.chisilevich likes this

[TOPIC: post.html]
#7

### alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
• Observer

• 28 posts
• Corona SDK

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 love u.

the ssk2 is wow! it's a must !

@roaminggamer is a god!

......it's not working

[TOPIC: post.html]
#8

### alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
• Observer

• 28 posts
• Corona SDK

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

[topic_controls]

[/topic_controls]