Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101
Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101
Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745
Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":986,"date":"2017-07-21T15:59:42","date_gmt":"2017-07-21T15:59:42","guid":{"rendered":"https:\/\/phasergames.com\/?p=986"},"modified":"2017-07-21T16:02:30","modified_gmt":"2017-07-21T16:02:30","slug":"creating-endless-runner-game-phaser-part-4-movement","status":"publish","type":"post","link":"https:\/\/phasergames.com\/creating-endless-runner-game-phaser-part-4-movement\/","title":{"rendered":"Creating an Endless Runner Game in Phaser Part 4 Movement"},"content":{"rendered":"
Creating Movement<\/h3>\n
Although we will create the illusion of running, as you probably have guessed by now, we will actually be moving the blocks. The temptation is to just move the group as one in the update function.<\/p>\n
But if we did that we couldn’t take advantage of the built in physics that Phaser has to offer. For the arcade physics collision detection to work the blocks inside the group need to be enabled for physics and they need to have velocity set.<\/p>\n
So to do this I’ve modified the makeBlocks function by looping through the group and applying physics to each one. I’ve also increased the distance between blocks from 25 to 50, as the earlier value was a mistake, and adjusted the wallHeight\u00a0variable from 1-6, to 1-4.<\/p>\n
makeBlocks: function() {\r\n \/\/remove all the blocks from the group\r\n this.blocks.removeAll();\r\n var wallHeight = game.rnd.integerInRange(1, 4);\r\n for (var i = 0; i < wallHeight; i++) {\r\n var block = game.add.sprite(0, -i * 50, \"block\");\r\n this.blocks.add(block);\r\n }\r\n this.blocks.x = game.width - this.blocks.width\r\n this.blocks.y = this.ground.y - 50;\r\n \/\/\r\n \/\/Loop through each block\r\n \/\/and apply physics\r\n this.blocks.forEach(function(block) {\r\n \/\/enable physics\r\n game.physics.enable(block, Phaser.Physics.ARCADE);\r\n \/\/set the x velocity to -160\r\n block.body.velocity.x = -150;\r\n \/\/apply some gravity to the block\r\n \/\/not too much or the blocks will bounce\r\n \/\/against each other\r\n block.body.gravity.y = 4;\r\n \/\/set the bounce so the blocks\r\n \/\/will react to the runner\r\n block.body.bounce.set(1,1);\r\n });\r\n }<\/pre>\n
Now we need 3 things to happen<\/p>\n
\n
The blocks to not fall through the ground<\/li>\n
The blocks not to fall through each other<\/li>\n
The blocks to react when they hit the player<\/li>\n<\/ul>\n
So to accomplish that we do the same as we did to make sure the player stayed on the ground. We set up a collision for each one. Place this in the update function.<\/p>\n
\/\/\r\n \/\/collide the hero with the blocks\r\n \/\/\r\n game.physics.arcade.collide(this.hero, this.blocks);\r\n \/\/\r\n \/\/colide the blocks with the ground\r\n \/\/\r\n game.physics.arcade.collide(this.ground, this.blocks);\r\n \/\/\r\n \/\/when only specifying one group, all children in that\r\n \/\/group will collide with each other\r\n \/\/\r\n game.physics.arcade.collide(this.blocks);<\/pre>\nEndless Runner Part 4 Result<\/figcaption><\/figure>\n
Resetting the blocks<\/h3>\n
The last thing to do is to reset the blocks if\u00a0the player successfully\u00a0jumps the blocks. If we were moving the group we could just check the x position of the group, and check if that were less than zero. Since we are moving the blocks instead and the blocks’ x position is relative to the group, meaning they have an x position of 0 at the start and moves a negative rate as the game goes on. So for the blocks to be off the left of the screen they will have to be a negative value of the game.width (-game.width);<\/p>\n
Instead of checking every block, since all blocks should be on the game x position unless they get hit, we can just check the first child of the group.<\/p>\n
\/\/get the first child\r\nvar fchild = this.blocks.getChildAt(0);\r\n\/\/if off the screen reset the blocks\r\nif (fchild.x < -game.width) {\r\n this.makeBlocks();\r\n}<\/pre>\n
Here is the complete code in StateMain.js so far<\/p>\n
var StateMain = {\r\n preload: function() {\r\n game.load.image(\"ground\", \"images\/ground.png\");\r\n game.load.image(\"hero\", \"images\/hero.png\");\r\n game.load.image(\"bar\", \"images\/powerbar.png\");\r\n game.load.image(\"block\", \"images\/block.png\");\r\n },\r\n create: function() {\r\n this.power = 0;\r\n \/\/turn the background sky blue\r\n game.stage.backgroundColor = \"#00ffff\";\r\n \/\/add the ground\r\n this.ground = game.add.sprite(0, game.height * .9, \"ground\");\r\n \/\/add the hero in \r\n this.hero = game.add.sprite(game.width * .2, this.ground.y - 25, \"hero\");\r\n \/\/add the power bar just above the head of the hero\r\n this.powerBar = game.add.sprite(this.hero.x + 25, this.hero.y - 25, \"bar\");\r\n this.powerBar.width = 0;\r\n \/\/start the physics engine\r\n game.physics.startSystem(Phaser.Physics.ARCADE);\r\n \/\/enable the hero for physics\r\n game.physics.enable(this.hero, Phaser.Physics.ARCADE);\r\n game.physics.enable(this.ground, Phaser.Physics.ARCADE);\r\n \/\/game.physics.arcade.gravity.y = 100;\r\n this.hero.body.gravity.y = 200;\r\n this.hero.body.collideWorldBounds = true;\r\n \/\/this.hero.body.bounce.set(0, .2);\r\n this.ground.body.immovable = true;\r\n \/\/record the initial position\r\n this.startY = this.hero.y;\r\n \/\/set listeners\r\n game.input.onDown.add(this.mouseDown, this);\r\n this.blocks = game.add.group();\r\n this.makeBlocks();\r\n },\r\n mouseDown: function() {\r\n if (this.hero.y != this.startY) {\r\n return;\r\n }\r\n game.input.onDown.remove(this.mouseDown, this);\r\n this.timer = game.time.events.loop(Phaser.Timer.SECOND \/ 1000, this.increasePower, this);\r\n game.input.onUp.add(this.mouseUp, this);\r\n },\r\n mouseUp: function() {\r\n game.input.onUp.remove(this.mouseUp, this);\r\n this.doJump();\r\n game.time.events.remove(this.timer);\r\n this.power = 0;\r\n this.powerBar.width = 0;\r\n game.input.onDown.add(this.mouseDown, this);\r\n },\r\n increasePower: function() {\r\n this.power++;\r\n this.powerBar.width = this.power;\r\n if (this.power > 50) {\r\n this.power = 50;\r\n }\r\n },\r\n doJump: function() {\r\n this.hero.body.velocity.y = -this.power * 12;\r\n },\r\n makeBlocks: function() {\r\n this.blocks.removeAll();\r\n var wallHeight = game.rnd.integerInRange(1, 4);\r\n for (var i = 0; i < wallHeight; i++) {\r\n var block = game.add.sprite(0, -i * 50, \"block\");\r\n this.blocks.add(block);\r\n }\r\n this.blocks.x = game.width - this.blocks.width\r\n this.blocks.y = this.ground.y - 50;\r\n \/\/\r\n \/\/Loop through each block\r\n \/\/and apply physics\r\n this.blocks.forEach(function(block) {\r\n \/\/enable physics\r\n game.physics.enable(block, Phaser.Physics.ARCADE);\r\n \/\/set the x velocity to -160\r\n block.body.velocity.x = -150;\r\n \/\/apply some gravity to the block\r\n \/\/not too much or the blocks will bounce\r\n \/\/against each other\r\n block.body.gravity.y = 4;\r\n \/\/set the bounce so the blocks\r\n \/\/will react to the runner\r\n block.body.bounce.set(1, 1);\r\n });\r\n },\r\n update: function() {\r\n game.physics.arcade.collide(this.hero, this.ground);\r\n \/\/\r\n \/\/collide the hero with the blocks\r\n \/\/\r\n game.physics.arcade.collide(this.hero, this.blocks);\r\n \/\/\r\n \/\/colide the blocks with the ground\r\n \/\/\r\n game.physics.arcade.collide(this.ground, this.blocks);\r\n \/\/\r\n \/\/when only specifying one group, all children in that\r\n \/\/group will collide with each other\r\n \/\/\r\n game.physics.arcade.collide(this.blocks);\r\n \/\/\r\n \/\/get the first child\r\n var fchild = this.blocks.getChildAt(0);\r\n \/\/if off the screen reset the blocks\r\n if (fchild.x < -game.width) {\r\n this.makeBlocks();\r\n }\r\n }\r\n}<\/pre>\n