zigzag enemies

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
stsmuniz
Prole
Posts: 4
Joined: Sun Dec 30, 2018 5:45 pm

zigzag enemies

Post by stsmuniz »

I'm making my first game in löve and having my first try to lua and I did a little topdown shooter based on http://www.osmstudios.com/tutorials/lov ... the-basics.
So far It has been a good experience, but now I'm having issues trying to make the enemies "zigzag" on the screen.

Here's what I tried so far:

Code: Select all

		for i, enemy in ipairs(enemies) do
			enemy.y = enemy.y + (enemySpeed * dt)
			enemy.spin = enemy.spin + (createEnemyTimerMax/100)
			enemy.x = enemy.x + ((math.tan(math.rad(enemy.spin))*100) * dt)
			if enemy.spin >= 360 then
				enemy.spin = 0
			end

			if enemy.y > 530 then
				table.remove(enemies, i)
			end
		end
Any tips of how can I make the enemies to move?
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: zigzag enemies

Post by ReFreezed »

Store a direction value on the enemy that says whether it's moving left or right. When the enemy is close to the edge of the screen, reverse the direction. When you update the x position, just check the direction value.

To limit the enemy's position to part of the screen instead of the whole screen you could have a "base x position" value for each enemy, and if the enemy strays too far from that value you reverse the direction.

Note that the way you remove enemies here will make the for loop skip the next enemy right after a removal. You should iterate backwards through the array.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 42 guests