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

2016/4/25

HP Decrease and Death Condition

JonSkyWalker JonSkyWalker

2016/4/25

#
Hey all, so i need help, i just build a scenario of multiplayer games so i have 2 character. and so i have 2 hp bar. but i dont know how to decrease hp bar each other. i need to decrease hp bar who success jump on other actor head. it is possible?
danpost danpost

2016/4/25

#
Each player should be assigned its own hp bar to work with. Create the hp bar for each player before creating the player. When creating the player, pass the hp bar it is to use to it. Have the player retain the hp bar in a reference field. The following will most probably need some modifications; but the idea behind what to do should be apparent:
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
27
28
29
30
31
/**   in subclass of world   */
 
// in constructor or prepare method
HpBar hpbar1 = new HpBar(); // creating healthbar
Player1 player1 = new Player1(hpBar1); // creating player
 
// repeat for player2
 
 
/**      in class of player     */
 
// instance reference field
private HpBar hpBar;
private Actor playerTouching;
 
// the constructor
public Player1(HpBar bar)
{
    hpBar = bar;
}
 
// in act or method it calls
Actor player = getOneIntersectingObject(Player2.class);
if (player != playerTouching)
{
    playerTouching = player;
    if (playerTouching != null && playerTouching.getY() < this.getY())
    {
        hpBar.setValue(hpBar.getValue()-20);
    }
}
JonSkyWalker JonSkyWalker

2016/4/26

#
i tried your code i just implement the if logic from your code : this my code look like :
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public void hitBadGuy()
    {
        Actor badguy = getOneObjectAtOffset(0, getImage().getHeight()/2+4, Veylectra.class);
//        Actor badguy = getOneIntersectingObject(Veylectra.class);
        if(badguy != null && badguy.getY() >= this.getY())
        {
            World myWorld = getWorld();
            Stage1 myworld = (Stage1)myWorld;
       HealthBar healthbar = myworld.getHealthBar();
            if(touchingBadGuy == false)
            {
                healthbar.loseHealth();
                touchingBadGuy = true;
                //Greenfoot.playSound("punch.mp3");
                if(healthbar.health <=0)
                {
                    setRotation(90);
                    Greenfoot.delay(delayAmount);
                    setRotation(180);
                    Greenfoot.delay(delayAmount);
                    setRotation(270);
                    Greenfoot.delay(delayAmount);
                    setRotation(0);
                    Greenfoot.delay(delayAmount);
                    setRotation(90);
                    Greenfoot.delay(delayAmount);
                    setRotation(180);
                    Greenfoot.delay(delayAmount);
                    //Greenfoot.playSound("youlose.mp3");
                    setRotation(270);
                    Greenfoot.delay(delayAmount);
                    setRotation(0);
                    Greenfoot.delay(delayAmount);
                    Greenfoot.delay(50);
                   // GameOver gameover = new GameOver();
                    //getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
                    //Greenfoot.playSound("gameover.mp3");
                    getWorld().removeObject(this);
                }
                 
            //}
 
        } else
        {
            touchingBadGuy = false;
    }
}
}
but everytime i hit my enemy head the hp didn't decrease, can you tell me whats wrong with my code? thanks
JonSkyWalker JonSkyWalker

2016/4/26

#
this my code look like :
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public void hitBadGuy()
    {
        Actor badguy = getOneObjectAtOffset(0, getImage().getHeight()/2+4, Veylectra.class);
//        Actor badguy = getOneIntersectingObject(Veylectra.class);
        if(badguy != null && badguy.getY() < this.getY())
        {
            World myWorld = getWorld();
            Stage1 myworld = (Stage1)myWorld;
       HealthBar healthbar = myworld.getHealthBar();
            if(touchingBadGuy == false)
            {
                healthbar.loseHealth();
                touchingBadGuy = true;
                //Greenfoot.playSound("punch.mp3");
                if(healthbar.health <=0)
                {
                    setRotation(90);
                    Greenfoot.delay(delayAmount);
                    setRotation(180);
                    Greenfoot.delay(delayAmount);
                    setRotation(270);
                    Greenfoot.delay(delayAmount);
                    setRotation(0);
                    Greenfoot.delay(delayAmount);
                    setRotation(90);
                    Greenfoot.delay(delayAmount);
                    setRotation(180);
                    Greenfoot.delay(delayAmount);
                    //Greenfoot.playSound("youlose.mp3");
                    setRotation(270);
                    Greenfoot.delay(delayAmount);
                    setRotation(0);
                    Greenfoot.delay(delayAmount);
                    Greenfoot.delay(50);
                   // GameOver gameover = new GameOver();
                    //getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
                    //Greenfoot.playSound("gameover.mp3");
                    getWorld().removeObject(this);
                }
                  
            //}
  
        } else
        {
            touchingBadGuy = false;
    }
}
}
but everytime i hit my enemy head the hp didn't decrease, can you tell me whats wrong with my code? thanks
JonSkyWalker JonSkyWalker

2016/4/26

#
the right code is the last one
danpost danpost

2016/4/26

#
You already have the Veylectra object as an Actor object called 'badguy'. You should get the healthbar from it. Something like this:
1
2
Veylectra player = (Veylectra)badguy;
HealthBar healthbar = player.healthBar;
You need to login to post a reply.