Search the Community
Showing results for tags 'phaser physics sprites'.
-
HI im trying to create a game where you run and avoid obstacles. I'm still in the early stages of the code and a problem occoured. I loop a the platform to make the ground fit with the world width, when i reach the end of the platform the character gets stuck like it's an edge stopping it from getting further (I can jump over it). Any help is appreciated, thank you. Here's the code. <!doctype html><html> <head> <meta charset="UTF-8" /> <title>testGame</title> <!-- If you're wondering why we embed each script separately, and not just the single-file phaser lib it's because it makes debugging *significantly* easier for us. Feel free to replace all the below with just a call to ../dist/phaser.js instead if you prefer. --> <script src="phaser.min.js"></script> <script type="text/javascript"> window.onload = function() { var game = new Phaser.Game(1920, 1080, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.spritesheet('dude', 'dude.png', 32, 48); game.load.image('background', 'background.png'); game.load.image('ground', 'platform.png'); game.load.image('mushroom', 'obstacle.png'); } var platforms; var player; var facing = 'left'; var jumpTimer = 0; var cursors; var jumpButton; var bg; var ground; var worldWidth = 4000; var worldHeight = 1080; var groundWidth = 400; function create() { game.stage.backgroundColor = '#000000'; game.world.setBounds(0, 0, worldWidth, worldHeight); bg = game.add.sprite(0, 0, 'background'); bg.fixedToCamera = true; // The platforms group contains the ground and the 2 ledges we can jump on platforms = game.add.group(); // Here we create the ground. //ground = platforms.create(0, game.world.height - 128, 'ground'); //ground.width = groundWidth; for(var i = 0; i < worldWidth; i+=groundWidth){ ground = platforms.create(i, game.world.height - 128, 'ground'); ground.body.immovable = true; ground.body.collideWorldBounds=true; } for (var i = 0; i < 150; i++) { game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom'); } //ground.scale.setTo(2, 2); // This stops it from falling away when you jump on it player = game.add.sprite(32, 32, 'dude'); player.body.bounce.y = 0.2; player.body.collideWorldBounds = true; player.body.gravity.y = 6; player.body.setSize(16, 32, 8, 16); player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('turn', [4], 20, true); player.animations.add('right', [5, 6, 7, 8], 10, true); game.camera.follow(player); cursors = game.input.keyboard.createCursorKeys(); jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); } function update() { game.physics.collide(player, platforms); player.body.velocity.x = 0; ground.position.x -= 5; if (cursors.left.isDown) { player.body.velocity.x = -150; if (facing != 'left') { player.animations.play('left'); facing = 'left'; } } else if (cursors.right.isDown) { player.body.velocity.x = 150; if (facing != 'right') { player.animations.play('right'); facing = 'right'; } } else { if (facing != 'idle') { player.animations.stop(); if (facing == 'left') { player.frame = 0; } else { player.frame = 5; } facing = 'idle'; } } if (jumpButton.isDown && player.body.touching.down && game.time.now > jumpTimer) { player.body.velocity.y = -250; jumpTimer = game.time.now + 750; } // player.scale.x += 0.001; // player.scale.y += 0.001; } function render () { game.debug.renderSpriteBody(player); } }; </script> </head> <body> </body></html>