problems with game state and clicking mouse

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
randy0428
Citizen
Posts: 51
Joined: Sun Jul 31, 2016 10:19 am

problems with game state and clicking mouse

Post by randy0428 »

This post is about 2 problems I’m having with a game I’m developing. I have attached the project directory as a .zip file.

I am an ESL teacher (English as a Second Language) in Brazil teaching English to native Portuguese speakers. This game is to be a listening exercise for the students. They will listen to an audio clip (which I may eventually add to the game, but not at this time) and then they will play this game.

The objective of the game is to complete the text of the audio clip. The game starts with the splash screen where they can choose to see instructions or start the game. When they start the game, they are shown the play screen which presents 3 sprites, each of which are “saying” something in a text balloon. The player clicks on the sprite they think is saying the first words of the text (in subsequent rounds they choose the next text).

If they choose the correct text, they get the correctClick screen which tells them they were correct and adds their selected text to the text box. There’s a button on this screen for them to click to continue playing, which increments the phrase and returns them to the joga (play) screen, This continues until all of the text is completed. (This game has 26 phrases to complete the text.) If they complete the text before making 3 mistakes, they win and this screen will tell them they’ve won and there will be buttons to either quit or play again.

If they choose the wrong sprite, they get a wrong answer screen, which tells them their answer was wrong and tells them how many more mistakes they can make (a total of 3) before they lose the game. If they’ve made 3 mistakes, this screen will tell them they’ve lost and there will be buttons to either quit or play again.

The game has 5 games states and a screen to go with each. These games states and screens (represented by files) are:
splash (splash.lua)
instrucoes (instructions – instrucoes.lua)
play (joga – joga.lua)
cliqueCerto (correctClick – cliqueCerto.lua)
cliqueErrado (wrongClick – cliqueErrado)

Debugging tools:
I added a few things to help with debugging that will be removed when the game is finished. In main.lua I added code so I can click left ctrl + left shift + a letter to switch to each game state.

In the mousereleased function in theMouse.lua I added print() statements in each section so I would know when each section was run and what the game state was when that section started to run.

Problems I can’t figure out:
1. In the splash screen, when you click the bottom button to start playing the game you should ALWAYS go to the play (joga) screen. Sometimes it takes me to the correct screen, meaning the game state (estadoJogo) is joga. Sometimes it INCORRECTLY takes me to the correctClick screen (meaning the game state is cliqueCerto), without getting any input to know if it was a correct click or not. And sometimes it INCORRECTLY takes me to the wrongClick screen (meaning the game state is cliqueErrado), without getting any input to know if it was a correct click or not.

2. Sometimes, when I’m in the game playing screen, I click on the correct sprite it just updates the joga game state. What it should do (and frequently does) is go to the correctClick screen to show me I answered correctly. Then, when I click on the button, it should increment to the next round of the game and take me back to the play screen. It appears to be doing all of the processes I just described, I mean, it appears to be moving to the next round as it should, but it doesn’t show me the correctClick screen. Frequently it DOES show the correctClick screen, but sometimes it doesn’t and I can’t figure out why?
Attachments
insects.zip
(385.99 KiB) Downloaded 74 times
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: problems with game state and clicking mouse

Post by 0x72 »

Hi!

Sorry for lengthy answer; I hope that it's helpful nonetheless:

Ad. #1

Problem

Take a look on your `love.mousereleased` in theMouse.lua - you have there something like:

Code: Select all

function love.mousereleased(x, y, button)
	if estadoJogo == "splash" then
		-- here sometimes do:
		estadoJogo = "joga"
	end

	if estadoJogo == "joga" then
		-- here sometimes do:
		estadoJogo = "cliqueCerto"
		-- and sometimes:
		estadoJogo = "cliqueErrado"
	end
	
  
	if estadoJogo == "cliqueCerto" then
 		-- logic here
	end
  
	if estadoJogo == "cliqueErrado" then
 		-- logic here
	end
end
Give it a moment - read line by line. If you're in the first if-statement you can set estadoJogo to "joga" and then you move to the next if-statement but the variable was changed to "joga", so the condition becomes true, so it is executed as well. If it happens that you give correct or wrong answer with the same mouserelease you've clicked the button in the splash screen then you'll move to different state accordingly.

basically:

Code: Select all

local x = 5
if x == 5 then
	print('x is 5')
	x = 6
end
if x == 6 then
	print('x is 6')
end
print("omg, how come that x == 5 and 6 at the same time O.o") -- well it doesn't it equals 5 first and a moment **later** it's 6
3rd answer button is roughly the same place as the button on the splash screen - the answers are random so SOMETIMES this button is correct, and sometimes it's not, and sometimes you click on the place where they don't overlap, hence it appeared random to you.

Solutions

There could be many solutions to this problem:

1) quick and dirty ones:
1.1) you could add return statement in each topmost if statement:

Code: Select all

if estadoJogo == "splash" then
	-- logic
	return
end
if estadoJogo == "joga" then
	-- logic
	return
end
-- ...
1.2) you could use elseif so only one of the if's bodies is executed for given mouserelease callback call:

Code: Select all

if estadoJogo == "splash" then
	-- logic
elseif estadoJogo == "joga" then
	-- logic
elseif
-- ...

2) you could avoid so much global state checking everywhere and make your app more maintainable separating states.

2.1)
E.g. instead of holding estadoJogo as a string and checking what it's equal to everywhere so many times, it would be better to have it as an object with some functions:

Code: Select all

estadoSplash = {
  name = 'splash', -- or isSplashState = true, or whatever
  draw = function() --[[...]] end,
  update = function() --[[...]] end,
  mousereleased = function() --[[...]] end
}

-- then you can set global state with just:
estadoJogo = estadoSplash -- or some helper function if you wish

-- you still can check what's the current state
if estadoJogo.name == 'splash' then
	print('it\'s splash')
end

-- and in the callbacks you'd have something like:
function love.draw()
	estadoJogo:draw() -- or with "."?, depends on your implementation
end

function love.update(dt)
	estadoJogo:update(dt)
end

function love.mousereleased(x, y, button)
	estadoJogo:mousereleased(x, y, button)
end


2.2)
Use some state lib, although I'll recommend 2.1 for sake of learning

---

Ad. #2.

I believe it's basically the same thing.

good luck :)
Last edited by 0x72 on Fri Jan 13, 2017 12:04 am, edited 1 time in total.
randy0428
Citizen
Posts: 51
Joined: Sun Jul 31, 2016 10:19 am

Re: problems with game state and clicking mouse

Post by randy0428 »

Thank you, thank you, thank you, 0x72!

Such a simple solution!

I just added a "return" line immediately before the "end" of each of the "if" sections for the "splash" game state. And this solved both problems As you said, the 2nd problem had the same cause as the first.

Obviously, your quick and dirty answer #1 was the easiest to implement and that's what I used, However, I'm going to study your solution 2.1 and perhaps I'll implement that, either in this game or a subsequent version of it. I am pretty new with LÖVE, so I'll need to study it, but it doesn't look too complicated.

As for your solution 2.2 it will be a while before I can try that. You may have noticed from my coding that I'm not familiar with using libraries at this early stage of my LÖVE education, so using a library is out for now.

Thanks again for your taking the time to look at this program and helping me with developing it.

Randall
Post Reply

Who is online

Users browsing this forum: No registered users and 246 guests