Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
KEY_UP and KEY_DOWN listeners — Gideros Forum

KEY_UP and KEY_DOWN listeners

MoKaLuxMoKaLux Member
edited November 2020 in General questions
I need your help please :)
I am a bit confused and I don't find the answer. I am trying to implement Event.KEY_DOWN and KEY_UP in a separate class but that doesn't work :|

1st class:
LevelX = Core.class(Sprite)
 
function LevelX:init()
	self.player = Player.new() -- calling 2nd class here
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
 
function LevelX:onEnterFrame(e)
end
2nd class:
Player = Core.class(Sprite)
 
function Player:init()
	self.isleft, self.isright, self.isup, self.isdown = false, false, false, false
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	self:addEventListener(Event.KEY_DOWN, self.onKeyDown, self)
end
 
function Player:onEnterFrame(e)
	print("xxx") -- that works OK
end
 
function Player:onKeyDown(e)
	print("yyy") -- that does not work NOT OK
	if e.keyCode == KeyCode.I then self.isup = true end -- that does not work NOT OK
end
What am I doing wrong please? Is there a typo somewhere? I am sure I had it working before :#
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • MoKaLuxMoKaLux Member
    edited November 2020
    EDIT: I found my error :)
    LevelX = Core.class(Sprite)
     
    function LevelX:init()
    	self.player = Player.new() -- calling 2nd class here
    	self:addChild(self.player) -- NOW EVERYTHING IS OK
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function LevelX:onEnterFrame(e)
    end
    We need to add it to stage for it to receive keyup and keydown events.

    It is a bit inconsistent I think because enterframe event triggers without adding sprite to stage!
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    Accepted Answer
    You need to add sprite to a scene. So
    stage:addChild(Player.new())
    will do the job :)

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited November 2020
    MoKaLux said:

    We need to add it to stage for it to receive keyup and keydown events.

    thank you antix rrraptor, we posted at the same time :smile:
    What do you think about the following?
    MoKaLux said:

    It is a bit inconsistent I think because enterframe event triggers without adding sprite to stage!

    :*
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited November 2020
    a bonus question:
    Why do we have to add two times a bitmap to a sprite?
    For example in my class player I do:
    local bitmap = Bitmap.new(Texture.new("img.png"))
    self:addChild(bitmap)
    And I need to do the same in my game class:
    local player = Player.new()
    self:addChild(player)
    Isn't it redundant to do two times addChild?

    Sorry I am back to the basics :)

    If I undestand this correctly the first addChild seems fair.
    The second addChild (in the game class) is because we created the object player but it's up to me to add it to the stage or not. So by default an instance of player is created but not added to stage.

    My opinion should be to add the instance automatically to the stage, my two cents :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLux said:

    It is a bit inconsistent I think because enterframe event triggers without adding sprite to stage!

    Idk for this one.
    MoKaLux said:

    a bonus question:
    Why do we have to add two times a bitmap to a sprite?
    For example in my class player I do:

    local bitmap = Bitmap.new(Texture.new("img.png"))
    self:addChild(bitmap)
    And I need to do the same in my game class:
    local player = Player.new()
    self:addChild(player)
    Isn't it redundant to do two times addChild?
    Because this is 2 different objects? First is an image, second is your player controller.
    If you want, you can move bitmap into player class, so it will look like:
    Player = Core.class(Sprite)
    function Player:init(texture)
    	self.btm = Bitmap.new(texture)
    	self:addChild(self.btm)
    end
    -- ...
     
    local player = Player.new("gfx/player.png")
    stage:addChild(player)
    MoKaLux said:

    My opinion should be to add the instance automatically to the stage, my two cents :)

    If I have a SceneManager then I want my sprites to appear on a scene, but not on stage, because stage is a global sprite object that contains all other sprites (including scene manager) and cant be removed (but can be hidden :D )
  • MoKaLuxMoKaLux Member
    edited November 2020
    rrraptor said:

    If you want, you can move bitmap into player class, so it will look like:...)

    That's what I do in my code.
    rrraptor said:

    If I have a SceneManager then I want my sprites to appear on a scene, but not on stage

    I meant ultimately the sprite is added to stage (sprite->scene->stage).

    So those remain:
    We need to add sprite to scene for it to receive keyup and keydown events.
    It is a bit inconsistent because enterframe event triggers without adding sprite to scene.

    Why do we have to add two times a bitmap to a sprite?
    My opinion should be to add the instance automatically to the scene, my two cents :smile:
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • keszeghkeszegh Member
    Accepted Answer
    @MoKaLux , i don't agree with you.
    the philosophy is that keys should behave like mouse events and for those it's quite natural that they are received only if the object is on the stage hierarchy.
    on the other hand enterframe events are quite natural to affect objects that are not on the stage (e.g. a hidden object that nevertheless needs to be updated).

    also i don't understand why you regard the bitmap as being added 'twice' to the stage, it is only added once to a parent and the parent is added to the stage. so only once it is assigned a parent (which is added to stage before or after, does not matter).
  • btw probably the best practice is to assign key events with stage itself.
  • thank you keszegh, yes now that totally makes sense :)
    keszegh said:

    btw probably the best practice is to assign key events with stage itself.

    We control the player so I wanted to add the controls to the player class.

    Thank you all for your insights.

    Viva gideros :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • keszeghkeszegh Member
    Accepted Answer
    yes, also i agree that key events are less obvious (compared to mouse events) whether should have the need to the object be added to stage, both solutions can be argued for, but the current one is as good as any.
  • keszeghkeszegh Member
    edited November 2020
    btw you can always add a dummy empty sprite to the stage 'belonging' to your player just to receive key etc. events.
  • antixantix Member
    Accepted Answer
    @MoKaLux I created a little class to manage key presses, maybe it would be of some use?

    https://github.com/Antix-Development/KeyManager

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited December 2020
    The problem I see with how gideros works is that I add the player to a sprite then the sprite gets added to the game class -> key event don't work :| = NOT COOL because enterframe event works :/
    	-- a player
    	self.player1 = Player.new(self.world)
    	self.tiled_level.camera:addChild(self.player1)
    PS: antix, sorry did not reply earlier but I would like to make the key events belong to my player class first.

    EDIT: see post below
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited December 2020
    sorry hgy29 but this still bugs me that enter frame event works and not key events :o
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • MoKaLuxMoKaLux Member
    edited December 2020
    btw you can always add a dummy empty sprite to the stage 'belonging' to your player just to receive key etc. events.
    Ok, I think I am going this way instead (the closest to what I would want so far):
    LevelX = Core.class(Sprite)
     
    function LevelX:init()
    	-- BG
    	application:setBackgroundColor(0x0)
    	...
    	-- PLAYER KEYS LISTENERS
    	self:addEventListener(Event.KEY_DOWN, self.player1.onKeyDown, self.player1)
    	self:addEventListener(Event.KEY_UP, self.player1.onKeyUp, self.player1)
    end
    Thank you all for your insights :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.