spinnerbox Posted March 11, 2015 Share Posted March 11, 2015 Here is my code:window.document.addEventListener("DOMContentLoaded", function (event) {'use strict'; var game = new Phaser.Game(800, 600, Phaser.AUTO, ''), arcade = null, stateManager = null, physicsManager = null, state1Object = new Phaser.State(), player = null, platforms = null, ledge = null, ground = null, cursors = null, keyboard = null; state1Object.preload = function (gameObject) { gameObject.load.image('logo', 'assets/phaser.png'); gameObject.load.image('sky', 'assets/sky.png'); gameObject.load.image('ground', 'assets/platform.png'); gameObject.load.image('star', 'assets/star.png'); gameObject.load.spritesheet('dude', 'assets/dude.png', 32, 48); }; state1Object.create = function (gameObject) { //var logo = game.add.sprite(game.world.centerX, game.world.centerY, 'logo'); //logo.anchor.setTo(0.5, 0.5); //game.add.sprite(0, 0, 'star'); // We're going to be using physics, so enable the Arcade Physics system //game.physics.startSystem(Phaser.Physics.ARCADE); arcade = new Phaser.Physics.Arcade(gameObject); // A simple background for our game gameObject.add.sprite(0, 0, 'sky'); // The platforms group contains the ground and the 2 ledges we can jump on platforms = gameObject.add.group(); // We will enable physics for any object that is created in this group platforms.enableBody = true; platforms.physicsBodyType = Phaser.Physics.ARCADE; // Here we create the ground. ground = platforms.create(0, gameObject.world.height - 64, 'ground'); // Scale it to fit the width of the game (the original sprite is 400x32 in size) ground.scale.setTo(2, 2); // This stops it from falling away when you jump on it ground.body.immovable = true; // Now let's create two ledges ledge = platforms.create(400, 400, 'ground'); ledge.body.immovable = true; ledge = platforms.create(-150, 250, 'ground'); ledge.body.immovable = true; // The player and its settings player = gameObject.add.sprite(32, gameObject.world.height - 150, 'dude'); // We need to enable physics on the player // arcade.enable(player); // Player physics properties. Give the little guy a slight bounce. player.enableBody = true; player.physicsBodyType = Phaser.Physics.ARCADE; player.body.bounce.y = 0.2; player.body.gravity.y = 300; player.body.collideWorldBounds = true; // Our two animations, walking left and right. player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); player.body.gravity.y = 300; keyboard = new Phaser.Keyboard(gameObject); keyboard.addKey(Phaser.Keyboard.LEFT); console.log(keyboard); cursors = keyboard.createCursorKeys(); //controlPlayer(cursors, player); }; state1Object.update = function (gameObject) { // Collide the player and the stars with the platforms arcade.collide(player, ground); player.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the left player.body.velocity.x = -150; player.animations.play('left'); } else if (cursors.right.isDown) { // Move to the right player.body.velocity.x = 150; player.animations.play('right'); } else { // Stand still player.animations.stop(); player.frame = 4; } // Allow the player to jump if they are touching the ground. if (cursors.up.isDown && player.body.touching.down) { player.body.velocity.y = -350; } }; state1Object.render = function (gameObject) { }; // create state manager stateManager = new Phaser.StateManager(game); stateManager.add("state1", state1Object, false); stateManager.start(state1Object); // create physics manager //physicsManager = new Phaser.Physics.Arcade(game); game.state = stateManager; //game.physics = physicsManager; });I am fighting with this stuff. What do I do wrong? I cannot get my arrow keys to work. Link to comment Share on other sites More sharing options...
CtlAltDel Posted March 11, 2015 Share Posted March 11, 2015 You should not have to create a statemanager. Just add your state to the game. Link to comment Share on other sites More sharing options...
spinnerbox Posted March 11, 2015 Author Share Posted March 11, 2015 I followed the tutorial and reworked my code: window.document.addEventListener("DOMContentLoaded", function (event) {'use strict'; var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }), platforms = null, arcade = null, player = null, cursors = null, stars = null; function preload() { game.load.image('sky', 'assets/sky.png'); game.load.image('ground', 'assets/platform.png'); game.load.image('star', 'assets/star.png'); game.load.spritesheet('dude', 'assets/dude.png', 32, 48); } function create() { var ground = null, ledge = null, star = null, i = 0; // We're going to be using physics, so enable the Arcade Physics system arcade = new Phaser.Physics.Arcade(game); // A simple background for our game game.add.sprite(0, 0, 'sky'); // The platforms group contains the ground and the 2 ledges we can jump on platforms = game.add.group(); // We will enable physics for any object that is created in this group platforms.enableBody = true; platforms.physicsBodyType = Phaser.Physics.ARCADE; // Here we create the ground. ground = platforms.create(0, game.world.height - 64, 'ground'); // Scale it to fit the width of the game (the original sprite is 400x32 in size) ground.scale.setTo(2, 2); // This stops it from falling away when you jump on it ground.body.immovable = true; // Now let's create two ledges ledge = platforms.create(400, 400, 'ground'); ledge.body.immovable = true; ledge = platforms.create(-150, 250, 'ground'); ledge.body.immovable = true; // The player and its settings player = game.add.sprite(32, game.world.height - 150, 'dude'); // We need to enable physics on the player player.enableBody = true; player.physicsBodyType = Phaser.Physics.ARCADE; // Player physics properties. Give the little guy a slight bounce. player.body.bounce.y = 0.2; player.body.gravity.y = 300; player.body.collideWorldBounds = true; // Our two animations, walking left and right. player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); stars = game.add.group(); stars.enableBody = true; stars.physicsBodyType = Phaser.Physics.ARCADE; // Here we'll create 12 of them evenly spaced apart for (i = 0; i < 12; i += 1) { // Create a star inside of the 'stars' group star = stars.create(i * 70, 0, 'star'); // Let gravity do its thing star.body.gravity.y = 6; // This just gives each star a slightly random bounce value star.body.bounce.y = 0.7 + Math.random() * 0.2; } cursors = game.input.keyboard.createCursorKeys(); } function update() { arcade.collide(player, platforms); arcade.collide(stars, platforms); arcade.overlap(player, stars, collectStar, null, this); // Reset the players velocity (movement) player.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the left player.body.velocity.x = -150; player.animations.play('left'); } else if (cursors.right.isDown) { // Move to the right player.body.velocity.x = 150; player.animations.play('right'); } else { // Stand still player.animations.stop(); player.frame = 4; } // Allow the player to jump if they are touching the ground. if (cursors.up.isDown && player.body.touching.down) { player.body.velocity.y = -350; } } function collectStar (player, star) { // Removes the star from the screen star.kill(); } });Now the keyboard works but the stars do not bounce of the platforms and my alien also doesn't stay on the platforms. It falls to the bottom edge. What do I do wrong? Link to comment Share on other sites More sharing options...
spinnerbox Posted March 12, 2015 Author Share Posted March 12, 2015 Let me guess, there is nothing wrong with my code? Link to comment Share on other sites More sharing options...
spinnerbox Posted March 12, 2015 Author Share Posted March 12, 2015 Oh I am so sorry. Just found out I am using Phaser version 1.3.3 Link to comment Share on other sites More sharing options...
Recommended Posts