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

2019/1/18

Get an actor to bounce back when egde is hit.

Notted Notted

2019/1/18

#
private int movingBomber(int bomberSpeed)
    {
        setLocation(getX() + bomberSpeed, getY());
        if (isAtEdge())
        {
           setLocation(getX() - bomberSpeed, getY());
        }
        return bomberSpeed;
    }
This does not seem to work; whenever he hits the egde, he just stops. How do I fix this? Also, how can get the width of an image for hit detection purposes?
 private void collectBomb()
    {
      Actor bomb = getOneObjectAtOffset(GreenfootImage.getWidth(), 0, Bomb.class);
      if (bomb != null)
      {
        getWorld().removeObject(bomb);
        if (++numOfBombs == 100) Greenfoot.stop();
      }
    }
danpost danpost

2019/1/18

#
Notted wrote...
<< Code Omitted >> This does not seem to work; whenever he hits the egde, he just stops.
The code given only moves the actor back off the edge. It does nothing for the direction that the actor is set to move in.
how can get the width of an image for hit detection purposes? << Code Omitted >>
Certainly a class (GreenfootImage) does not have a width. Q (to make you think): what image are you trying to get the width of?
Notted Notted

2019/1/18

#
bucket.png. It's 31 pixels in width. I don't really know how to set the direction for isAtEgde. How can I do this? Is this the right way to get the width of an image for hit detection?
GreenfootImage bucketImg = getImage();
      Actor bomb = getOneObjectAtOffset(bucketImg.getWidth(), 0, Bomb.class);
danpost danpost

2019/1/18

#
Notted wrote...
bucket.png. It's 31 pixels in width.
So, it is the image assigned to your Bucket object and the collectBomb method is in your Bucket class. The Bucket object is an Actor object, so how do you get the image of an actor?
I'm pretty sure I don't really know how to set the direction for isAtEgde. How can I do this?
"direction for isAtEdge" does not make any sense. The actor has a direction. It is given by bomberSpeed. It needs changed when the edge is hit.
Notted Notted

2019/1/18

#
I think it is being changed by these lines.
 if (isAtEdge())
        {
           setLocation(getX() - bomberSpeed, getY());
        }
I think I already found out how to get an image's boundaries for hit detection.
private void collectBomb()
    {
      GreenfootImage bombImg = getImage();
      Actor bomb = getOneObjectAtOffset(bombImg.getWidth(), bombImg.getHeight(),
The problem is now the bomb go straight through the bucket. It used to be that the bombs go through the sides of the bucket. Now they just phase through the bucket. I'm not sure how to remedy this.
danpost danpost

2019/1/18

#
Notted wrote...
I think it is being changed by these lines. << Code Omitted >>
I already pointed out
The code given only moves the actor back off the edge. It does nothing for the direction that the actor is set to move in.
I think I already found out how to get an image's boundaries for hit detection. << Code Omitted >> The problem is now the bomb go straight through the bucket. It used to be that the bombs go through the sides of the bucket. Now they just phase through the bucket. I'm not sure how to remedy this.
You are looking at a point off the lower-right edge of the image. The offset values used for arguments are from the center of the image (at the actor's location).
Notted Notted

2019/1/18

#
So, if this
if (isAtEdge())
       {
          setLocation(getX() - bomberSpeed, getY());
       }
is not setting the direction of the actor, will setRotation(), do the job? If not, what will? I want to use the boundaries of the image as the bombs hitting the bucket. How do I do this, since I don't know?
danpost danpost

2019/1/18

#
Notted wrote...
So, if this << Code Omitted >> is not setting the direction of the actor, will setRotation(), do the job? If not, what will?
Yes, but your actor will not the be upright. Just negate the speed of the actor.
I want to use the boundaries of the image as the bombs hitting the bucket. How do I do this, since I don't know?
First, see if there is an intersecting bomb. If so, then get the image and determine whether the location of the bomb is in the collectible region. The left edge of the basket would be at:
int basketLeftX = getX()-getImage().getWidth()/2;
From this, you should be able to deduce how to get the right x as well as the top y of the basket.
You need to login to post a reply.