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

2012/11/22

Make object1 push object 2 to a certain location and go back to its starting location

Luffy Luffy

2012/11/22

#
Hi guys, im new to Greenfoot and im struggling :( As title says, I wanna know how can i make object1 move object2 to a certain location for example a 'square' and object 1 goes back to its original potion. I know how I can make it move via keyboard but I wanna make it automatically detect the object and push it to the square and go back to its original position. I was thinking of trying to make a scanner and scan the area for the object but it didnt work out in the end :( any help would be appreciated, Here is my robot - public void act() { int x = getX(); int y = getY(); setLocation(x + 1, y); } public boolean foundBomb() { Actor bomb = getOneObjectAtOffset(0, 0, bomb_1.class); if(bomb != null) { return true; } else { return false; } } } Here is my 'tank' public void act() { if (robotBump()) move(); } public boolean robotBump() { Actor robot = getOneObjectAtOffset(0, 0, robot_1.class); if(robot != null) { return true; } else { return false; } } public void move() { int x = getX(); int y = getY(); setLocation(x + 1, y); } and my square is empty
danpost danpost

2012/11/22

#
Your question was a bit confusing; not in what you want to do, but in what will be doing the pushing and what is being pushed. It looks like you want the robot to push the tank (because the robot always moves forward and the tank only moves forward when 'robotBump' is 'true'. Using 'getOneObjectAtOffset(0, 0, robot_1.class)' in your 'robotBump' method in the tank class should probably be 'getOneIntersectingObject(robot_1.class)'; otherwise, the tank will not move until to image of the robot makes it to the center of the tank object. The robot can check for intersection of the square to trigger a return to start location; however, you will need to keep track of the direction the robot is moving (an 'int direction' to equal -1, 0, or 1), adjusting its value when neccessary and using it in the setLocation statement. You will also need a check to find out when the robot made it back to its starting location and another to signify that a tank is present an ready to be pushed to start the robot moving (unless you want it to move back and forth, in which case the tank cannot be placed while the robot is moving back toward its starting location). There is too much unknown to me about your scenario to really give a detailed description of what is needed.
Luffy Luffy

2012/11/22

#
Thanks for the help dan but still kinda lost hehe :D, Should I post my scenario up and let people give me advice?
danpost danpost

2012/11/22

#
You can do that, but if your code looks similar to the code above, it will probably take some time to get it working the way you want. You need to put some effort into trying to get something going, first; then, if you encounter difficulties, post what you have and ask for assistance (explaining exactly what you want, and how it is not doing what you want). You need to work on one thing at a time. First thing you need to do is decide how you want to restrict the motion of your robot. Like, do you want to use its x-coordinate its starting location to determine when to stop when it is returning (you can use intersection with the square to change direction to go back)? Next, how can I get the robot to know which way it is to be going (or stand in place)? Then, do I want the robot to turn around and start going back toward to square when it returns to its home location, or do I want it to stop at home until some coditions are met? and if the latter, what conditions do you want to use? Once you get the robot doing what you want, you can start on something else.
Luffy Luffy

2012/11/27

#
Hey dan, this is my updated version of the robot. So far it does what its meant to do. If you can see improvements to be added then please go ahead and tell me :) public void act() { int x = getX(); int y = getY(); if (595> x) { move(1); } else if (x==595){ turn(180); move(1); } if (x==99) { turn(180); Greenfoot.stop(); } } public boolean foundBomb() { Actor bomb = getOneObjectAtOffset(0, 0, bomb_1.class); if(bomb != null) { return true; } else { return false; } } }
danpost danpost

2012/11/27

#
It usually better to check conditions of variables and state immediately after changing them. When moving an actor, the location (x and y) is what changes; so, move the actor, then do your checks. In your case, only the x is of concern (since the y does not change). The following is comparable to your code, using move, then check :
public void act()
{
    move(1);
    if (getX()==595)
    {
        turn(180);
    }
    if (getX()==99)
    {
        turn(180);
        Greenfoot.stop();
    }
}

public boolean foundBomb()
{
    return getOneObjectAtOffset(0, 0, bomb_1.class) != null;
}
}
As you can see, I cleaned the code up a little. The only difference in the execution between your code and mine is that your actor must start with an x coordinate value no less than 100; where in mine, no less than 99. I kind of got the feeling your actor starts at 100 and you had to stop it at 99 because of the order of 'check/then move' instead of 'move/then check'.
Luffy Luffy

2012/11/27

#
Thanks
You need to login to post a reply.