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

2011/12/5

How to make an Object let block the other Object?

TopInPut TopInPut

2011/12/5

#
That they can't run thought the other Object. ^^ Thx
Morran Morran

2011/12/5

#
You want an Actor, say, "Wall", to stop another Actor, say, "Person", from moving? You could put in the "Person" class, whenever you want it to move:
Actor wall = getOneObjectAtOffset(Wall.class, xOffset, yOffset); //xOffset and yOffset can be whatever you want
if(wall == null) { //if the person is not touching a wall
      move();//or do whatever you do to move the person
}
//if the wall is not null(it is real and is touching the person), do nothing.
Your question is a little vague, so if you can make it clearer I can give you better details.
TopInPut TopInPut

2011/12/5

#
I wanna programm a little 2D soccer game. And the Players may not run into the ball or the other Player.;)
TopInPut TopInPut

2011/12/5

#
Players are TeamRot and TeamBlau. Both are under the Mover and the Mover is a class of Actor. =D
Morran Morran

2011/12/7

#
Are you sure that you don't want the Ball to be able to move past the player? Maybe what you want is for the player to hold on to the ball whenever he touches it. But, maybe you do want the player to stop whenever he hits the ball. If so, then you would want this:
TeamRot redTeamMember = getOneObjectAtOffset(TeamRot.class, xOffset, yOffset); //xOffset and yOffset can be whatever you want  
Ball ball = getOneObjectAtOffset(Ball.class, xOffset, yOffset); //xOffset and yOffset can be whatever you want  
if(redTeamMember == null && ball == null) { //if the player is not hitting an opposing team member, or the ball
      //put the code to move the player here 
}  
If you do not want the player to stop moving when he hits the ball, but instead to hold onto the ball, you should do this:
TeamRot redTeamMember = getOneObjectAtOffset(TeamRot.class, xOffset, yOffset); //xOffset and yOffset can be whatever you want  
if(redTeamMember == null) { //if the player is not hitting an opposing team member
      //then it can catch the ball,
      tryToCatchBall();
      //put the code to move the player here 
} 
//here is what tryToCatchBall() should look like:
public void tryToCatchBall()
{
    Ball ball = (Ball) getOneIntersectingObject(this);
    if(ball != null) {
         //we divide the width and height by 2 to get the space between the center of the player and the edge of the player's image.
         int width = getImage().getWidth()/2;
         int height = getImage().getHeight()/2;
         if(direction.equals("west"))
            ball.setLocation(getX()-width, getY());
         else if(direction.equals("north"))
            ball.setLocation(getX(), getY()-height);
         else if(direction.equals("east"))
            ball.setLocation(getX()+width, getY());
         else if(direction.equals("south"))
            ball.setLocation(getX(), getY()+height);
    }
}
 
TeamRot and TeamBlau? They must be German teams. Maybe you could have international matches?
TopInPut TopInPut

2011/12/7

#
Yeah, thanks.;) And yes they are german.;D
You need to login to post a reply.