Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
addEventListener in Mashballs scene — Gideros Forum

addEventListener in Mashballs scene

saeyssaeys Member
edited April 2015 in Code snippets
Hi all,
I'm a big newbie, going through the Mashballs tutorial in the Gideros Mobile Game Development book.

In the start scene, there are 3 addEventListeners, but only the last one in the code works, the other 2 doesn't seem to "listen", I get no error on them either. If I move one of the two not-working addEventListeners to be the last, that one begins to work, and the other two before it doesn't. Hope you get what I mean...

Hope anyone can explain how to get several evenlisteners working in the same scene... =) (sorry for some swedish in the code, just ignore them)

Cheers, thomas

---
StartScene = Core.class(Sprite)

function StartScene:init()

print("\ninside start scene")

--lägg till bakgrundsbilden
local bg = Bitmap.new(Texture.new("images/bg.jpg", true))
bg:setAnchorPoint(0.5,0.5)
bg:setPosition(conf.width/2, conf.height/2)
self:addChild(bg)

--lägg till logotypen
local logo = Bitmap.new(Texture.new("images/logo.png", true))
logo:setAnchorPoint(0.5,0.5)
logo:setPosition(conf.width/2, conf.height/2 - 50)
self:addChild(logo)

--lägg till textknapp "starta spelet", lyssna efter onMouseUp
local startText = TextField.new(conf.fontLarge, "Starta spelet")
startText:setTextColor(0xffff00)
local startButton = Button.new(startText)
startButton:setPosition((conf.width - startButton:getWidth())/2, conf.height - 80)
self:addChild(startButton)
startButton:addEventListener("click", function()
print("startaspeletknappen")
sceneManager:changeScene("level", conf.transitionTime, conf.transition, conf.easing)
end)

--lägg till textknapp "inställningar", lyssna efter onMouseUp
local optionsText = TextField.new(conf.fontMedium, "Inställningar")
optionsText:setTextColor(0xffff00)
local optionsButton = Button.new(optionsText)
optionsButton:setPosition(30, conf.height - 30)
self:addChild(optionsButton)
optionsButton:addEventListener("click", function()
print("optionsknappen")
sceneManager:changeScene("options", conf.transitionTime, conf.transition, conf.easing)
end)

--lägg till textknapp "om spelet", lyssna efter onMouseUp
local aboutText = TextField.new(conf.fontMedium, "Om spelet")
aboutText:setTextColor(0xffff00)
local aboutButton = Button.new(aboutText)
aboutButton:setPosition(conf.width - aboutButton:getWidth() - 30, conf.height - 30)
self:addChild(aboutButton)
aboutButton:addEventListener("click", function()
print("aboutknappen")
sceneManager:changeScene("about", conf.transitionTime, conf.transition, conf.easing)
end)

Comments

  • saeyssaeys Member
    Hmm, did I just post this in the category? :-/
  • piepie Member
    @saeys that's strange. I believe that the issue is somewhere else in your project.
    did you notice that there's an "end" missing to close your init? :) maybe it's just this, maybe you missed it pasting in forum.
    However to lua-format a post use tags:
    < pre lang = " lua " >
    your lua code
    </ pre >
    without spaces. it's a lot easier to read.

    I made a little copy/paste of your init function in a empty game template, had to change
    some things in references but your code is ok to me - just the init() end missing :)
    rar
    rar
    changesceneSaeys.rar
    17K
  • saeyssaeys Member
    thanks pie, I just missed pasting the last "end" in the forum, sorry about that. Thank you though for the tag tip!

    I've gone through dependencies (don't think what's causing it) and references but can't find anything wrong (although I bet I'm missing something). What puzzles me is that, again, it's always the *last* addEventListener in the code that's leading to a expected behavior, it's like the two before weren't there. No matter how I rearrange them. I'm having a hard time understanding that something else in my project could be causing this.

    The code one more time, readable: ;;)
    StartScene = Core.class(Sprite)
     
    function StartScene:init()
     
    	print("\ninside start scene")
     
    --lägg till bakgrundsbilden
    	local bg = Bitmap.new(Texture.new("images/bg.jpg", true))
    	bg:setAnchorPoint(0.5,0.5)
    	bg:setPosition(conf.width/2, conf.height/2)
    	self:addChild(bg)
     
    --lägg till logotypen	
    	local logo = Bitmap.new(Texture.new("images/logo.png", true))
    	logo:setAnchorPoint(0.5,0.5)
    	logo:setPosition(conf.width/2, conf.height/2 - 50)
    	self:addChild(logo)
     
    --lägg till textknapp "starta spelet", lyssna efter onMouseUp
    	local startText = TextField.new(conf.fontLarge, "Starta spelet")
    	startText:setTextColor(0xffff00)
    	local startButton = Button.new(startText)
    	startButton:setPosition((conf.width - startButton:getWidth())/2, conf.height - 80)
    	self:addChild(startButton)
    	startButton:addEventListener("click", function()
    		print("startaspeletknappen")
    		sceneManager:changeScene("level", conf.transitionTime, conf.transition, conf.easing)
    	end)
     
    --lägg till textknapp "om spelet", lyssna efter onMouseUp
    	local aboutText = TextField.new(conf.fontMedium, "Om spelet")
    	aboutText:setTextColor(0xffff00)
    	local aboutButton = Button.new(aboutText)
    	aboutButton:setPosition(conf.width - aboutButton:getWidth() - 30, conf.height - 30)
    	self:addChild(aboutButton)
    	aboutButton:addEventListener("click", function()
    		print("aboutknappen!")
    		sceneManager:changeScene("about", conf.transitionTime, conf.transition, conf.easing)
    	end)
     
    --lägg till textknapp "inställningar", lyssna efter onMouseUp
    	local optionsText = TextField.new(conf.fontMedium, "Inställningar")
    	optionsText:setTextColor(0xffff00)
    	local optionsButton = Button.new(optionsText)
    	optionsButton:setPosition(30, conf.height - 30)
    	self:addChild(optionsButton)
    	optionsButton:addEventListener("click", function()
    		print("optionsknappen!")
    		sceneManager:changeScene("options", conf.transitionTime, conf.transition, conf.easing)
    	end)
     
    --lyssna efter tryck på androids bakknapp
    	self:addEventListener(Event.KEY_DOWN, function(event)
    		if event.keyCode == KeyCode.BACK then
    			if application:getDeviceInfo() == "Android" then
    				application:exit()
    			end
    		end
    	end)
    end
  • piepie Member
    @saeys I can see nothing wrong here :-??

    Try comparing your project with my previous upload, the basics of scene manager and event listeners should be almost the same.
    As an alternative you can zip and upload your project I will take a look to it (no warranties aout success though :D ) .
  • saeyssaeys Member
    I compared with the complete project code that came with the book, and I found a piece of code in a button class that I, in an earlier stage, experimented with. "Back then" the "experiment" went well, but left there it caused this unwanted behavior. Deleted, all good now!

    Thanks anyway for your quick replies! =D>
Sign In or Register to comment.