Page 1 of 1

zigzag enemies

Posted: Sun Dec 30, 2018 6:11 pm
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?

Re: zigzag enemies

Posted: Tue Jan 01, 2019 8:52 pm
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.