My enemies keep moving right.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Wrathguy78
Prole
Posts: 10
Joined: Sun Jul 27, 2014 4:54 pm

My enemies keep moving right.

Post by Wrathguy78 »

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
User avatar
DeltaF1
Citizen
Posts: 64
Joined: Mon Apr 27, 2015 4:12 pm
Location: The Bottom of the Stack
Contact:

Re: My enemies keep moving right.

Post by DeltaF1 »

It looks like you've put some if statements in the wrong places. Could you upload a .love file so the context is clear?
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: My enemies keep moving right.

Post by 0x72 »

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.
Wrathguy78
Prole
Posts: 10
Joined: Sun Jul 27, 2014 4:54 pm

Re: My enemies keep moving right.

Post by Wrathguy78 »

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
Wrathguy78
Prole
Posts: 10
Joined: Sun Jul 27, 2014 4:54 pm

Re: My enemies keep moving right.

Post by Wrathguy78 »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests