So when my game starts out, there are already random balls on the screen. I want them to move around but I tried doing move(); and that wasn't working. I saw that that only works when you right click on the actor and make a new one.
/** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { CreateBalls(); } public void CreateBalls() { int [] num = new int [4]; num [0] = 5+Greenfoot.getRandomNumber(2); num [1] = 5+Greenfoot.getRandomNumber(2); num [2] = 5+Greenfoot.getRandomNumber(2); num [3] = 5+Greenfoot.getRandomNumber(2); for (int i=0; i<num.length; i++) { for (int n=0; n<num[i]; n++) { int randomX = Greenfoot.getRandomNumber(getWidth()-50)+25; int randomY = Greenfoot.getRandomNumber(getHeight()-50)+25; Ball0 ball = new Ball0(i); addObject(ball, randomX,randomY); } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball0 here. * * @author (your name) * @version (a version number or a date) */ public class Ball0 extends Actor { private static final String [] Colors = {"RED", "BLUE", "GREEN", "YELLOW"}; private int value; public Ball0(int id) { value = id; setImage(new GreenfootImage(Colors[value] +"-ball.png")); } public String getColor() { return Colors[value]; } }