Jirka1111 Posted August 25, 2014 Share Posted August 25, 2014 I have this code. Hi purpose is to give me random position of spikes. Each spike is 50 px height. addRightSpikes: function(){ for(var i = 0; i < 6; i++){ var group = this.rightSideSpikes.create(Bird.GAME_WIDTH-30, this.spikesPositions(), "rightSideSpikes"); group.body.immovable = true; group.body.setSize(22, 17, 8, 17); } }, spikesPositions: function(){ var pos = [100, 160, 220, 280, 340, 400]; return pos[Math.round(Math.random() * pos.length-1)]; }, Sometimes it generates spike at the position GAME_WIDTH,0 and I don't know why. Link to comment Share on other sites More sharing options...
Jirka1111 Posted August 26, 2014 Author Share Posted August 26, 2014 Nobody knows? Link to comment Share on other sites More sharing options...
Chendler Posted August 26, 2014 Share Posted August 26, 2014 I have this code. Hi purpose is to give me random position of spikes. Each spike is 50 px height. addRightSpikes: function(){ for(var i = 0; i < 6; i++){ var group = this.rightSideSpikes.create(Bird.GAME_WIDTH-30, this.spikesPositions(), "rightSideSpikes"); group.body.immovable = true; group.body.setSize(22, 17, 8, 17); } }, spikesPositions: function(){ var pos = [100, 160, 220, 280, 340, 400]; return pos[Math.round(Math.random() * pos.length-1)]; }, Sometimes it generates spike at the position GAME_WIDTH,0 and I don't know why. Because your code:Math.round(Math.random() * pos.length-1)can return -1 and pos[-1] is undefined.// Returns a random number between min (inclusive) and max (exclusive)function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min;}https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random Jirka1111 1 Link to comment Share on other sites More sharing options...
Jirka1111 Posted August 26, 2014 Author Share Posted August 26, 2014 Thanks a lot! Link to comment Share on other sites More sharing options...
Jirka1111 Posted August 26, 2014 Author Share Posted August 26, 2014 Actually, I don't get it... randomPos: function(min, max){ return Math.random() * (max - min) + min; }, spikesPositions: function(){ var pos = [100, 150, 200, 250, 300, 350, 400]; return pos[this.randomPos(1, 6)]; } I've always had problems with this kind of stuff. Even at college Link to comment Share on other sites More sharing options...
Chendler Posted August 26, 2014 Share Posted August 26, 2014 Actually, I don't get it... randomPos: function(min, max){ return Math.random() * (max - min) + min; }, spikesPositions: function(){ var pos = [100, 150, 200, 250, 300, 350, 400]; return pos[this.randomPos(1, 6)]; } I've always had problems with this kind of stuff. Even at college LookrandomPos: function(min, max){ return Math.random() * (max - min) + min; }, spikesPositions: function(){ var pos = [100, 150, 200, 250, 300, 350, 400]; return pos[Math.round(this.randomPos(1, 6))]; }Because Math.random() * (max - min) + min;returns float. Link to comment Share on other sites More sharing options...
Jirka1111 Posted August 26, 2014 Author Share Posted August 26, 2014 Still nothing... Link to comment Share on other sites More sharing options...
Jirka1111 Posted August 26, 2014 Author Share Posted August 26, 2014 Math.round, I've got it. Thx. Link to comment Share on other sites More sharing options...
lewster32 Posted August 26, 2014 Share Posted August 26, 2014 Phaser does have some very handy utilities to make this easier; you could write the above as: spikesPositions: function() { return this.game.rnd.pick([100, 160, 220, 280, 340, 400]); // this could also be written as Phaser.Math.getRandom([100, 160, 220, 280, 340, 400]); // or the mathematical way: 100 + (Math.floor(Math.random() * 6) * 60); },Take a look at the Phaser.Math, Phaser.RandomDataGenerator and Phaser.Utils objects for some nice time-saving stuff. Jirka1111 1 Link to comment Share on other sites More sharing options...
Jirka1111 Posted August 26, 2014 Author Share Posted August 26, 2014 I think it is impossible that Phaser has pretty much everything what I need to create a simple game. So easy solution. Thank you again, lewster. lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts