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

2018/3/28

Two Problems

Hellster Hellster

2018/3/28

#
Problem one: How do I make my actor look left when he moves left and look right when he moves right. This is the code:
 if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY()-9);
        }
        if (Greenfoot.isKeyDown("s"))
        {
           setLocation(getX(), getY()+9);
        }  
Problem 2: How do in make my bullets disappear when they are touching the border.
danpost danpost

2018/3/28

#
P1: determine direction to move and then determine if rotation and image needs changed before moving. P2: Use the Actor class isAtEdge method and in conjunction with the World class removeObject(Actor) method.
Hellster Hellster

2018/3/28

#
danpost wrote...
P1: determine direction to move and then determine if rotation and image needs changed before moving. P2: Use the Actor class isAtEdge method and in conjunction with the World class removeObject(Actor) method.
Ok I understand the second one but I don't understand the first how would I do it exactly
Hellster Hellster

2018/3/28

#
danpost wrote...
P1: determine direction to move and then determine if rotation and image needs changed before moving. P2: Use the Actor class isAtEdge method and in conjunction with the World class removeObject(Actor) method.
also y doesn't this work
public void act() 
    {
        move(5);
        if (isAtEdge() == true)
        {
         Actor Bullet;
         Bullet = getOneObjectAtOffset(0, 0, Bullet.class);
         World world;
         world = getWorld();
         world.removeObject(Bullet); 
        }
    }
    public boolean isAtEdge()
    {
     
     if(getX() >=998)
     {
         return true;
     }
     if(getY() >=798)
     {
         return true;
        }
     return false;
    }
danpost danpost

2018/3/28

#
The getOneObjectAtOffset method looks for other objects that might be intersecting the bullet. You can just use the this keyword to refer to the acting bullet. Also, your isAtEdge method only checks for two edges (right and bottom); however, the Actor class already provides an isAtEdge method -- you do not need to write one out yourself. The following would be sufficient for what you gave above:
public void act()
{
    move(5);
    if (isAtEdge()) getWorld().removeObject(this);
}
You need to login to post a reply.