Page 1 of 1

### My enemies keep moving right.

Posted: Thu Jun 18, 2015 8:18 pm
Hello, I am wrathguy78 and I am making a game where you have to shoot enemies to win! I made the AI so the enemies follow the player but they are only heading right. This is the code:

Code: Select all

``````function enemy.AI(dt)
for i,v in ipairs(enemy) do
if player.x + player.width / 2 < v.x / 2 then
if v.xvel > -enemy.speed then
v.xvel = v.xvel - enemy.speed * dt
end
end
if player.x + player.width / 2 > v.y / 2 then
if	v.xvel < enemy.speed then
v.xvel = v.xvel + enemy.speed * dt
end
end
end
end
``````
If you could help that would be awesome!

-Wrath

### Re: My enemies keep moving right.

Posted: Thu Jun 18, 2015 8:39 pm
It looks like you've put some if statements in the wrong places. Could you upload a .love file so the context is clear?

### Re: My enemies keep moving right.

Posted: Thu Jun 18, 2015 8:44 pm
Hi!

1) There is probably no reason to divide enemy x and y by 2 when comparing it with centre position of the player (if there is the there is the issue )

2) you compare x and y but then only update xvel of the enemy, not sure if enemy is list of enemies (suggested by the loop) or single one (suggested by the second part), I encourage you to name tables plural so it's easier to read.

### Re: My enemies keep moving right.

Posted: Thu Jun 18, 2015 8:47 pm
DeltaF1 wrote:It looks like you've put some if statements in the wrong places. Could you upload a .love file so the context is clear?
Nvm, it's fixed

### Re: My enemies keep moving right.

Posted: Thu Jun 18, 2015 8:53 pm
0x72 wrote:Hi!

1) There is probably no reason to divide enemy x and y by 2 when comparing it with centre position of the player (if there is the there is the issue )

2) you compare x and y but then only update xvel of the enemy, not sure if enemy is list of enemies (suggested by the loop) or single one (suggested by the second part), I encourage you to name tables plural so it's easier to read.
1 was the issue thanks! xD Idk why either. I changed file sprites so that could have been the problem