fiverivers

Members
  • Content Count

    18
  • Joined

  • Last visited

  1. The ability to generate lines on the display and then interact with these. Right now if I make a line using the Phaser.Line method I can't interact with that line i.e. get a user to follow their finger across it.
  2. Thanks that makes sense however, s1 and s2 in the code you have demonstrated come out as undefined. Any ideas? EDIT: I GOT IT!! Thanks man!
  3. Thanks for the info however, I would really like to figure out why this doesn't work as i've invested so much time into it e: I'll check out that post it looks like the solution! Thanks friend.
  4. Hi This is my code var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'gamediv', { preload: preload, create: create, update: update });var sprite1;var sprite2;//yArray contains the 4 possible positions for a sprite//all must be used once and no more.var yArray = [50, 200, 350, 500];var y2Array = [50, 200, 350, 500];//var rand = reroll();ys1Array = Phaser.Math.shuffleArray(yArray);ys2Array = Phaser.Math.shuffleArray(y2Array);var leftSideSprites = [];var rightSideSprites = [];var leftSpriteArray = [];var rightSpriteArray = [];function preload() { game.load.image('teaspoon', 'teaspoon.png'); game.load.image('teacup', 'teacup.png'); game.load.image('shoes', 'shoes.png'); game.load.image('socks', 'sock.png'); game.load.image('television', 'television.png'); game.load.image('remote', 'remote.png'); game.load.image('hat', 'hat.png'); game.load.image('head', 'head.png');};function create() { leftSideSprites.push('teacup', 'shoes', 'television', 'head'); rightSideSprites.push('teaspoon', 'socks', 'remote', 'hat'); game.stage.backgroundColor = '#ffffff'; // Creating 8 sprites, randomly placing them and then assigning IDs to them. for (i = 0; i <= 3; i ++) { var spriteName = "sprite" + i; spriteName = game.add.sprite(50, ys1Array[i], leftSideSprites[i]); spriteName.id = i; game.physics.enable(spriteName, Phaser.Physics.ARCADE);// Enabling input of each sprite spriteName.inputEnabled = true; spriteName.input.enableDrag(); spriteName.events.onDragStart.add(startDrag, this); spriteName.events.onDragStop.add(stopDrag, this); leftSpriteArray.push(spriteName); } for (var i = 0; i <=3; i++) { var spriteName = "sprite" + i; spriteName = game.add.sprite(670, ys2Array[i], rightSideSprites[i]); spriteName.id = i; game.physics.enable(spriteName, Phaser.Physics.ARCADE); rightSpriteArray.push(spriteName); } console.log(leftSpriteArray[2]); console.log(rightSpriteArray[2].id);};function update() {};function overlapHandler (clickedSprite) { console.log("SUCCESS");}function startDrag(clickedSprite) { console.log('the id of the sprite I clicked = ' + clickedSprite.id); sprite1 = leftSpriteArray[clickedSprite.id]; sprite2 = rightSpriteArray[clickedSprite.id];}function stopDrag(clickedSprite) { game.physics.arcade.overlap(sprite1, sprite2, overlapHandler(clickedSprite) , null, this); }Basically the overlap handler is called any time I drag the sprite even though it doesn't collide with anything. Is there anyway for it to be called only when two specific sprites overlap.
  5. I'm writing a report on a game i've made in Phaser and need more information on Phaser itself. Does anyone know where I can get this information without sifting through loads of documentation? Information such as, when development for it started, who was involved in the development, its main features etc etc
  6. The green line is what I have achieved so far however, I want it to be a randomly generated curvy line. I then want a user to follow their finger across the line and I want to be able to dynamically change the width of the line so that I can make it more difficult/easier for the user. Does this make sense? I was thinking of just getting a few sprites which are just curvy lines and using them instead, the only problem with this though is how would I dynamically increase and decrease the width of the line? Furthermore, does anyone have any ideas as to how I can detect whether or not the line is being followed by the finger properly.
  7. Hi there, How do I go about generating a random curvy line between two sprites?
  8. Hello, var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'gamediv', { preload: preload, create: create, update: update });var sprite;//yArray contains the 4 possible positions for a sprite//all must be used once and no more.var yArray = [50, 200, 350, 500];var rand = reroll();function preload() { game.load.image('badman', 'badman.gif'); game.load.image('evilman', 'Evilman.gif');};function create() { // creating your 8 sprites with id from 0 to 7 for (i = 0; i <= 7; i += 1) { //get a random value reroll(); console.log(rand); if (i < 4) { //Add a sprite on the left side of the screen //and it's y axis should be one of the four possible values sprite = game.add.sprite(50, yArray[rand], 'badman'); //Assigning an id to the sprite sprite.id = i; //removing the value which was just used delete yArray[rand]; } else { // this else statement does the same but for the other side (right side) sprite = game.add.sprite(670, yArray[rand], 'evilman'); sprite.id = i; } // we need to enable input before we can access the input events sprite.inputEnabled = true; // add the callback to the input down event sprite.events.onInputDown.add(listener, this); } console.log(yArray); game.add.text(100, 32, 'Drag and drop the the first sprite on the left side onto the last sprite on the right side!', { font:'16px Arial', fill: '#ffffff' }); };function update() { };function listener(clickedSprite) { console.log('the id of the sprite I clicked = ' + clickedSprite.id);}Array.prototype.contains = function(k) { for(var i=0; i < this.length; i++){ if(this[i] === k){ return true; } } return false;}function reroll() { //gets a number number based on the size of array var rand = Math.floor(Math.random() * yArray.length); //checks if array value is undefined (aka - deleted) if (typeof yArray[rand] === "undefined") { reroll(); } return rand;} What I am trying to do is place 8 sprites on the screen like so - In order to do this I have set up an array with the values needed to place them in these positions. I am then trying to get a random value from that array so that the sprite is place in one of the four possible places. Next I try to remove the value from the array because it has just been used and I don't want sprites to overlap. I created a function called reroll which gets a random value from the array, but if the value was previously deleted (thus making it 'undefined') it calls itself again to get a different random value. I don't see why this isn't working? Thanks edit: added better comments
  9. Hi there, I want to make 8 sprites and assign an ID to each one and then whenever I click on a sprite I would like the ID to be returned so that I can check which one was clicked. Kind regards,
  10. 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 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?
  11. 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?
  12. 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?
  13. // 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...