Bossman759 Posted April 1, 2014 Share Posted April 1, 2014 How would I go about detecting collosion of a player and a block(image)? The player currently collides with the block but I want to know how I could restart the game after this happens. Link to comment Share on other sites More sharing options...
drhayes Posted April 2, 2014 Share Posted April 2, 2014 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 More sharing options...
Bossman759 Posted April 2, 2014 Author Share Posted April 2, 2014 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. Link to comment Share on other sites More sharing options...
Martiny Posted April 2, 2014 Share Posted April 2, 2014 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 More sharing options...
Bossman759 Posted April 2, 2014 Author Share Posted April 2, 2014 My bad, I misunderstood lol. I actually am using a sprite. and thanks for the link. I'll try that. Link to comment Share on other sites More sharing options...
Bossman759 Posted April 3, 2014 Author Share Posted April 3, 2014 I tried the link that Martiny provided and it worked fine, but is there anyway to detect collision with the game border? Link to comment Share on other sites More sharing options...
TomFiveThumbs Posted April 4, 2014 Share Posted April 4, 2014 I think if you set this, where 'player' is your spritegame.world.setBounds(0, 0, game.width, game.height);player.body.collideWorldBounds = true;he will hit the edges of the world and stop there, not being able to fall through the world boundary Link to comment Share on other sites More sharing options...
adamyall Posted April 4, 2014 Share Posted April 4, 2014 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 More sharing options...
valueerror Posted April 4, 2014 Share Posted April 4, 2014 there are sprite events onOutOfBoundsonEnterBounds 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 adamyall 1 Link to comment Share on other sites More sharing options...
gikdew Posted April 9, 2014 Share Posted April 9, 2014 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 More sharing options...
Recommended Posts