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

2023/3/22

how to change an actors color

envxity envxity

2023/3/22

#
im looking to learn how to change an objects color. and how to access those different colors from another actor. im looking to switch between 2 colors and thats it heres the code i got so far
public class button extends Actor
{
    Color color1 = new Color(0, 255, 0);
    Color color2 = new Color(255, 0, 0);//red
    Color Color = getImage().getColor();
    public button(){
        setImage(new GreenfootImage(70,50));
        getImage().setColor(Color.RED);
        getImage().fillOval(0, 0, 20 , 20);
        
    }
    public void act() 
    {
        
        if( getImage().getColor(color2) = Color.RED && Greenfoot.mouseClicked(this)){
            getImage().setColor(color1);
        }
        if(getImage().Color == color1 && Greenfoot.mouseClicked(this)){
            getImage().setColor(color1);
        }
    }    
danpost danpost

2023/3/22

#
envxity wrote...
im looking to learn how to change an objects color. and how to access those different colors from another actor. im looking to switch between 2 colors and thats it heres the code i got so far << Code Omitted >>
An array can be used for the color list (even if only two of them). An index value can point to the current color set to the actor. A method can be added to "get" the current color:
public class button extends Actor
{
    Color[] colors = { Color.GREEN, Color.RED };
    int colorValue = 0; // index of colors array
    
    public button() {
        setImage(new GreenfootImage(70, 50));
        changeColor();
    }
        
    public Color getCurrentColor() {
        return colors[colorValue];
    }
    
    private void changeColor() {
        colorValue = (colorValue+1)%2; // toggle index between 0 and 1
        getImage().setColor(getCurrentColor());
        getImage().fillOval(0, 0, 20 , 20);
    }
    
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) {
            changeColor();
        }
    }
envxity envxity

2023/3/23

#
what method would i use to get the color of the button to the player class in order to change something like changing the firing mode of the the character
envxity envxity

2023/3/23

#
if you need the code here it is:
public void shoot(){
        
        if ( ! getWorld().getObjects(Enemy.class).isEmpty() || Greenfoot.mouseClicked(getWorld())){       
            Actor bullet = new Bullet();
            Actor enemy = new Enemy();
            getWorld().addObject(bullet, getX(), getY());
            MouseInfo mouse = Greenfoot.getMouseInfo();
            bullet.turnTowards(mouse.getX(), mouse.getY());

        }
    }
envxity envxity

2023/3/23

#
that code is the players shoot code and i was trying to use the button to transfer from the player aiming with the mouse to the game auto aiming for you towards the nearest enemy. i was having trouble with having greenfoot finding and implementing what enemy was the closest enemy and it would help if you also put that in as well
danpost danpost

2023/3/23

#
envxity wrote...
that code is the players shoot code and i was trying to use the button to transfer from the player aiming with the mouse to the game auto aiming for you towards the nearest enemy. i was having trouble with having greenfoot finding and implementing what enemy was the closest enemy and it would help if you also put that in as well
Your shoot method could simply be:
public void shoot() {
    Button button = ((MyWorld)getWorld()).getButton();
    if (Color.GREEN.equals(button.getCurrentColor())) manualShoot(); else autoShoot();
}

private void manualShoot()
{
    if ( ! Greenfoot.mouseClicked(null)) return;
    Mouse mouse = Greenfoot.getMouseInfo();
    Actor bullet = new Bullet();
    getWorld().addObject(bullet, getX(), getY());
    bullet.turnTowards(mouse.getX(), mouse.getY());
}

private void autoShoot()
{
    if (getWorld().getObjects(Enemy.class).isEmpty()) return;
    Actor closest = null;
    int closeness = 9999;
    for (Object obj : getWorld().getObjects(Enemy.class)) {
        Actor enemy = (Actor)obj;
        int dist = (int)Math.hypot(getX()-enemy.getX(), getY()-enemy.getY());
        if (dist < closeness) {
            closeness = dist;
            closest = enemy;
        |
    }
    Actor bullet = new Bullet();
    getWorld().addObject(bullet, getX(), getY());
    bullet.turnTowards(closest.getX(), closest.getY());
}
You may have another way (or other ways) to get the button object for line 2.
envxity envxity

2023/3/24

#
i put that in but it is saying that in line 9 the Mouse mouse line with the first Mouse causing the error and i dont know why it was calling it an error and the getbutton one i was just currious on how to call that properly
envxity envxity

2023/3/24

#
 public void shoot() {
        button button = ((MyWorld)getWorld()).getButton(); <here!
        if (Color.BLACK.equals(button.getCurrentColor())) manualShoot(); else autoShoot();
    }
    private void manualShoot()
    {
        if ( ! Greenfoot.mouseClicked(null)) return;
        MouseInfo mouse = Greenfoot.getMouseInfo();
        Actor bullet = new bullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.turnTowards(mouse.getX(), mouse.getY());
    }
    private void autoShoot()
    {
        if (getWorld().getObjects(Enemy.class).isEmpty()) return;
        Actor closest = null;
        int closeness = 9999;
        for (Object obj : getWorld().getObjects(Enemy.class)) {
            Actor enemy = (Actor)obj;
            int dist = (int)Math.hypot(getX()-enemy.getX(), getY()-enemy.getY());
            if (dist < closeness) {
                closeness = dist;
                closest = enemy;
            }
            Actor bullet = new bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.turnTowards(closest.getX(), closest.getY());
        }
    }
envxity envxity

2023/3/24

#
pls help asap
envxity envxity

2023/3/24

#
i dont really know how to declare how to get the color
envxity envxity

2023/3/24

#
the color of a different class that is
envxity envxity

2023/3/25

#
i keep on getting a null pointer exception for the turn towards mouse how do i fix it? because i dont want it to only turn towards the mouse when i click i want it to always follow my mouse if the button is green which is the manual shoot mode
Spock47 Spock47

2023/3/25

#
Safe, reliable solution: Make sure your world knows about the button:
class MyWorld : World {
    ...
    private button shootButton = ...; // add creation of the shootButton on the right side
    ...
    public boolean isManualShootActive() {
        return Color.BLACK.equals(shootButton.getCurrentColor());
    }
    ...
}
    public void shoot() {
        if (isManualShootActive()) manualShoot(); else autoShoot();
   }
Or unsafe solution (will not work reliable if you have more than one button).
envxity wrote...
the color of a different class that is
    public void shoot() {
       button button = getWorld().getObjects(button.class).get(0);
       if (button != null && Color.BLACK.equals(button.getCurrentColor())) manualShoot(); else autoShoot();
   }
danpost danpost

2023/3/26

#
envxity wrote...
i keep on getting a null pointer exception for the turn towards mouse how do i fix it?
Try this then:
private void manualShoot()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse == null) return;
    turnTowards(mouse.getX(), mouse.getY());
    if ( ! Greenfoot.mouseClicked(null)) return;
    Actor bullet = new bullet();
    getWorld().addObject(bullet, getX(), getY());
    bullet.setRotation(getRotation());
}
i dont want it to only turn towards the mouse when i click i want it to always follow my mouse if the button is green which is the manual shoot mode
The code here accounts for this, if I understand you correctly.
You need to login to post a reply.