Hey guys,
I'm stumped on this right now; Trying to reset my level but my update function continues and errors out some Bump stuff. So I figure I need to remove the event listener on reset, but it doesn't work. Here's some code:
function GameScene:addEventListeners()
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function GameScene:removeListeners(s)
print(s)
print(self)
print(GameScene)
s:removeEventListener(Event.ENTER_FRAME, s.onEnterFrame, s)
end |
In my NPC class's update loop (triggered from GameScene:onEnterFrame on a GAME_STARTED bool):
if LIVES > 1 then
GameScene:removeListeners(GameScene)
else
... |
I'm not great around the whole event listening concept to be honest.
But basically the new level loads an then swiftly crashes when GAME_STARTED is set to true and it will try to finish the previous round's update loop...
Comments
I am Sorry I am on mobile right now so it's hard to write it out...
I think that in your NPC function you need to call the gamescene with its instance name (not the class name).
if you're using scene manager it may be scenemanager.scene1
Edit: another option could be to ask the NPC instance to get its parent (which somewhere in the hierarchy of sprites is the scene) using self:getParent() - where self, if placed inside the NPC base class refers to the instance of NPC class which is executing the code.
Likes: Astirian
When you create your game scene you must be calling GameScene.new() so you would need to use that instance when removing listeners, like this..
Are you using SceneManager in your project?
Likes: Astirian
https://github.com/jdbcdev/Dots
I guess your problem is you need to understand class (use . as separator) and objects methods (use : as separator) in Object Oriented Programming. For instance:
GameScene.staticFunction1() is a class function. You have to call in this way:
Besides GameScene:removeListener() is a function of one instance of the GameScene. You must use
I'm instantiating scenes like this in main.lua:
The memory addresses (or pointers?) for the variables above are interesting (obviously they change on every run but you get the idea )
o = 0x115cf748
o2 = 0x115bc090
o3 = 0x1159f1d0
o4 = game
o5 = nil
self and GameScene are 0x115c52e8 (which I assume is the base class directly?)
I'm guessing that passing in 'game' is not correct?
Interestingly trying GAME = sceneManager.game is nil...
Let us know how you get on
Might I ask why every npc instance has it's own EnterFrame event?
Likes: Astirian
To answer your question, I don't have an onEnterFrame function in the NPC class, but my game scene's onEnterFrame calls the NPC's update function.
So basically, here we're saying to the NPC instance (on the update run where it tries to reset the round):
So I still don't quite get your code though. This line..
Likes: Astirian
In the main.lua the following code to create scenes using SceneManager:
Likes: Astirian
Likes: antix