This site requires JavaScript, please enable it in your browser!
Greenfoot back
Sammyy2323
Sammyy2323 wrote ...

2020/6/3

Help moving an image that is already generated in the world

Sammyy2323 Sammyy2323

2020/6/3

#
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.
danpost danpost

2020/6/3

#
Sammyy2323 wrote...
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.
Where did you try 'move();'. Show codes. Did you try 'move(1);'?
Sammyy2323 Sammyy2323

2020/6/3

#
i did move (5); in the ball class Im not sure if Im supposed to add the move in the world but here is myworld code
/**
 * 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);
                
            }
            
        }
    }
   
}
but that didn't do anything.
danpost danpost

2020/6/3

#
Sammyy2323 wrote...
i did move (5); in the ball class << Code Omitted >> but that didn't do anything.
Show the Ball0 class.
Sammyy2323 Sammyy2323

2020/6/3

#
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];
    }
    
    
    
}
danpost danpost

2020/6/3

#
No code to make the ball perform its act?
Sammyy2323 Sammyy2323

2020/6/3

#
i made an act method and put move(5); in but it didn't move(put the act in the Ball0 class
danpost danpost

2020/6/4

#
Sammyy2323 wrote...
i made an act method and put move(5); in but it didn't move(put the act in the Ball0 class
Well, that IS quite strange. I presume you are clicking the "Run" button and its text becomes "Pause"? Otherwise, I do not get it.
Sammyy2323 Sammyy2323

2020/6/4

#
yea i want the balls to move when I press "run" but they are not because I think they are preseted into the world. I didn't right-click on the balls and add the balls into the world.
danpost danpost

2020/6/4

#
Sammyy2323 wrote...
they are preseted into the world. I didn't right-click on the balls and add the balls into the world.
That should not make any difference at all. Show current Ball0 class.
Sammyy2323 Sammyy2323

2020/6/4

#
oh i think i wasn't putting it in the right place because now it is working. Sorry for waiting time. Thanks for the help!
You need to login to post a reply.