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

2012/12/27

turn cords location

Future Future

2012/12/27

#
Hey ! im trying to make a robot which starts at 25,25 in the world and then it will be turning around the world edge.. I need it to turnRight (90); at the cords 100,180 in the world and then push my bomb.. i am not sure how to do that! please help ! I will post my codes here if required
Future Future

2012/12/27

#
public boolean atCords() { Actor robot = getOneObjectAtOffset(0, 0, robot_1.class); if (robot = getX(100) && getY(180)) { return true; } else { return false; } } this is the code but it doesnt seem to help.. there is a mistake overthere which im not sure how to solve
vonmeth vonmeth

2012/12/27

#
Is this method in your robot_1 class? If it is, you just need it like this:
public boolean atCords()
{
	if (getX() == 100 && getY() == 180)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
Or, if you do need to get the robot at that offset:
public boolean atCords()
{
	Actor robot = getOneObjectAtOffset(0, 0, robot_1.class);
	if (robot.getX() == 100 && robot.getY() == 180)
	{
		return true;
	}
	else 
	{
		return false;
	}
}
Future Future

2012/12/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class robot_1 here. * * @author (your name) * @version (a version number or a date) */ public class robot_1 extends Actor { /** * Act - do whatever the robot_1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(foundBomb()==false && robotBump()==false) { move(1); if(atWorldEdge()) { turnRight(90); move(30); turnRight(90); move(30); turnLeft(90); } else { move(1); } if(atCords()== true) { turnRight(90); move(1); } /** if(atWorldEdge()) { turnLeft(90); move(10); turnRight(-90); Greenfoot.stop(); }*/ } else{ move(1); if (getX()==595) { turn(180); } else if (getX()==99) { turn(180); Greenfoot.stop(); } if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } if (Greenfoot.isKeyDown("up")) { move(5); } } } private void turnRight(int degrees) { turn(degrees); Greenfoot.delay(1); } private void turnLeft(int degrees) { turn(-1*degrees); Greenfoot.delay(1); } private void forward(int step) { move(step); Greenfoot.delay(1); } public boolean foundBomb() { return getOneObjectAtOffset(0, 0, bomb_1.class) !=null; } public boolean robotBump() { Actor robot = getOneObjectAtOffset(0, 0, robot_1.class); if(robot != null) { return true; } else { return false; } } public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class robot_1 here. * * @author (your name) * @version (a version number or a date) */ public class robot_1 extends Actor { /** * Act - do whatever the robot_1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(foundBomb()==false && robotBump()==false) { move(1); if(atWorldEdge()) { turnRight(90); move(30); turnRight(90); move(30); turnLeft(90); } else { move(1); } if(atCords()== true) { turnRight(90); move(1); } /** if(atWorldEdge()) { turnLeft(90); move(10); turnRight(-90); Greenfoot.stop(); }*/ } else{ move(1); if (getX()==595) { turn(180); } else if (getX()==99) { turn(180); Greenfoot.stop(); } if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } if (Greenfoot.isKeyDown("up")) { move(5); } } } private void turnRight(int degrees) { turn(degrees); Greenfoot.delay(1); } private void turnLeft(int degrees) { turn(-1*degrees); Greenfoot.delay(1); } private void forward(int step) { move(step); Greenfoot.delay(1); } public boolean foundBomb() { return getOneObjectAtOffset(0, 0, bomb_1.class) !=null; } public boolean robotBump() { Actor robot = getOneObjectAtOffset(0, 0, robot_1.class); if(robot != null) { return true; } else { return false; } } public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; } public boolean atCords() { if (getX() == 100 && getY() == 180) { return true; } else { return false; } } } this is the whole code.. um I tried using what you gave me but it didnt make the robot turn at the point i chose :S i dont know why..
danpost danpost

2012/12/27

#
The 'if' in line 4 in vonmeth's code will error if there is no robot at those coordinates. Line 4 should read
if (robot != null && robot.getX() == 100 && robot.getY() == 180)
Future Future

2012/12/27

#
I fixed the code by changing it to public boolean atCords() { Actor robot = getOneObjectAtOffset(0, 0, robot_1.class); if (robot != null && robot.getX() == 180 && robot.getY() == 100) { return true; } else { return false; } } but it still aint working...
danpost danpost

2012/12/27

#
I never said that would fix your code. I think it would be best for you to go over the Greenfoot tutorials accessed through the 'Documentation' link above before continuing to attempt to write any code. You seem to be lost as far as some of the basics of programming with Greenfoot and Java.
You need to login to post a reply.