fiverivers Posted June 30, 2014 Share Posted June 30, 2014 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 Link to comment Share on other sites More sharing options...
wayfinder Posted June 30, 2014 Share Posted June 30, 2014 Try placing the positions in an array, then using Phaser.Math.shuffleArray on it, and reading the positions out sequentially. Link to comment Share on other sites More sharing options...
fiverivers Posted June 30, 2014 Author Share Posted June 30, 2014 Try placing the positions in an array, then using Phaser.Math.shuffleArray on it, and reading the positions out sequentially. Hi there! This is an excellent solution! Link to comment Share on other sites More sharing options...
Recommended Posts