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?
/** 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);
}
}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;
}
}
}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;
}
}
}