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

2012/1/10

Painting Starts onto a Background Image

3
4
5
6
7
leeX leeX

2012/10/28

#
thank you.
Wavesludge Wavesludge

2012/10/28

#
Hello. I have a similar problem as leeX, but the soloution didn't work for me, the stars went insanly fast and went a different direction every few seconds. My scenario works great when the stars are moving from left to right, but I need them to move from right to left. World
import greenfoot.*;   
import java.awt.Color;  
  
public class Space extends World  
{  
    final int starCount = 300;  
    Stars[] stars = new Stars[starCount];
    
     public Space()  
    {      
        super(600, 400, 1);   
        getBackground().setColor(Color.BLACK);  
        getBackground().fill();  
        createStars();
        for (int i = 0; i < stars.length; i++) stars[i].move(); 
    }  
      
    public void createStars()  
    {  
        for (int i = 0; i < starCount; i++)  
        {  
            int x = Greenfoot.getRandomNumber(getWidth());  
            int y = Greenfoot.getRandomNumber(getHeight());  
            stars[i] = new Stars();  
            addObject( stars[i], x, y);  
        }  
    }
}
Actor
import greenfoot.*;  
import java.awt.Color;  
  
public class Stars extends Actor  
{  
   private int speed;  
  
   public void act()
   {
       move();
   }
    
    public Stars()  
    {  
        int size = Greenfoot.getRandomNumber(4)+ 1;  
        GreenfootImage img = new GreenfootImage(size, size); 
        
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);  
        int b = Greenfoot.getRandomNumber(256); 
        img.setColor(new Color(r, g, b));  
        img.fillOval(0, 0, size, size);  
        setImage(img);
        
        speed = Greenfoot.getRandomNumber(4) + 1;  
    }  
  
    public void move()  
    {  
        int x = (getX() + speed) % getWorld().getWidth();  
        setLocation(x, getY());  
    } 
}
danpost danpost

2012/10/28

#
@Wavesludge, The code provided looks like the code that should work (the stars moving from left to right). What are you trying to make them go from right to left?
Wavesludge Wavesludge

2012/10/28

#
I tried the code you gave leeX, to make the stars go from right to left, but I can't get it to work, they either just not move at all or go really fast.
danpost danpost

2012/10/28

#
@Wavesludge, I need to see what code you used to attempt to make them go in reverse. Unrelated: remove line 15 from your world class code (moving the stars within the world constructor is something you do not want/need to do).
Wavesludge Wavesludge

2012/10/28

#
OK, I used this to make it go in reverse, but it doesn't move at all. World
import greenfoot.*;   
import java.awt.Color;  
  
public class Space extends World  
{  
    final int starCount = 300;  
    Stars[] stars = new Stars[starCount];
    
     public Space()  
    {      
        super(600, 400, 1);   
        getBackground().setColor(Color.BLACK);  
        getBackground().fill();  
        createStars();
    }  
      
    public void createStars()  
    {  
        for (int i = 0; i < starCount; i++)  
        {  
            int x = Greenfoot.getRandomNumber(getWidth());  
            int y = Greenfoot.getRandomNumber(getHeight());  
            stars[i] = new Stars();  
            addObject( stars[i], x, y);  
        }  
    }
    
    public void act()  
    {  
        for (Object obj : getObjects(Stars.class))  
        {  
            int i = 0;
            Stars star = (Stars) obj;  
            int newSpeed = star.getSpeed() - 1;  
            star.setSpeed(newSpeed);  
            star.move();  
        }  
    } 
}
Actor
import greenfoot.*;  
import java.awt.Color;  
  
public class Stars extends Actor  
{  
   private int speed;  
  
   public void act()
   {
       move();
   }
    
    public Stars()  
    {  
        int size = Greenfoot.getRandomNumber(4)+ 1;  
        GreenfootImage img = new GreenfootImage(size, size); 
        
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);  
        int b = Greenfoot.getRandomNumber(256); 
        img.setColor(new Color(r, g, b));  
        img.fillOval(0, 0, size, size);  
        setImage(img);
        
