Weird behavior of back key

A project to port LÖVE to Android handhelds
Locked
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Weird behavior of back key

Post by undef »

Hi,

I'm having a weird error on android.
My escape key is mapped to gameOver( "skip" ), and it works perfectly fine on PC.
Since the back key on android is mapped to the escape key it should work just the same, but unfortunately it doesn't...
If I press back on my android device while in game, the function seems to be partially triggered.

Code: Select all

function gameOver( arg, stage, completed )
	love.update = updateMenu
	stageNotStarted = true
	keys = {
		escape = function()
			love.graphics.reset()
			love.audio.stop()
			stageDraw = titleScreen.drawStage
			speed = titleScreen.speed
			t=0
			if stage=="tutorial" then
				setKeys( titleScreen.mainMenuKeys )
				isTutorial = nil
				love.touchpressed = titleScreen.mainMenuTouchpressed
			else
				setKeys( titleScreen.chooseStageKeys )
				love.touchpressed = titleScreen.chooseStageTouchpressed
			end
		end,
	}
	keys["return"] = function() keys.escape(); initStage( stage ) end
	keys[" "] = function() keys.escape(); initStage( stage ) end
	love.touchpressed = function( id, x, y )
		if x>((w-h)/2)/w and x<((w+h)/2)/w then
			keys["return"]()
		end
	end
	if arg=="skip" then
		keys.escape()
	else
		stageDraw = createGameOverScreen( completed )
	end
	newRecord = nil
end
I'll explain it with this extract of the above function:

Code: Select all

			love.graphics.reset() -- doesn't get triggered
			love.audio.stop() -- doesn't get triggered
			stageDraw = titleScreen.drawStage -- does work
So I kind of get back to the title screen, but only partially, if I press the back button once more on the other hand, I'm back on the title screen and everything seems to work fine.

Another very strange thing though is, that this only seems to happen when I first press the back key after launch.
If I enter the stage again and repeat the process, everything seems to work fine.

This problem occurs with a galaxy s2 and a nexus 7.

The error is most probably mine, but I can't seem to find it, and I'd be happy if someone could help me...
If my explaination is insuffcient please tell me so, I will try to specify.

Thanks in advance!
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Weird behavior of back key

Post by Positive07 »

Yeah probably your mistake... Also you are playing with touchpressed too much and overwriting it many times it would probably be better to create an if elseif elseif else end with all the conditions (states) of your game. Also I couldnt find why
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Weird behavior of back key

Post by undef »

Thanks for having a look at it anyways.
What is the advantage of having a huge if-block like that?
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Weird behavior of back key

Post by undef »

I just found out something peculiar!

Regardless of where in the game i press the back button (including in the menus), the first time I press it after launch it doesn't work.
So I guess it's not related to my game over code after all.
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Weird behavior of back key

Post by slime »

undef wrote:the first time I press it after launch it doesn't work
Is that still the case with a .love that only has this code in its main.lua?

Code: Select all

function love.keypressed(key)
    if key == "escape" then
        assert(false)
    end
end
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Weird behavior of back key

Post by undef »

slime wrote:
undef wrote:the first time I press it after launch it doesn't work
Is that still the case with a .love that only has this code in its main.lua?

Code: Select all

function love.keypressed(key)
    if key == "escape" then
        assert(false)
    end
end
It didn't, and I found the error.
I was so focused on my gameOver function that I didn't look in a lot of other places.
My mistake was that I changed love.keypressed after a splashscreen if you pressed any button, and I changed love.touchpressed after the splashscreen if you touched anywhere.
But I didn't change love.keypressed with love.touchpressed.
That's why the first time I pressed back my love.keypressed was still in splashscreen condition.

A rather simle error after all, but just in case someone else makes a similar mistake, I wrote this explaination.
twitter | steam | indieDB

Check out quadrant on Steam!
Locked

Who is online

Users browsing this forum: No registered users and 83 guests