fiverivers Posted June 22, 2014 Share Posted June 22, 2014 // Initialize Phaser, and creates a 400x490px gamevar game = new Phaser.Game(400, 490, Phaser.AUTO, 'game_div');// Creates a new 'main' state that wil contain the gamevar main_state = { preload: function() { // Function called first to load all the assets // Change the background color of the game this.game.stage.backgroundColor = '#71c5cf'; // Load the bird sprite this.game.load.image('bird', 'assets/bird.png'); // Load the pipe sprite this.game.load.image('pipe', 'assets/pipe.png'); }, create: function() { game.physics.startSystem(Phaser.Physics.ARCADE); // Fuction called after 'preload' to setup the game // Display the bird on the screen this.bird = this.game.add.sprite(100, 245, 'bird'); // Add gravity to the bird to make it fall this.bird.body.gravity.y = 1000; // Call the 'jump' function when the spacekey is hit var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); space_key.onDown.add(this.jump, this); // Create multiple pipes this.pipes = game.add.group(); this.pipes.createMultiple(20, 'pipe'); this.pipes.enableBody = true; // Timer and adds rows of pipes this.timer = this.game.time.events.loop(1500, this.add_row_of_pipes, this); // Adding key values. upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP); downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN); leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT); rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT); this.bird.body.collideWorldBounds = true; this.score = 0; var style = { font: "30px Arial", fill: "#ffffff" }; this.label_score = this.game.add.text(20, 20, "0", style); game.physics.arcade.enable(this.bird); }, update: function() { // Function called 60 times per second // If the bird is out of the world (too high or too low), call the 'restart_game' function// if (this.bird.inWorld == false)// this.restart_game(); if (leftKey.isDown) { this.bird.body.velocity.x = -350 } else if (rightKey.isDown) { this.bird.body.velocity.x = 350 } game.physics.arcade.collide(this.bird, this.pipes); }, // Make the bird jump jump: function() { // Add a vertical velocity to the bird this.bird.body.velocity.y = -350;}, // Restart the game restart_game: function() { // Start the 'main' state, which restarts the game this.game.state.start('main'); this.game.time.events.remove(this.timer);}, add_one_pipe: function(x, y) { // Get the first dead pipe of our group var pipe = this.pipes.getFirstDead(); // Set the new position of the pipe pipe.reset(x, y); // Add velocity to the pipe to make it move left pipe.body.velocity.y = -200; // Kill the pipe when it's no longer visible pipe.outOfBoundsKill = true; }, add_row_of_pipes: function() { var hole = Math.floor(Math.random()*5)+1;// for (var i = 0; i < 8; i++)// if (i != hole && i != hole +1) // this.add_one_pipe(400, i*60+10); for (var i = 0; i < 8; i++) if (i != hole && i != hole +1) this.add_one_pipe(i*60+10, 500); this.score += 1; this.label_score.content = this.score; },};// Add and start the 'main' state to start the gamegame.state.add('main', main_state); game.state.start('main'); I can't figure out for the life of me why this won't work I get an error saying gravity is null... Link to comment Share on other sites More sharing options...
ragnarok Posted June 22, 2014 Share Posted June 22, 2014 Try to add "this.game.physics.enable(bird, Phaser.Physics.ARCADE)" before modifying body-variables. I think bird has no body before this, so using "this.bird.body.gravity" is doomed to fail. Link to comment Share on other sites More sharing options...
fiverivers Posted June 23, 2014 Author Share Posted June 23, 2014 Try to add "this.game.physics.enable(bird, Phaser.Physics.ARCADE)" before modifying body-variables. I think bird has no body before this, so using "this.bird.body.gravity" is doomed to fail. Thanks for that! Through adding that code to the bird and pipes both have collision however..... after 3 pipes spawn the entire code crashes and I get an error saying cannot read property of 'reset' null.add_one_pipe: function(x, y) { // Get the first dead pipe of our group var pipe = this.pipes.getFirstDead(); // Set the new position of the pipe pipe.reset(x, y); // Add velocity to the pipe to make it move left pipe.body.velocity.y = -200; // Kill the pipe when it's no longer visible pipe.outOfBoundsKill = true; },In this function the x and y parameters are null for some reason, any ideas? Link to comment Share on other sites More sharing options...
Recommended Posts