        speed = Greenfoot.getRandomNumber(4) + 1;  
    }  
  
    public void setSpeed(int newSpeed)  
    {  
        while (newSpeed < 0) newSpeed += getWorld().getWidth();  
        speed = newSpeed % getWorld().getWidth();  
    }  
  
    public int getSpeed()  
    {  
        return speed;  
    }  

    public void move()  
    {
        speed = Greenfoot.getRandomNumber(4) + 1; 
    }
}
danpost danpost

2012/10/28

#
Your 'move' method in the Stars class does not move the star object. In fact, with your 'act' method calling this 'move' method, as it is, every cycle, the speed of your stars will always be positive and, therefore, not go in reverse. In fact, I do not see any code that would actually re-locate the star objects (no 'setLocation' method calls).
Wavesludge Wavesludge

2012/10/28

#
OK, if I use a setLocation method, it will move? If I change the move method to
    public void move()  
    {
        int x = (getX() + speed) % getWorld().getWidth();  
        setLocation(x, getY());
    }
It goes in reverse, but really, really fast.
danpost danpost

2012/10/28

#
@Wavesludge, how are you changing the 'speed' variable to make it go in reverse?
Wavesludge Wavesludge

2012/10/28

#
In actor
    public void setSpeed(int newSpeed)  
    {  
        while (newSpeed < 0) newSpeed += getWorld().getWidth();  
        speed = newSpeed % getWorld().getWidth();  
    }  
  
    public int getSpeed()  
    {  
        return speed;  
    }  
In world
    public void act()  
    {  
        for (Object obj : getObjects(Stars.class))  
        {  
            int i = 0;
            Stars star = (Stars) obj;  
            int newSpeed = star.getSpeed() - 1;  
            star.setSpeed(newSpeed);  
            star.move();  
        }  
    }
danpost danpost

2012/10/28

#
For one thing, you are moving all the stars from the world, as well as moving each one from within the Stars class. Basically moving all the star objects twice each act cycle. You will have to decide if you need the world to control the stars or just have the stars do what they do from within the Stars class.
Wavesludge Wavesludge

2012/10/28

#
OK, I see what you mean, but If I remove the code from the world, the stars don't go in reverse, they go from left to right. And if I remove the code in the actor, the code in the world won't work. Got any suggestion on how I can get it to move in reverse otherwise?
danpost danpost

2012/10/28

#
I would remove the 'act' method from the Stars class. Line 7 in you 'act' method of the world class, ('int newSpeed = star.getSpeed() - 1;') is what is making a new value for setting the variable 'speed' for a Stars object. It should not really be executing unchecked (without condition). I need a few questions answered before I can give any advice: (1) Is the change in velocity from positive to negative to be a gradual untriggered event? By untriggered, I mean without a keystroke or mouseclick. (2) Do you want limits applied to the 'speed' value (a maximum and/or minimum)? It would be advisable; and supply the limit values. (3) If limiting, what do you want to happen after the limit is reached? are the stars supposed to gradual slow down and go back to the positive limit? By knowing what is required beforehand, everything can be set up appropriately (as needed, instead of having to keep changing things around every time something new is added to the requirements).
Wavesludge Wavesludge

2012/10/28

#
(1) Untriggered, I want them to move the entire time the scenario is running. (2) Not really, I want the stars to have random speed, with no limit, and always go in reverse (right to left)
danpost danpost

2012/10/28

#
OK, from your answers, I would say to put the code for the movement of the stars in the Stars class (not in the world class). Remove the 'act' method from the world class. In the Stars class, remove the 'setSpeed' and 'getSpeed' methods, keep the 'act' method, and change the 'move' method to the following:
public void move()
{
    int x = (getX() + getWorld().getWidth() - speed) % getWorld().getWidth();
    setLocation(x, getY());
}
There are more replies on the next page.
3
4
5
6
7