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

2019/2/25

changing an Actor's Image from a World Class

xandreas_1 xandreas_1

2019/2/25

#
Hi guys, How can get a reference from a world class to an Actor, to change the Actors Image?? This is what I got so far:
public void Racer()
    {
        if(Greenfoot.isKeyDown("2"))  
      {
        

          
        
        car Car = (car)getWorld().getObjects(car.class).get(0);
        Car.setImage(new GreenfootImage("BlueCar.jpg")); 
        


        Greenfoot.setWorld(new Welt());
        sound1.stop();
      }
    }
petyritonel petyritonel

2019/2/25

#
a solution is to use a
public static GreenfootImage picture = new GreenfootImage ("picture1.png");  
variable for your actor's picture and after that in the world, you can change it by using
actor.picture = new GreenfootImage ("picture2.png");  
in the actors act method you should have
setImage(picture);
xandreas_1 xandreas_1

2019/2/25

#
petyritonel wrote...
a solution is to use a
public static GreenfootImage picture = new GreenfootImage ("picture1.png");  
variable for your actor's picture and after that in the world, you can change it by using
actor.picture = new GreenfootImage ("picture2.png");  
in the actors act method you should have
setImage(picture);
Don't I need a refence to the Actor?
petyritonel petyritonel

2019/2/25

#
with
actor.picture = new GreenfootImage ("picture2.png");
you reference it. you should try it out. it worked for me.
xandreas_1 xandreas_1

2019/2/25

#
This is the code of my World class:
 public void Racer()
    {
        if(Greenfoot.isKeyDown("2"))  
      {
        

          
        
        
        car.picture = new GreenfootImage("BlueCar.jpg"); 
        
        
        
        //Auto mit Fahrer Cut.png
        
        
        
        Greenfoot.setWorld(new Welt());
        sound1.stop();
      }
    }
and this is my actors code:
public class car extends Actor 
{
     
   Color color1 = new Color(106,82,54);  
   
   
   private boolean FarbeGrundVorne = false; 
   private boolean FarbeGrundHinten = false;
   private boolean BewegungVorne = false;
   private boolean BewegungHinten = false;
   
   public static GreenfootImage picture = new GreenfootImage ("Auto mit Fahrer Cut.png");
   
   
   public static GreenfootSound sound2 = new GreenfootSound("Gas2.wav"); 
   GreenfootSound sound1 = new GreenfootSound("StartGas.wav");
   GreenfootSound sound3 = new GreenfootSound("GasSlow.wav"); 
   GreenfootSound sound4 = new GreenfootSound("Aufsammeln.wav");
   GreenfootSound sound5 = new GreenfootSound("Tank.wav");
   GreenfootSound sound6 = new GreenfootSound("horn.wav");
   
   private int Timer1 = 0;
    private int Timer2 = 0;
    private int Timer3;
    private int Timer4;
    private int Timer5; 
    private int Timer6 = 0;  
   
    public car()
    {
       
    }
     
    
    
    
    
    public void act() 
    {
       
        setImage(picture);
        
xandreas_1 xandreas_1

2019/2/25

#
It doesn't work, at my World class it says in the merthod Racer at car. picture cannot find that symbol or variable
danpost danpost

2019/2/25

#
xandreas_1 wrote...
Don't I need a refence to the Actor?
The solution given by peyritonel will not work as expected as a new world is immediately set active (any changes in old world are irrelevant here). Most probably, you would want to change the image of the car in the new world:
World welt = new Welt()
((Actor)welt.getObjects(car.class).get(0)).setImage("BlueCar.jpg");
Greenfoot.setWorld(welt);
xandreas_1 xandreas_1

2019/2/25

#
danpost wrote...
xandreas_1 wrote...
Don't I need a refence to the Actor?
The solution given by peyritonel will not work as expected as a new world is immediately set active (any changes in old world are irrelevant here). Most probably, you would want to change the image of the car in the new world:
World welt = new Welt()
((Actor)welt.getObjects(car.class).get(0)).setImage("BlueCar.jpg");
Greenfoot.setWorld(welt);
Where do I have to insert this Code Danpost?
danpost danpost

2019/2/25

#
xandreas_1 wrote...
Where do I have to insert this Code Danpost?
It should replace lines 5 through 14 in your first code posting above.
xandreas_1 xandreas_1

2019/2/25

#
Thx a lot! I missed a ( ; ), it missed in youe Code xD
danpost danpost

2019/2/25

#
xandreas_1 wrote...
Thx a lot! I missed a ( ; ), it missed in youe Code xD
It happens -- sorry.
xandreas_1 xandreas_1

2019/2/25

#
danpost wrote...
xandreas_1 wrote...
Thx a lot! I missed a ( ; ), it missed in youe Code xD
It happens -- sorry.
don't mine your the best :D
You need to login to post a reply.