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

2012/1/10

Painting Starts onto a Background Image

2
3
4
5
6
7
danpost danpost

2012/10/25

#
To call the move() in the Star class from the world class, use the following to move the first star in your array of stars
stars[0].move();
Change '0' to any number between '1' and '299' to move a different star.
danpost danpost

2012/10/25

#
To move all the stars at the same time, use
for (int i = 0; i < stars.length; i++) stars[i].move();
leeX leeX

2012/10/27

#
Hi. my stars still get stuck one side and are not created on the other
        int x = (getX() + speed) % getWorld().getWidth();
        setLocation(x, getY());
danpost danpost

2012/10/28

#
The problem does not reside in the statements given. I would suggest looking at your world class, first.
leeX leeX

2012/10/28

#
    public void act()
    {
        for (Object obj : getObjects(Star.class)) ((Star) obj).move(setSpeed);
    }
i just want it to move in one direction in your example what part makes the stars move at different speeds? // i think i made some progress, but thank you anyways. your earler posts very helpful
leeX leeX

2012/10/28

#
    public void act()
    {
        setSpeed = (setSpeed + 359) % 360;
        for (Object obj : getObjects(Star.class)) ((Star) obj).move(setSpeed);
    }
i changed my code to this. when it scrolls from right to left its ok, but when i make it a minus the stars get stuck
danpost danpost

2012/10/28

#
I had put an instance variable 'speed' in the Star class. In the Star constructor, each star is assigned a random speed between 1 and 4.
danpost danpost

2012/10/28

#
Sorry, I had forgot we moved the moving of the stars to the world class. Give me a few minutes to re-acquaint myself to what you have so far.
danpost danpost

2012/10/28

#
Maybe it would be better if you post your complete SpaceWorld and Star classes again, so I can see what you have at this point.
leeX leeX

2012/10/28

#
import greenfoot.*;
import java.awt.Color;

public class Space extends World
{
    int stars = 100;
    int i;
    int setSpeed;
    int x = Greenfoot.getRandomNumber(getWidth());
    int y = Greenfoot.getRandomNumber(getHeight());
    
    public Space()
    {    
        super(600, 400, 1); 
        createBackgroundImage();
        createStars();
    }

    private void createBackgroundImage()
    {
        getBackground().setColor(Color.black);
        getBackground().fill();
    }
    
    private void createStars()
    {
        while (i < stars) {
            i++;
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Star(), x, y);
        }
    }
        
    public void act()
    {
        setSpeed -= 1; 
        for (Object obj : getObjects(Star.class)) ((Star) obj).move(setSpeed);
    }
}
import greenfoot.*;
import java.awt.Color;

public class Star extends Actor
{
    /**
     *  creates the star and gives it various random attributes
     */
    public Star()
    {
        int size = Greenfoot.getRandomNumber(4) + 1; 
        GreenfootImage star = new GreenfootImage(size, size);
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);
        int b = Greenfoot.getRandomNumber(256);
        star.setColor(new Color(r, g, b));
        star.fillOval(0, 0, size, size);
        setImage(star);
    }
    
    public void move(int speed)
    {
        int x = (getX() + speed) % getWorld().getWidth();
        setLocation(x, getY());
    }
}
dont want to just copy your code thats why i try to do things step by step so i learn.
leeX leeX

2012/10/28

#
currently i dont get why if i make the speed negativ stars get stuck and positive it works.
danpost danpost

2012/10/28

#
Oh, were you refering to my demo, when you asked about the different speeds? If so, the name of the variable is 'proximity'. It gets a random double value between 0.1 and 5.0, which is used as the speed factor. The higher the proximity value (the closer the star is) the faster it will appear to move.
leeX leeX

2012/10/28

#
i got the random speed working but how can i make it go from right to left. with a positv it move correctly from left to right. if i make it negativ stars move to a side and get stuck.
danpost danpost

2012/10/28

#
First, remove lines 7 through 10 as they is no need to save these. The 'speed' variable should still be in the Star class, as it is an attribute of the star object. You can add methods to change and return its value.
// in the Star class
// add the following two methods
public void setSpeed(int newSpeed)
{
    while (newSpeed < 0) newSpeed += getWorld().getWidth();
    speed = newSpeed % getWorld().getWidth();
}

public int getSpeed()
{
    return speed;
}
// then, change line 21 in the Star class to
public void move()
// also, add the line in the Star constructor
speed = Greenfoot.getRandomNumber(4) + 1;
// NOW, in the SpaceWorld class
// insert before line 27
int i = 0;
// change the act() method to the following
public void act()
{
    for (Object obj : getObjects(Star.class))
    {
        Star star = (Star) obj;
        int newSpeed = star.getSpeed() - 1;
        star.setSpeed(newSpeed);
        star.move();
    }
}
NOTE: this was posted prior to seeing your last post
danpost danpost

2012/10/28

#
If you make the changes as listed above, you should not continue to have that problem. Lines 5 and 6 in the code of my previous post ensure that the value of 'speed' remains between 0 and getWorld().getWidth() - 1, inclusive.
There are more replies on the next page.
2
3
4
5
6
7