Jump to content

How to detect collision and restart the game?


Bossman759
 Share

Recommended Posts

Are you using an actual Image instead of a Sprite? If you use a Sprite then you can attach a physics system to both sprites and check collision that way.

 

And it depends what you mean by restart the game: if you want to restart the current state I think you can do this:

game.state.start(game.state.current);
Link to comment
Share on other sites

Yes I'm using an actual image instead of a sprite. The part I don't understand is how to detect collision and perform a function when this happens. Your game restart code works fine btw.

 

I'm not 100% sure if I understand by actual image, but wouldn't it be easier to put a sprite in your game and then doing this example: http://examples.phaser.io/_site/view_full.html?d=arcade%20physics&f=sprite+vs+sprite.js&t=sprite%20vs%20sprite ?

 

Instead of the collisionHandler, you can put your own function that restarts the state.

 

If you can't do it with a sprite, then I can't help ):

Link to comment
Share on other sites

I looked through the documentation, and I'm not sure if/how you can perform code on hitting the bounds.
What you could do however, is create a new group with 4 invisible sprites that wrap around your screen boundaries. Then you'd turn off collideWorldBounds and check for collisions with that group.

Link to comment
Share on other sites

there are sprite events    

onOutOfBounds

onEnterBounds
 
you can add  to do this..  i'm not exactly sure how..  i'd try    mysprite.events.onOutOfBounds.add(myfunction, this);  or something similar..
 
 
otherwise do it manually -  just check if sprite.body.x < 0 ||  sprite.body.x > game.world.width    (and the same for Y) in the update loop and then fire my custom function
Link to comment
Share on other sites

When a game state starts, it runs the create function of that state, but all the variables that where setup don't change. What I do is call on create a function call restart(); where I initialize the values of all my variables.

 

create: function() {        this.ll = 0;        this.canShoot = true;        this.restart();}
restart: function() {        this.gfunc;        this.player;        this.enemie;        this.powerUp;        this.cursors;        this.circle;        this.d = 270;        this.p1;        this.p2;        this.p3;        this.ORBIT_RADIUS = 60;        this.enemies = new Array();        this.counter = 0;        this.counter1 = 0;        this.counter2 = 0;        this.eToDelete;        this.level1 = [            [18, 19, 0, 1, 2], 101, 102        ];        this.ll = 0;}

So that works for me, I also stop and start the timers of my game, in order to not get errors.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...