Hello, I was just wondering how do you generate objects randomly on the X axis? I'm creating a game and their is a falling bombs and coins from the sky, but I don't know how to make the bombs and coins fall down randomly on the X axis. Please help me? And sorry, I'm actually new to this and only registered today :D
Down below is my code for 'MyWorld' Currently the coins and bombs are just falling at the same time and in one place. I'm not too sure if the bomb can be in the act code as the coin but I created a separate one for the bomb, because the bomb makes the protagonist lose health and the coins increases the score points.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { //background was taken from this website //background was edited to make the height higher /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 659 , 483 , 1 ); prepare(); // randomCoins(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Pirate pirate = new Pirate(); addObject(pirate, 306 , 411 ); pirate.setLocation( 286 , 411 ); pirate.setLocation( 268 , 414 ); } int iCounter = 0 ; public void act(){ iCounter++; if (iCounter== 90 ){ iCounter = 0 ; Coin myCoin = new Coin(); addObject(myCoin, 100 , 0 ); Bomb myBomb = new Bomb(); addObject(myBomb, 200 , 0 ); } } } |