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

2019/2/9

get backgroound of an Actor

1
2
xandreas_1 xandreas_1

2019/2/9

#
Hello community. I already startes a discussion about colordetection. Now I want to know what I ahve to change in my code to geet the background of an another Actor.
getWorld().getBackground().getColorAt(getX(), getY()+4).equals(color1)
xandreas_1 xandreas_1

2019/2/9

#
public class Wheel1 extends Actor
{
    
   Color color1 = new Color(106,82,54); 
   private boolean FarbeGrund = false;
    
    
    public void act() 
    {
       
     if(getWorld().getBackground().getColorAt(getX(), getY()+4).equals(color1))
     {
        this.FarbeGrund = true;
    }
    else
    {
    this.FarbeGrund = false;
    }
        
    if(FarbeGrund = true)
       {
           setLocation(getX(), getY()+2);
        }
   else
   {
       setLocation(getX(), getY()-2);
    }
        
        
       if (Greenfoot.isKeyDown("w"))
       turn(+1);
       if (Greenfoot.isKeyDown("s"))
       turn(-1);
       
        
        
    } 
danpost danpost

2019/2/9

#
xandreas_1 wrote...
want to know what I ahve to change in my code to geet the background of an another Actor. << Code Omitted >>
I am not sure you need the actor image. I do see a problem with line 20. You are assigning true to the value of FarbeGrund there which will make the condition true no matter what. Either use the conditional equality operator "==" or, since it is already a boolean value, nothing:
if (FarbeGrund == true)
/**     or     */
if (FarbeGrund)
xandreas_1 xandreas_1

2019/2/9

#
So I want to check the color of the other Actor background.. Or is there another way to solve that?
danpost danpost

2019/2/9

#
xandreas_1 wrote...
So I want to check the color of the other Actor background.. Or is there another way to solve that?
You first need a reference to that other actor. Then use getImage on that.
xandreas_1 xandreas_1

2019/2/9

#
What do yoe mean by reference and how can I do that? Thx already!
danpost danpost

2019/2/9

#
xandreas_1 wrote...
What do yoe mean by reference and how can I do that? Thx already!
For example, an actor would get a reference to its world with:
World world = getWorld();
where world is the name of a variable that contains a reference to the World object the actor is in. How you do it for that actor you want really depends on what you are trying to do. Since you have given very little to go on in that regard, I cannot be any more specific.
xandreas_1 xandreas_1

2019/2/9

#
Okay let me tell a bit more: I want my Actor class "Wheel1" to check if its at the current coordinates in contact with color1(defined in actor class Wheel1) of the image of the other Actor class "background"
danpost danpost

2019/2/9

#
xandreas_1 wrote...
Okay let me tell a bit more: I want my Actor class "Wheel1" to check if its at the current coordinates in contact with color1(defined in actor class Wheel1) of the image of the other Actor class "background"
Is it necessary that the background be an actor? that is, is there any specific reason you are not setting the background image of the world to that background image?
xandreas_1 xandreas_1

2019/2/10

#
I tried to do it like you described in your scrolling totourial but something is wrong...
public class MyWorld extends World
{
public static final int WIDE = 1500;
public static final int HIGH = 750;
Scroller scroller;
    
    public MyWorld()
    {    
      
    
    
        super(WIDE, HIGH, 1, false) ;
      GreenfootImage image = new GreenfootImage("greenLandscapewithbrown1.0.jpg");
      scroller = new Scroller(this,image);
    }
    
    public void act()
    {
     scroll();   
    }
    private void scroll()
    {
     int rate = 2;
     int dsx = 0;
     if(Greenfoot.isKeyDown("W")) dsx ++;
     if(Greenfoot.isKeyDown("S")) dsx --;
     scroller.scroll(dsx*rate);
    }
}
xandreas_1 xandreas_1

2019/2/10

#
In line 27 .scroll is underlined and it shows the errot: "cannot find symbol - method scroll(int)"
danpost danpost

2019/2/10

#
xandreas_1 wrote...
In line 27 .scroll is underlined and it shows the errot: "cannot find symbol - method scroll(int)"
The scroll method called on line 27 takes 2 int parameters. If you only want horizontal scrolling, change the line to:
scroller.scroll(dsx*rate, 0);
xandreas_1 xandreas_1

2019/2/10

#
Do I have to create a Subclass of myWorld called "Scroller" ?
danpost danpost

2019/2/10

#
xandreas_1 wrote...
Do I have to create a Subclass of myWorld called "Scroller" ?
No. You should already have a Scroller class in your project listed in your Other classes frame.
xandreas_1 xandreas_1

2019/2/10

#
public class MyWorld extends World
{
public static final int WIDE = 1500;
public static final int HIGH = 750;
Scroller scroller;
    
    public MyWorld()
    {    
      
    
    
        super(WIDE, HIGH, 1, false) ;
      GreenfootImage image = new GreenfootImage("greenLandscapewithbrown1.0.jpg");
      scroller = new Scroller(this,image);
    }
    
    public void act()
    {
     scroll();   
    }
    private void scroll()
    {
     int rate = 2;
     int dsx = 0;
     if(Greenfoot.isKeyDown("W")) dsx ++;
     if(Greenfoot.isKeyDown("S")) dsx --;
     scroller.scroll(dsx*rate);
    }
}
There are more replies on the next page.
1
2