AramCP Posted December 25, 2016 Share Posted December 25, 2016 Hi guys today i started making a game and i got a problem. I want to make a game where you move to the left and to the right and items are falling from the sky, the thing is i can make one item fall in a random position, thats easy: var randomx; randomx = Math.random()*256; dollars = game.add.sprite(randomx, -18, 'dollar'); Then i set a velocity and it starts to fall, the problem comes when i want to make fall more than one sprite. I thought about something like this: var randomy; randomy = Math.random()*300; And in update function: if(dollars.position.y = randomy){ game.add.sprite(randomx, -18, 'dollar'); } With that i wanted to make a dollar fall, and when that dollar reaches a random point in the Y cordinate, another dollar will start falling, but it doesnt work, could you help me please? Link to comment Share on other sites More sharing options...
The_dude8080 Posted December 25, 2016 Share Posted December 25, 2016 Could you give something more? Link to comment Share on other sites More sharing options...
AramCP Posted December 25, 2016 Author Share Posted December 25, 2016 17 minutes ago, The_dude8080 said: Could you give something more? ofc, here is the entire code: var game = new Phaser.Game(256, 392, Phaser.AUTO, 'game', { preload: preload, create: create, update: update }); var player; var cursors; var dollars; var randomx; var randomy; function preload() { game.load.image('background', 'assets/background.png'); game.load.spritesheet('minisprite', 'assets/spritesheet.png', 48, 72); game.load.image('dollar', 'assets/dollar.png'); game.load.image('scorebar', 'assets/score.png'); } function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.add.sprite(0, 0, 'background'); game.add.sprite(0, 352, 'scorebar'); randomx = Math.random()*256; randomy = Math.random()*300 player = game.add.sprite(100, 248, 'minisprite'); game.physics.arcade.enable(player); dollars = game.add.sprite(randomx, -18, 'dollar'); game.physics.arcade.enable(dollars); player.body.collideWorldBounds=true; cursors = game.input.keyboard.createCursorKeys(); player.frame = 3; player.animations.add('left', [0, 1, 2, 1], 6, true); player.animations.add('right', [6, 5, 4, 5], 6, true); } function update() { player.body.velocity.x = 0; dollars.body.velocity.y = 60; if (cursors.left.isDown){ player.body.velocity.x = -60; player.animations.play('left'); return; } if (cursors.right.isDown){ player.body.velocity.x = 60; player.animations.play('right'); return; } else { player.animations.stop(); player.frame = 3; } } Link to comment Share on other sites More sharing options...
The_dude8080 Posted December 25, 2016 Share Posted December 25, 2016 Hmm I see. I would do something like this: You should call this function in your create function. function DollarCheck () { var randomx = (...) var randomy = (...) var dollar = game.add.sprite(randomx,-18,"dollars"); if(dollar.position.y >= randomy) { dollar.kill(); // if you want to kill the dollar DollarCheck(); } } This way at the start of your game a dollar will be created (don't forget to apply gravity or velocity or whatever). When the dollar goes past the randomy the function runs again. If you want the randomy and the randomx to always be the same random just put make them global like you did AramCP 1 Link to comment Share on other sites More sharing options...
Recommended Posts