Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Need Help — Gideros Forum

Need Help

markusmarkus Member
edited July 2012 in General questions
Hi all. I am new here. I try to combine two examples Ebook template and example project under Hardware -- "Keyboard"
I just copy the code from Hardware/Keyboard example into sceneController.lua. So when I try to print out any message erevything is ok
function onKeyDown(event)
if event.keyCode == KeyCode.LEFT then
print("menu Button Down")
end
end

But when when I try to move, for example self.left button the APPLICATION՝ player, terminated (closed)
function onKeyUp(event)
if event.keyCode == KeyCode.LEFT then
print ("menu Button Up")
self.left:setPosition(100, 100)
end
end
Please help me to understand what is wrong, seems I cant do anything with other objects on the stage

Comments

  • My guess is you aren't passing in a copy of self when you register the event listener. There is no automatic reference to self in Lua. When you call a function using a colon, there is a reference to self automatically passed in as a hidden parameter.

    The addEventListener function allows you to pass in additional data however, so you can do something like this:
    something:addEventListener(Event.KEY_DOWN, onKeyDown, something)
  • @markus: Try taking a look at the Button example. You will see in the init function of the class all the addEventListener calls pass in a reference to themselves.
  • please write here the lua error.
    Don't forget that lua files are processed from up to down. If you are trying to alter some vaiable out of scope it will raise error. Like before init of a class, trying to use self.left
  • markusmarkus Member
    thanks for reply actually there isn't error code, when I press the button player shuts. also if it necessary I can post the whole project.
  • please post if you can.
  • markusmarkus Member
    edited July 2012
    THE WHOLE CODE
    sceneController =  gideros.class(Sprite)
     
    local keyNames = {
    		[KeyCode.BACK] = "back",
    		[KeyCode.SEARCH] = "search",
    		[KeyCode.MENU] = "menu",
    		[KeyCode.CENTER] = "center",
    		[KeyCode.LEFT] = "left",
    		[KeyCode.UP] = "up",
    		[KeyCode.RIGHT] = "right",
    		[KeyCode.DOWN] = "down",
    }
    local pages = {}
    pages[1] = "Page0"
    pages[2] = "Page1"
    pages[3] = "Page2"
    pages[4] = "Page3"
    pages[5] = "Page4"
    local W = application:getContentWidth()
    local H = application:getContentHeight()
    function onKeyDown(event)
    	if event.keyCode == KeyCode.LEFT then
    		print("menu Button Down")
    	end
    end
    --"up"
    function onKeyUp(event)
    if event.keyCode == KeyCode.LEFT then		
    print ("menu Button Up")	
    GTween.new(self.menugroup, 2, {y = -H/2}, {delay = 0, ease = easing.inBounce, repeatCount = 1, reflect = true})	
    end 
    end
    stage:addEventListener(Event.KEY_DOWN, onKeyDown, self)
    stage:addEventListener(Event.KEY_UP, onKeyUp, self)
     
    function sceneController:init()
    	self.scenes = SceneManager.new({ --scenes are defined
    	["Page0"] = Page0, 	
    	["Page1"] = Page1,
    	["Page2"] = Page2,
    	["Page3"] = Page3,
    	["Page4"] = Page4,
    })
    --menu
    --music = Sound.new("sound/music4.mp3")	
    --local channel  = music:play(0, 1000)
    --channel:setVolume(0.4)	
    self:addChild(self.scenes)
    -- add the first page
    self.scenes:changeScene("Page0")
    self.pageNo = 1
    -- this is the button on the left. to go back
    --self.left = Button.new(Bitmap.new(Texture.new("gfx/left-up.png")), Bitmap.new(Texture.new("gfx/left-down.png")))
    --self.left:setPosition(leftx, lefty)
    --self:addChild(self.left)
    -- thins is the button on the right, to go fprward. 
    --self.right = Button.new(Bitmap.new(Texture.new("gfx/right-up.png")), Bitmap.new(Texture.new("gfx/right-down.png")))
    --self.right:setPosition(rightx, righty)
    --self:addChild(self.right)
    -- "click" event is mapped to the function of scenecontroller, self.leftClick
    -- when the "click" event is pressed, this function will be called.
    --self.left:addEventListener(Event.MOUSE_DOWN, self.leftClick, self)
    --self.right:addEventListener("click", self.rightClick, self) 
    --self.right:addEventListener(Event.MOUSE_DOWN, self.rightClick, self)
    --INFO MENU GROUP
    self.menugroup = Sprite.new()
     
    self.homebtn = Button.new(Bitmap.new(Texture.new("gfx/homebtn.png")), Bitmap.new(Texture.new("gfx/homebtn.png")))
    self.homebtn:setPosition(W/2-self.homebtn:getWidth()*1.5, 0)
    self.homebtn:addEventListener(Event.MOUSE_DOWN, self.homebtnClick, self)
     
    self.authorbtn = Button.new(Bitmap.new(Texture.new("gfx/authorbtn.png")), Bitmap.new(Texture.new("gfx/authorbtn.png")))
    self.authorbtn:setPosition(W/2-self.authorbtn:getWidth()/2, 0)
    --self.authorbtn:addEventListener(Event.MOUSE_DOWN, self.authorbtn, self)
     
    self.aboutbtn = Button.new(Bitmap.new(Texture.new("gfx/aboutbtn.png")), Bitmap.new(Texture.new("gfx/aboutbtn.png")))
    self.aboutbtn:setPosition(W/2+self.aboutbtn:getWidth()*0.5, 0)
    --self.aboutbtn:addEventListener(Event.MOUSE_DOWN, self.aboutbtn, self)
     
    self.closebtn = Button.new(Bitmap.new(Texture.new("gfx/closebtn.png")), Bitmap.new(Texture.new("gfx/closebtn.png")))
    self.closebtn:setPosition(W/2-self.closebtn:getWidth()*3.5, -self.closebtn:getHeight()/2)
    self.closebtn:addEventListener(Event.MOUSE_DOWN, self.closebtnClick, self)
     
    self.menugroup:addChild(self.homebtn)
    self.menugroup:addChild(self.authorbtn)
    self.menugroup:addChild(self.aboutbtn)
    self.menugroup:addChild(self.closebtn)
     
    --SHARE MENU GROUP
    sharetitle = "AppcodingEasy.com - Share button in Gideros app"
    shareurl = "<a href="http://appcodingeasy.com/Gideros-Mobile/Share-button-in-Gideros-app&quot" rel="nofollow">http://appcodingeasy.com/Gideros-Mobile/Share-button-in-Gideros-app&quot</a>;
    sharer = Share.new(sharetitle, shareurl)
    self.fbbutton = sharer:getButton("facebook")
    self.fbbutton:setPosition(W/2-self.fbbutton:getWidth(), 0-self.fbbutton:getHeight()/2)
    self.twbutton = sharer:getButton("twitter")
    self.twbutton:setPosition(W/2, 0-self.fbbutton:getHeight()/2)
    self.menugroup:addChild(self.fbbutton)
    self.menugroup:addChild(self.twbutton)
    self:addChild(self.menugroup)
     
    --INFO BUTTON
    self.infobtn = Button.new(Bitmap.new(Texture.new("gfx/infobtn.png")), Bitmap.new(Texture.new("gfx/infobtn.png")))
    self.infobtn:setPosition(W/2-self.infobtn:getWidth()/2-self.infobtn:getWidth(), H-self.infobtn:getHeight())
    self:addChild(self.infobtn)
    self.infobtn:addEventListener(Event.MOUSE_DOWN, self.infobtnClick, self)
    --SOUND BUTTON
    self.soundbtn = Button.new(Bitmap.new(Texture.new("gfx/soundonbtn.png")), Bitmap.new(Texture.new("gfx/soundonbtn.png")))
    self.soundbtn:setPosition(W/2-self.soundbtn:getWidth()/2, H-self.soundbtn:getHeight())
    self:addChild(self.soundbtn)
    --self.soundbtn:addEventListener(Event.MOUSE_DOWN, self.soundbtnClick, self)
    --SHARE BUTTON
    self.sharebtn = Button.new(Bitmap.new(Texture.new("gfx/sharebtn.png")), Bitmap.new(Texture.new("gfx/sharebtn.png")))
    self.sharebtn:setPosition(W/2-self.sharebtn:getWidth()/2+self.sharebtn:getWidth(), H-self.sharebtn:getHeight())
    self:addChild(self.sharebtn)
    --self.sharebtn:addEventListener(Event.MOUSE_DOWN, self.sharebtnClick, self)
    end
    -- INFO BUTTON CLICK
    function sceneController:infobtnClick(event)
    if (self.infobtn:hitTestPoint(event.x, event.y) == true) then 
    GTween.new(self.menugroup, 2, {y = H/2+40}, {delay = 0, ease = easing.outBounce, repeatCount = 1, reflect = true})
    end
    end
    ------------------------
    -- CLOSE BUTTON CLICK
    function sceneController:closebtnClick(event)
    if (self.closebtn:hitTestPoint(event.x, event.y) == true) then		
    GTween.new(self.menugroup, 2, {y = -H/2}, {delay = 0, ease = easing.inBounce, repeatCount = 1, reflect = true})
    end
    end
    ------------------------
    -- HOME BUTTON CLICK
    function sceneController:homebtnClick(event)
    if (self.homebtn:hitTestPoint(event.x, event.y) == true) then
    self.scenes:changeScene(pages[1],1, SceneManager.flipWithFade, easing.inBack)
    end
    end
    ------------------------
    function sceneController:leftClick()
    print(self.pageNo)
    self.pageNo = self.pageNo - 1
    if self.pageNo < 1 then self.pageNo = 1 end	
    print(self.pageNo)
    self.scenes:changeScene(pages[self.pageNo],1, SceneManager.fade, easing.inBack) 
    end 
    function sceneController:rightClick()
    self.pageNo = self.pageNo + 1
    if self.pageNo > 5 then self.pageNo = 1 end
    self.scenes:changeScene(pages[self.pageNo],1, SceneManager.fade, easing.inBack) 
    end
  • markusmarkus Member
    Thanks again
  • ar2rsawseenar2rsawseen Maintainer
    You define event listeners and pass the self reference
    stage:addEventListener(Event.KEY_DOWN, onKeyDown, self)
    stage:addEventListener(Event.KEY_UP, onKeyUp, self)
    out of the scope where self exists, out of the function sceneController:init()

    Try putting these eventlisteners inside sceneController:init method
     
    function sceneController:init()
    self:addEventListener(Event.KEY_DOWN, onKeyDown, self)
    self:addEventListener(Event.KEY_UP, onKeyUp, self)
  • markusmarkus Member
    edited July 2012
    Still no luck :(
    at this time it isn't recognized events, I mean even not print messages
  • ar2rsawseenar2rsawseen Maintainer
    And if you put something like
    print(self)
    print(self.left)
    print(self.menugroup)
    what does it print out?

    You can try to put them one by one if player crashes
  • hnimhnim Member
    edited July 2012
    i think 'self' will be nil. Try put 2evenListener as @ar2rsawseen and add sceneController before your function:
    function sceneController:onKeyUp(event)
Sign In or Register to comment.