if statement fails randomly but works most times

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
ketmore
Prole
Posts: 2
Joined: Thu Jul 07, 2022 10:40 pm

if statement fails randomly but works most times

Post by ketmore »

Hello, im new on the forum and with love2d in general, but i have intermediate coding skills.

So basically, I have expanded on a game that is made in the following tutorial: (http://osmstudios.com/tutorials/your-fi ... -exercises)
I added many changes and features which u will be able to read about on the first line of my main.lua
In the game, after the player dies, there is a prompt that tells the player to press 'R' to restart. This is completed through an 'if' statement in my love.update function (line 192).
In the original iteration of this games code, i never seem to run into any problems, however, on my iteration it fails every so often while I'm playing the game. This is regardless of the fact that i barely even touched this section of the code, I only added a line at the end that resets the players health.
Also, sometimes I respawn with 3 health when its supposed to be 4, and I feel like sometimes the asteroids spawn outside the screen but I'm not sure if I'm just tripping?
Can anybody help me figure out whats causing these bugs? I'm pretty sure its a simple fix but I can't quite put my finger on it.
I attached my .love file below, Thanks! :awesome:
SpaceOps.love
(3.66 MiB) Downloaded 82 times
User avatar
marclurr
Party member
Posts: 146
Joined: Fri Apr 22, 2022 9:25 am

Re: if statement fails randomly but works most times

Post by marclurr »

Your code to reset the player is inside your enemy collision loop. If there are no enemies in the enemy list there that code will not be executed. Move this block of code underneath the enemy collision loop and it works better:

Code: Select all

	if health == 0 then
		isAlive = false
	end
	--if not isAlive then isExploring=false end
	if not isAlive and love.keyboard.isDown('r') then
		-- remove all our bullets and enemies from screen
		bullets = {}
		enemies = {}

		-- reset timers
		canShootTimer = canShootTimerMax
		createEnemyTimer = createEnemyTimerMax
		health=4
		-- move player back to default position
		player.x = 50
		player.y = 410
		-- reset our game state
		score = 0
		isAlive = true
		health=4
	end
ketmore
Prole
Posts: 2
Joined: Thu Jul 07, 2022 10:40 pm

Re: if statement fails randomly but works most times

Post by ketmore »

marclurr wrote: Fri Jul 08, 2022 9:10 am Your code to reset the player is inside your enemy collision loop. If there are no enemies in the enemy list there that code will not be executed. Move this block of code underneath the enemy collision loop and it works better:

Code: Select all

	if health == 0 then
		isAlive = false
	end
	--if not isAlive then isExploring=false end
	if not isAlive and love.keyboard.isDown('r') then
		-- remove all our bullets and enemies from screen
		bullets = {}
		enemies = {}

		-- reset timers
		canShootTimer = canShootTimerMax
		createEnemyTimer = createEnemyTimerMax
		health=4
		-- move player back to default position
		player.x = 50
		player.y = 410
		-- reset our game state
		score = 0
		isAlive = true
		health=4
	end
This definitely fixed the restart issue and the health glitch, thank you. Any idea how i can make the meteors kinda like zero gravity and be able to spawn when i move horizontally? If you dont mind teaching
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests