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

2018/5/24

How do you make a character with the platform

Comp Comp

2018/5/24

#
private Actor player = getOneIntersectingObject(Viking.class); 
private int speed = 3;
   public void move()
    {
        if(isAtEdge() )
        {
            if(getX() > getWorld().getWidth()/2 || getX() < getWorld().getWidth()/2 )
            {
                speed = -speed;
            }
        }
        setLocation(getX() + speed, getY() );
    }
    public void playerOn()
    {
        if(this.isTouching(Viking.class) && player != null)
        {
            player.setLocation(player.getX() + speed, player.getY() );
        }
    }
Comp Comp

2018/5/24

#
I don't understand why it doesn't work.
Comp Comp

2018/5/24

#
Sorry, typo. How do I make my character move with the platform?
danpost danpost

2018/5/24

#
Comp wrote...
I don't understand why it doesn't work.
You will never have any object assigned to player as is. The assignment is being made during the object's creation and before it is placed into a world. Also, is there an act method in the class? -- and what does it look like? Another thing is that the conditional checks at line 7 are totally unnecessary.
danpost danpost

2018/5/25

#
Comp wrote...
Sorry, typo. How do I make my character move with the platform?
The playerOn method has the right idea. Just needs a little refining.
Comp Comp

2018/5/25

#
The act method is a default implementation in Greenfoot with no body.
Comp Comp

2018/5/25

#
danpost wrote...
Comp wrote...
I don't understand why it doesn't work.
You will never have any object assigned to player as is. The assignment is being made during the object's creation and before it is placed into a world. Also, is there an act method in the class? -- and what does it look like? Another thing is that the conditional checks at line 7 are totally unnecessary.
Sorry. I don't understand what you mean by this. Can you clarify a bit more, please.
Comp Comp

2018/5/25

#
public class Ship extends Actor
{
    private int speed = 3;
    private Actor player = getOneIntersectingObject(Viking.class);
    /**
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        playerOn();
    }    
    public void move()
    {
        if(isAtEdge() )
        {
                speed = -speed;
        }
        setLocation(getX() + speed, getY() );
    }
    public void playerOn()
    {
        if(this.isTouching(Viking.class) && player != null)
        {
            player.setLocation(player.getX() + speed, player.getY() );
        }
    }
}
danpost danpost

2018/5/25

#
Comp wrote...
<< Quote Omitted >> Sorry. I don't understand what you mean by this. Can you clarify a bit more, please.
Line 4 in your last code post declares a new field called player to hold an Actor object. It also assigns to it whatever is returned by getOneIntersectingObject asking for any Viking objects. This part should fail and give an IllegalStateException error because it is executed before the newly created Ship object is placed into any world. Even if it did not fail, it would be assigned a null value never update (the assignment is only done once when the Ship object is created). So, when the Ship object does intersect a Vikiing object, player would still be null and calling setLocation on it will cause a NullPointerException error. Remove private from line 4 and move it to be the first line in your playerOn method.
Comp Comp

2018/5/25

#
Thank You!
You need to login to post a reply.