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

2018/2/25

Making object return to starting position when touching a color

1
2
3
4
game_salajan game_salajan

2018/2/25

#
import greenfoot.*;
import java.awt.Color;

public class masinutza extends Actor
  {
     public void act()
       {
        if ( Greenfoot.isKeyDown("up") )
       {move(3);}
       if ( Greenfoot.isKeyDown("down") )
       {move(-3);}
       if ( Greenfoot.isKeyDown("right") )
       {turn(3);}
       if ( Greenfoot.isKeyDown("left") )
       {turn(-3);}
    Color color1 = new Color(34,177,76);
    if(color1.equals(getWorld().getBackground().getColorAt(130, 448)))
            { setLocation(106,448);}
       }
  }
game_salajan game_salajan

2018/2/25

#
danpost wrote...
game_salajan wrote...
So let me explain it clearly
Well, that was semi-clear. If you are checking for "road" color, which is on the background image of the world, then there is no 'actor' to get. Just get the world background and the color at which the car is located to compare with road color:
if (color1.equals(getWorld().getBackground().getColorAt(getX(), getY())))
so this should return something ? because i don't get it , i am comparing 2 colors to do what ?
Super_Hippo Super_Hippo

2018/2/25

#
130|448 should be the coordinates where the object is right now (getX() and getY()). So you check if the color of the background where the object is equals color1.
game_salajan game_salajan

2018/2/25

#
Tried that , doesen't works :( ...
game_salajan game_salajan

2018/2/25

#
if (color1.equals(getWorld().getBackground().getColorAt(getX(),getY()))) {setLocation(106,448);} is just acting like before , i really can t understand , it seems logically :/
game_salajan game_salajan

2018/2/25

#
actually it is 106|448 ( my initial car coordinates )
game_salajan game_salajan

2018/2/25

#
but it doesen't matters , it is just not working :(
danpost danpost

2018/2/25

#
Do not use values -- use 'getX()' and 'getY()' -- as given.
game_salajan game_salajan

2018/2/25

#
i did , in the end , but still the same :(
game_salajan game_salajan

2018/2/25

#
if (color1.equals(getWorld().getBackground().getColorAt(getX(),getY()))) {setLocation(106,448);}
danpost danpost

2018/2/25

#
Can you control the car -- move it along the road? then, if so, nothing happens when you encounter the given color (meaning what)? -- the car just continues to move around? Sounds like the color detection is not working. Are you sure you have he exact color to check against? Add the following temporary code at the end of the act method:
if (Greenfoot.isKeyDown("g"))
{
    Color color = getWorld().getBackground().getColorAt(getX(), getY());
    System.out.println("Color:  "+color);
    Greenfoot.stop();
}
Run the scenario and move the car to the grass; then hit the "g" key. Report back with what the terminal prints.
game_salajan game_salajan

2018/2/25

#
yea that s it , the car continues to move around , like it ignores that part of code that i wrote
game_salajan game_salajan

2018/2/25

#
yea it seems that this code has an error , something "greenfoot.color cannot be converted to java.awt.color " ..
danpost wrote...
Can you control the car -- move it along the road? then, if so, nothing happens when you encounter the given color (meaning what)? -- the car just continues to move around? Sounds like the color detection is not working. Are you sure you have he exact color to check against? Add the following temporary code at the end of the act method:
if (Greenfoot.isKeyDown("g"))
{
    Color color = getWorld().getBackground().getColorAt(getX(), getY());
    System.out.println("Color:  "+color);
    Greenfoot.stop();
}
Run the scenario and move the car to the grass; then hit the "g" key. Report back with what the terminal prints.
danpost danpost

2018/2/25

#
Remove any line similar to the following from any and all classes:
import java.awt.Color;
game_salajan game_salajan

2018/2/25

#
i did it , still same error
There are more replies on the next page.
1
2
3
4