Hi, i'm trying to create a game based on H4ch1's Doodle Jump Clone (
http://giderosmobile.com/forum/discussion/473/let039s-jump-d#Item_10)
I've added my pause menu, i want to add "reset game" feature on my menu. I actually added this feature but FPS doubles when i reset (60 to 120)
I basically remove every child of level then remove (I'm not sure actually) level then i add it again like i do at the begining of the game.
My character (part of code which i believe is related):
require "box2d"
b2World = b2.World.new(0, 30, true);
function GCchar:m_dest()
self:destroyBody(self.body);
self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
b2World:removeEventListener(Event.BEGIN_CONTACT, self.onBeginCollision, self);
b2World:clearForces();
b2World=nil;
self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self);
self:removeEventListener(Event.MOUSE_UP, self.onMouseUp, self);
collectgarbage();
end
function GCchar:onEnterFrame()
if g_paused then return end
b2World:step(1/35, 8, 3);
... |
Level:
function GClevel:m_dest()
self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame);
char:m_dest();
char=0;
while self:getNumChildren()>0 do
self:removeChildAt(1);
end
self:getParent():removeChild(self);
collectgarbage();
end |
Replay Button
replay:addEventListener("click",
function()
level:getParent():removeChild(level);
level=GClevel.new();
stage:addChild(level);
g_paused=false;
end) |
What am i doing wrong? Thanks...
Comments
That top 2 lines of code, where is that appearing in your code? Is that in the GCchar:init function?
Also, just a note but you can do: level:removeFromParent()
I notice a discrepancy between the two self:removeEventListener commands. One has 2 arguments and the other has 3. Could this be it?
You need to show the code when you add the ENTER_FRAME Event listener.
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
not in init, just the top of the file. you can check it at http://giderosmobile.com/forum/discussion/473/let039s-jump-d#Item_10, ofcourse there are some differences but i'm building my game on that template...
@john26 i don't know how and i don't really know why i have different count of arguments, basically i remove the some way i add...
But as for the enter frame calling twice, my guess if your removeEventListener isn't passing in the same parameters that were used to add it, so maybe one isn't getting removed. For instance:
self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame);
I'm guessing the addEventListener passed in self as a data parameter, so it should probably be provided when removing similar to the other one.
Now, i copy the voices but i believe i'll easily fix it by stopping the music at destroy. But i have a second problem, the bodies of other stuff on screen are still there, they are moving and colliding with my player. it makes me think that listeners still work...
Likes: Janberka