Sign in to follow this  
Bossman759

How to detect collision and restart the game?

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);

Share this post


Link to post
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 ):

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.