Restart world button [SOLVED]


(Eva Droch) #1

How i can make restart button with mouse click ?

package {
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.FP;
    

    public class MyRestart extends Entity {
        [Embed(source='assets/wall.png')] 
        private const RESTART:Class; 
        
        public function MyRestart(posX:int, posY:int){ 
            graphic = new Image(RESTART); 
            setHitbox(46, 46);
            type = "restart"; 
            x = posX * 46; 
            y = posY * 46;
            
        }
        override public function update():void{
            if (collide(/* there needs verification mouse x,y*/"", x, y) && /* there needs verification mouse click */){
                FP.world.removeAll();
                FP.world = new MainMenu;
                
            }
        }
    }
}

(Eva Droch) #2

I test with code

package {
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.FP;
    

    public class MyRestart extends Entity {
        [Embed(source='assets/wall.png')] 
        private const RESTART:Class; 
        
        public function MyRestart(posX:int, posY:int){ 
            graphic = new Image(RESTART); 
            setHitbox(46, 46);
            type = "restart"; 
            x = posX * 46; 
            y = posY * 46;
            
        }
        override public function update():void{
            if (collide("player", x, y)){
                FP.world.removeAll();
                FP.world = new MainMenu;
                
            }
        }
    }
}

After remove all entity, memory increases 7 times and is reset to its initial value, its normal ?


(Justin Wolf) #3

What you’re looking for is collidePoint().

if (Input.mousePressed && collidePoint(x, y, Input.mouseX, Input.mouseY))
{
     // restart world here
}

(Martí Angelats i Ribera) #4

AS3 works using garbage collector. This means that when you are deleting something you are not actually doing it. Instead it is waiting to be deleted. That’s why using recycle improves the code.

I’d recomend to avoid deleting the world and trying to set everything to the default with a function.

You’d have something like

if (Input.mousePressed && collidePoint(x, y, Input.mouseX, Input.mouseY))
{
    (FP.world as MainMenu).restart();
}

PS: yep, @justinwolf posted the collide detection faster.


(Martí Angelats i Ribera) #5

By the way, why do you need to reset a world? It’s not an usual thing to do.


(Eva Droch) #8

Guys thank you for your attention and answers. :relaxed:


(Jacob Albano) #9

If you want a button that will work with any world type, try this to automatically recreate the current world:

var type:Class = Object(FP.world).constructor;
FP.world = new type();