Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
World step doubles after resetting level — Gideros Forum

World step doubles after resetting level

JanberkaJanberka Member
edited January 2013 in General questions
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

  • Hmm usually doubling FPS is a good thing ;)

    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()
  • john26john26 Maintainer
    You are creating two ENTER_FRAME events on the same sprite, the question is how..?

    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.

  • @zvardin :), well in my case more more than 60 is absolute horror :)

    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...
  • Hm... well what's curious then is you set b2World to nil when you destroy it, but then does the world ever get recreated?

    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.
  • Well.. The issue is about the listener as you said. I added 3rd parameter as self and now it doesn't copy the sprites. FPS turned to normal :)

    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...
  • zvardinzvardin Member
    Accepted Answer
    You need to loop through and destroy all the bodies. Your code gets rid of all the sprites but not the bodies in the box2d world. If your sprites contain an instance of the body, then you could change your code to be like this:
    while self:getNumChildren()>0 do
           --assuming you had a reference to the b2World bodies in the sprite
            b2World:destroyBody(self:getChildAt(1).body)
    	self:removeChildAt(1)
    end
    You could probably also just create a new box2d world.

    Likes: Janberka

    +1 -1 (+1 / -0 )Share on Facebook
  • I've fixed it thank you too much :)
Sign In or Register to comment.