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
Comments
It is a bit inconsistent I think because enterframe event triggers without adding sprite to stage!
Likes: MoKaLux
antixrrraptor, we posted at the same timeWhat do you think about the following?
Why do we have to add two times a bitmap to a sprite?
For example in my class player I do:
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
If you want, you can move bitmap into player class, so it will look like:
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
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).
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Thank you all for your insights.
Viva gideros
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
https://github.com/Antix-Development/KeyManager
Likes: MoKaLux
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 worksEDIT: see post below
Likes: MoKaLux