fiverivers Posted June 23, 2014 Share Posted June 23, 2014 var game = new Phaser.Game(400, 490, Phaser.AUTO, 'gamediv', { preload: preload, create: create, update: update });function preload() { // Change the background color of the game game.stage.backgroundColor = '#71c5cf'; // Load the bird sprite game.load.image('bird', 'assets/bird.png'); // Load the pipe sprite game.load.image('pipe', 'assets/pipe.png'); };function create() { // Fuction called after 'preload' to setup the game game.physics.startSystem(Phaser.Physics.ARCADE); // Display the bird on the screen bird = game.add.sprite(100, 245, 'bird'); game.physics.enable(bird, Phaser.Physics.ARCADE) // Add gravity to the bird to make it fall bird.body.gravity.y = 1000; // Call the 'jump' function when the spacekey is hit var space_key = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); space_key.onDown.add(jump, this); // Create multiple pipes pipes = game.add.group(); pipes.createMultiple(20, 'pipe'); pipes.enableBody = true; game.physics.enable(pipes, Phaser.Physics.ARCADE) // Timer and adds rows of pipes timer = game.time.events.loop(1500, 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); bird.body.collideWorldBounds = true; score = 0; var style = { font: "30px Arial", fill: "#ffffff" }; label_score = game.add.text(20, 20, "0", style); game.physics.arcade.enable(bird);};function update() { if (leftKey.isDown) { bird.body.velocity.x = -350 } else if (rightKey.isDown) { bird.body.velocity.x = 350 } game.physics.arcade.collide(bird, pipes);};// Make the bird jump function jump() { // Add a vertical velocity to the bird bird.body.velocity.y = -350;};function add_one_pipe(x, y) { // Get the first dead pipe of our group var pipe = pipes.getFirstDead(); game.physics.enable(pipe, Phaser.Physics.ARCADE) // 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;};function add_row_of_pipes() { var hole = Math.floor(Math.random()*5)+1; for (var i = 0; i < 8; i++) if (i != hole && i != hole +1) add_one_pipe(i*60+10, 500); score += 1; label_score.content = score; };Why do I get this error? Link to comment Share on other sites More sharing options...
j0hnskot Posted June 23, 2014 Share Posted June 23, 2014 add pipe.checkWorldBounds=true;after the pipe.outOfBoundsKill=trueThey must come in pair to work. Else the pipes won't kill themselves..Since you never kill thoses pipes you will end with no pipes to use. Link to comment Share on other sites More sharing options...
fiverivers Posted June 23, 2014 Author Share Posted June 23, 2014 Hi there! That seemed to of worked but!!! It has only made it so that I can the error later on, before a maximum of 4 pipes would spawn before crashing but now it's much more, any ideas for a permanent solution? edit: I'm also getting a different error sometimes which says 'can't read 'reset' of null' or something along those lines... I'm assuming it's because when I do pipe = pipes.getFirstDead() there is no dead pipe so when I call pipe.reset it can't because it's null.. now the question is why there isn't any dead pipes :s edit 2: After some more testing it turns out after every 28th pipe I run out of dead pipes, just can't figure out why! Ok so I changed pipes.createMultiple(20, 'pipe'); I change this to 100 instead of 20 and now I can play until 100 pipes have spawned, surely this means the pipes aren't actually being killed when they go off screen? Link to comment Share on other sites More sharing options...
j0hnskot Posted June 23, 2014 Share Posted June 23, 2014 You get this error because you run out of pipes again. If you got no results when you call .getFirstDead(), the pipe will be null. Just check if the pipe variable is null. If it is, create more pipes to be able to continue or if you know the maximum amount of pipes you will have at any given time on your screen you can just create that many and never run out of pipes. In case you need the first solution, this might work if(pipe===null){ pipe=game.add.sprite(x,y,'pipe'); pipes.add(pipe);}add this just after "pipe=pipes.getFirstDead();" fiverivers 1 Link to comment Share on other sites More sharing options...
fiverivers Posted June 24, 2014 Author Share Posted June 24, 2014 Thank you very much Link to comment Share on other sites More sharing options...
Recommended Posts