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

2014/9/15

Link between actors

1
2
danpost danpost

2014/9/16

#
Sorry, 'b' will add a SpecialDoor object into the world. Insert the following line at 55:
1
Switche.specialdoor = (SpecialDoor)actor;
OR replace line 54 with this:
1
2
Switche.specialdoor = new SpecialDoor();
actor = Switche.specialdoor;
VideoGame VideoGame

2014/9/16

#
It works perfectly, thank you. Another question if you can answer it: This is the code of the trap:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Pikes extends Platform
{
    Player player = new Player();
    /**
     * Act - do whatever the Pikes wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        removePlayerByPikes();// Add your action code here.
    }
     
    public void removePlayerByPikes()
    {
        Actor player = getOneIntersectingObject(Player.class );      
        {
            getWorld().removeObject(player);// l'image du kunai est removed
            World Level = getWorld();
            GameOver gameover = new GameOver();
            Level.addObject (gameover, Level.getWidth()/2, Level.getHeight()/2);
            Greenfoot.stop(); 
        }
    }
}
and this is the part of code of the player who interacts with the 'boat':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void  onBoat()
    {
        if (isTouching(Boat.class) || isTouching(InvisibleBoat.class))
        {
            onBoat = true;
            if ( direction == 1)
            {
                setImage(boat1);               
            }     
            if ( direction == -1)
            {
                setImage(boat2);
            }
        }
        else
        {
            onBoat = false;
        }
    }
How I can immunize the player when onBoat = true ?
danpost danpost

2014/9/16

#
I would change the name of 'onBoat' to 'controlBoat', written as follows (change the call in the act to this):
1
2
3
4
5
6
7
8
9
10
11
12
13
public void controlBoat()
{
    if (onBoat())
    {
        if (direction == 1 && getImage() == boat2) setImage(boat1);
        if (direction == -1 && getImage() == boat1) setImage(boat2);
    }
}
// with this as the 'onBoat' method
public boolean onBoat()
{
    return isTouching(Boat.class) || isTouching(InvisibleBoat.class);
}
Now, you are missing an 'if' line for the Pike class code block, lines 16 through 22. With the two distinct parts above separated, you can use this condition:
1
if (player != null && !((Player)player).onBoat())
You can remove line 3 above in the Pike class.
Super_Hippo Super_Hippo

2014/9/16

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Pikes extends Platform
{
    //Player player = new Player(); <- this line isn't needed
    public void act()
    {
        removePlayerByPikes();
    }
     
    public void removePlayerByPikes()
    {
        Player player = (Player) getOneIntersectingObject(Player.class);      
        {
            if (player != null)
            {
                if (!player.getOnBoat())
                {
                    getWorld().removeObject(player);// l'image du kunai est removed
                    World Level = getWorld();
                    GameOver gameover = new GameOver();
                    Level.addObject (gameover, Level.getWidth()/2, Level.getHeight()/2);
                    Greenfoot.stop();
                }
            }
        }
    }
}
1
2
3
4
5
6
7
//in the Player class:
private Boolean onBoat; //you probably already have this
 
public Boolean getOnBoat()
{
    return onBoat;
}
Edit: Ok, someone was faster ;)
VideoGame VideoGame

2014/9/16

#
Perfect, thanks to you two
danpost danpost

2014/9/16

#
FYI, the changes Super_Hippo suggested will work also. I do prefer not to introduce excess fields, such as 'boolean onBoat', for current states that can be determined by immediate method calls (see line 12 in my last code post). By reducing the number of fields in your classes, you can make coding easier (less you have to think about or deal with) and reduce problems due to incorrect values being stored in your fields. With my above suggestion, you should be able to remove the 'boolean onBoat' field (I was going to edit my last post to inform you of this).
VideoGame VideoGame

2014/9/16

#
I use yours danpost but with modifications, the set image didn't work. The image remained the same even he was immunize. I just remove the " && getImage....)"
danpost danpost

2014/9/16

#
VideoGame wrote...
I use yours danpost but with modifications, the set image didn't work. The image remained the same even he was immunize. I just remove the " && getImage....)"
Ok. Use this instead:
1
2
3
4
5
6
7
8
public void controlBoat()
{
    if (onBoat())
    {
        if (direction == 1 && getImage() != boat1) setImage(boat1);
        if (direction == -1 && getImage() != boat2) setImage(boat2);
    }
}
VideoGame VideoGame

2014/9/16

#
Perfect, thanks !
You need to login to post a reply.
1
2