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?


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 ); } } |
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 ; } } } |
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 ; } } } |