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

2018/1/23

Detecting Color of the Pixel

Flare Flare

2018/1/23

#
Hey, I was wondering if someone could help me figure out how to have one actor detecting the color of another actor?
danpost danpost

2018/1/23

#
Get the GreenfootImage set to the actor with 'getImage'. Then get the Color of a pixel of that image with 'getColorAt'. So, for example, to get the color at the top-left corner of the image set to an Actor object named 'actor':
Color c = actor.getImage().getColorAt(0, 0);
Flare Flare

2018/1/25

#
Thank you, but now there's an error because it's saying, using your example:
Color c = actor.getImage().getColorAt(0, 0);
that I can't use the "actor.getImage()" bit. It says I can't reference a non-static method from a static context.
danpost danpost

2018/1/25

#
Flare wrote...
It says I can't reference a non-static method from a static context.
That means you are not referencing any particular object created from the class; but are only using the class name; however, a class does not have an image (altthough it may hold the default image to be used for its actors). Show the entire class code for further examination into the issue.
Flare Flare

2018/1/26

#
Here's my PlayerOne code:
public class PlayerOne extends Actor
{
    private GreenfootImage image;
    public static int playerOneX;
    public static int playerOneY;
    
    public PlayerOne()
    {
        image = new GreenfootImage("playerone.png");
    }
    
    /**
     * Act - do whatever the PlayerOne wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        turnMove();
        playerOneX = getX();
        playerOneY = getY();
        
    } 
    
    
    
    public void turnMove()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turnTowards(0, getY());
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            turnTowards(600, getY());
        }
        
        if(Greenfoot.isKeyDown("up"))
        {
            turnTowards(getX(), 0);
        }
        
        if(Greenfoot.isKeyDown("down"))
        {
            turnTowards(getX(), 700);
        }
        
        move(3);
    }
    
    private void checkLevel()
    {
        if(isAtEdge())
        {
            LineOne.lineOneLevel++;
        }
    }

    /*
     *Figure out how to reference a different actor's image
     *Also figure out how to define the color
     */
    
}
Here's the code for LineOne: public class LineOne extends Actor { public GreenfootImage imageLOOne = new GreenfootImage("FirstLine.png"); public static int lineOneLevel; /** * Act - do whatever the LineOne wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public LineOne() { lineOneLevel = 1; } public void act() { // Add your action code here. } private void checkLevel() { if(lineOneLevel == 1) { } if(lineOneLevel == 2) { } if(lineOneLevel == 3) { } if(lineOneLevel == 4) { } if(lineOneLevel == 5) { } if(lineOneLevel == 6) { } } }
danpost danpost

2018/1/26

#
Do you ever create a LineOne object (using 'new LineOne()')?
Flare Flare

2018/2/1

#
Only in the prepare() method in my world class
danpost danpost

2018/2/1

#
So, the PlayerOne object needs the color of the LineOne object? When does the color of the other actor become important?
Flare Flare

2018/2/6

#
It is important at all times. If PlayerOne is not on the red portion of LineOne, then it needs to reset.
You need to login to post a reply.