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

2012/4/20

PLEASE HELP?

Serhil Serhil

2012/4/20

#
I have an actor named Brick and another actor named Angrybird. The actor Brick only stands in my world and the Angrybird can fly anywhere. I want that the Brick stops the Angrybird from moving when he hits it, so that when the Angrybird hits the Brick, he needs to stop and the Angrybird should go around the Brick to proceed its flight. anyone who knows how to do this? this is the movement code from my Angrybird: void move() { int speed = 10; if(Greenfoot.isKeyDown("up")) setLocation(getX(), getY() - speed); if(Greenfoot.isKeyDown("down")) setLocation(getX(), getY() + speed); if(Greenfoot.isKeyDown("left")) setLocation(getX() - speed, getY()); if(Greenfoot.isKeyDown("right")) setLocation(getX() + speed, getY()); } And this is the code I've got so far for the Brick: public void act() { setLocation(getX() -3, getY()); if (foundAngrybird2()) { stopAngrybird2(); } } public boolean foundAngrybird2() { Actor Angrybird2 = getOneObjectAtOffset(0, 0, Angrybird2.class); if(Angrybird2 != null) { return true; } else { return false; } } public void stopAngrybird2() { Actor Angrybird2 = getOneObjectAtOffset(0, 0, Angrybird2.class); if(Angrybird2 !=null) { // stop Angrybird2 } }
danbhome danbhome

2012/4/22

#
try taking off the 2s? maybye?hmm?
danpost danpost

2012/4/23

#
The problem here is that Bricks (especially those that do not move) do not act (under normal circumstances). Instead of trying to control the Angrybird2 from the Brick class, try it from the Angrybird class. After moving, check to see in a Brick intersects, if so, move back. Basically: move(1); if (!getIntersectingObjects(Brick.class).isEmpty()) move(-1); BTW: 10 is quite a fast speed for the Angrybird, I hope your bricks are at least 10 x 10 in size, or your bird might fly right through one.
Serhil Serhil

2012/4/23

#
wow thank you very much! it worked! :D
Serhil Serhil

2012/4/23

#
But, i can go inside the brick from above and below (and from the backside) i have already changed the speed from the angrybird, but he can still go inside the brick is there a code for this or..?
ttamasu ttamasu

2012/4/24

#
Have you tried using getObjectsAtOffset(); method? This sounds like what you need since it checks for intersection at an offset dx, dy and since you know the velocity of the bird, you can peek at the offset location to see if there are any bricks there. and if so set the location of the bird to the edge of the brick.
danpost danpost

2012/4/24

#
You could also enclose may suggestion in a for loop that would iterate the speed of the Angrybird.
for (int i = 0; i < speed; i++)
{
    move(1);
    if (!getIntersectingObjects(Brick.class).isEmpty()) move(-1);
}
That would in essence give the bird the right speed and check each cell (or pixel) for a brick.
danpost danpost

2012/4/24

#
You might want to change your Angrybird move() method to:
public void move()
{
    int speed is 10;
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("right")) dx += 1;
    if (Greenfoot.isKeyDown("left")) dx -= 1;
    if (Greenfoot.isKeyDown("down")) dy += 1;
    if (Greenfoot.isKeyDown("up")) dy -= 1;
    for (int i = 0; i < speed; i++)
    {
        setLocation(getX() + dx, getY() + dy);
        if (!getIntersectingObjects(Brick.class).isEmpty()) setLocation(getX() - dx, getY() - dy);
    }
}
Serhil Serhil

2012/4/25

#
yess thankss now it works perfect :D
You need to login to post a reply.