crffty Posted December 14, 2016 Share Posted December 14, 2016 hello. I'm trying to stop my endless runner upon collision between the player and an 'obstacle' to do this i would like to stop the sprits form moving. velocity = 0. I can't work out how to do this though. thoughts? help? advice? beer? vaultage.game = function() {}; vaultage.game.prototype = { create : function() { // physics engine this.game.physics.startSystem(Phaser.Physics.ARCADE); this.game.physics.arcade.gravity.y = 1000; // background this.background = this.game.add.tileSprite(0, 0, this.game.width, 360, 'background'); this.background.autoScroll(-100, 0); // ground this.ground = this.game.add.tileSprite(0, 290, this.game.width, 8, 'ground'); this.ground.autoScroll(-400, 0); // player this.player = this.add.sprite(45, 200, 'player'); this.player.animations.add('run'); this.player.animations.play('run', 15, true); // obstacles this.obstacles = this.game.add.group(); this.obstacles.enableBody = true; // physics on sprites this.game.physics.arcade.enable([this.player, this.ground]); this.ground.body.immovable = true; this.ground.body.allowGravity = false; this.player.body.collideWorldBounds = true; // run the functions to create obstacles this.createObstacles(); this.nextObstacle(); }, update : function() { // look for collisions between sprites this.game.physics.arcade.collide(this.player, this.ground); if (this.game.physics.arcade.collide(this.player, this.obstacles)) { this.endGame(); }; // run the updateObstacle function for each obstacle this.obstacles.forEachAlive(this.updateObstacle, this); }, shutdown : function() { }, createObstacles: function() { // 1 x 5 keys == 5 obstacles this.obstacles.createMultiple(1, 'obstacle', [0, 1, 2, 3, 4]); this.obstacles.setAll('body.allowGravity', false); this.obstacles.setAll('body.immovable', true); }, nextObstacle: function() { // timer on next obstacle spawn this.resetNextObstacle(); this.time.events.add(this.rnd.between(750, 2000), this.nextObstacle, this); }, resetNextObstacle: function() { // reset each obstical to be re-used var obs = this.obstacles.getFirstDead(); // location of re-set dead obsticals if (obs) { obs.reset(); obs.left = this.world.bounds.right; obs.bottom = this.ground.top; obs.body.velocity.x = -400; } else { console.warn("None available", this.obstacles.children); } }, updateObstacle: function(obs) { // kill obstacle after it moves out of bounds if (obs.right < this.world.bounds.left) { obs.kill(); } }, endGame: function() { this.ground.autoScroll(0, 0); this.background.autoScroll(0, 0); this.obs.body.velocity.x = 0; } } thank you for your time and patients I'm still learning and its all a bit over my head sometimes. any advice would be greatly appreciated. Link to comment Share on other sites More sharing options...
erich Posted December 14, 2016 Share Posted December 14, 2016 why not when the collison happens, pause the game so everything stops, then you could restart the game state when needed after a few seconds with a timer http://examples.phaser.io/_site/view_full.html?d=misc&f=pause+menu.js&t=pause menu Eric Link to comment Share on other sites More sharing options...
crffty Posted December 14, 2016 Author Share Posted December 14, 2016 17 minutes ago, erich said: why not when the collision happens, pause the game so everything stops, then you could restart the game state when needed after a few seconds with a timer http://examples.phaser.io/_site/view_full.html?d=misc&f=pause+menu.js&t=pause menu Eric thanks for you reply. the idea is to have the game end on collision. so in order after the collision - player sprite to change to a "death" sprite, 2 seconds later - a game over/score screen with a play again button and a submit score button ( submit score will show a form - i'll me making a hight score table with a bit of php and SQL. ). the rest should be achievable but can the player sprite be changed whilst the game is pause? thanks for your advice. Link to comment Share on other sites More sharing options...
mickeyren Posted December 14, 2016 Share Posted December 14, 2016 If you multiply the velocity by a fractional number - it will slowly reduce to 0. Would that help? Link to comment Share on other sites More sharing options...
samme Posted December 14, 2016 Share Posted December 14, 2016 You can do obs.body.velocity.set(0); // or this.obstacles.setAll('body.velocity.x', 0); this.obstacles.setAll('body.velocity.y', 0); erich 1 Link to comment Share on other sites More sharing options...
erich Posted December 14, 2016 Share Posted December 14, 2016 22 minutes ago, samme said: You can do obs.body.velocity.set(0); // or this.obstacles.setAll('body.velocity.x', 0); this.obstacles.setAll('body.velocity.y', 0); thats brilliant - I was using the game.pause method beforehand, I will use this in future for my games ! Link to comment Share on other sites More sharing options...
crffty Posted January 4, 2017 Author Share Posted January 4, 2017 thank you for your help. I have the obstacles, ground, background and player stopped but new obstacles are still being generated and moving across the screen. I also need to stop the timer without loosing to current count. and was thinking about how to get that number into a database update : function() { // time tracker var thisTime = new Date(); var diff = (thisTime.getTime() - this.startingTime.getTime())/1000; this.score.text = diff; }, endGame: function() { this.endGame = true; // stop everything this.obstacles.setAll('body.velocity.x', 0); this.ground.autoScroll(0, 0); this.background.autoScroll(0, 0); this.player.animations.stop(null, true); // timer stop // stop new obstacles from being created ??? <<<<<<<<< // game over text this.game.add.text(this.game.world.centerX - 150, this.game.world.centerY - 100, "game over", { font: "60px Raleway"} ); // place the buttons this.rePlay = game.add.sprite(this.game.world.centerX - 150, this.game.world.centerY + 100, 'rePlay'); this.rePlay.anchor.setTo(0.5, 0.5); this.subScore = game.add.sprite(this.game.world.centerX + 150, this.game.world.centerY + 100, 'subScore'); this.subScore.anchor.setTo(0.5, 0.5); // Enable input on the replay button this.rePlay.inputEnabled = true; // Attach a function to the input down ( click/tap) this.rePlay.events.onInputDown.add(function() { this.game.state.start('game'); }, this); } Link to comment Share on other sites More sharing options...
Tom Atom Posted January 4, 2017 Share Posted January 4, 2017 Hi, I made several endless runners in past and I do not think it is good idea to move all obstacles to simulate scrolling. It is better to keep all your environment static and move only player. You can follow him with camera and remove obstacles, that are out of camera view on left. On the other side, you can generate new obstacles a little to right bound of camera. With this, when you want to stop, you need to stop only player and do not have to stop anything else. You can also simply play with player's movement velocity, etc. Btw, some time ago I wrote whole book about it (https://gumroad.com/l/CZuhn). Link to comment Share on other sites More sharing options...
Recommended Posts