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

2019/3/28

the health bar, it doest lose health when hit by the bullet

Deveshwarreddy Deveshwarreddy

2019/3/28

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Villian here. * * @author (your name) * @version (a version number or a date) */ public class Villian extends Actor { boolean touchingherobullet = false; public int villianRotation; Actor SpaceStone; Actor SoulStone; Actor PowerStone; Actor TimeStone; Actor herobullet; /** * Act - do whatever the Villian wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeyPress(); SpaceStone = getOneIntersectingObject(SpaceStone.class); MyWorld myWorld = (MyWorld)getWorld(); SoulStone = getOneIntersectingObject(SoulStone.class); if (SoulStone!=null){ myWorld.removeObject(SoulStone); myWorld.increaseScore(); } PowerStone = getOneIntersectingObject(PowerStone.class); if (PowerStone!=null){ myWorld.removeObject(PowerStone); myWorld.increaseScore(); } SpaceStone = getOneIntersectingObject(SpaceStone.class); if (SpaceStone!=null){ myWorld.removeObject(SpaceStone); myWorld.increaseScore(); } TimeStone = getOneIntersectingObject(TimeStone.class); if (TimeStone!=null){ myWorld.removeObject(TimeStone); myWorld.increaseScore(); } herobullet = getOneIntersectingObject(herobullet.class); if(herobullet !=null) { HealthBar healthbar = myWorld.getHealthBar(); if (touchingherobullet ==false) { healthbar.loseHealth(); touchingherobullet = true; if(healthbar.health <=0) { myWorld.removeObject(this); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); } } } else { touchingherobullet = false; } villianRotation = getRotation(); } public void checkKeyPress() { if (Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-5); } if (Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+5); } if (Greenfoot.isKeyDown("right")) { move(7); } if (Greenfoot.isKeyDown("left")) { move(-7); } if (Greenfoot.isKeyDown("space")) { shoot(); } } public void shoot() { getWorld() .addObject(new villianbullet(villianRotation), getX(), getY()); } public void tryToEat(){ if (isTouching(SoulStone.class)) { removeTouching(SoulStone.class); } if (isTouching(SpaceStone.class)) { removeTouching(SpaceStone.class); } if (isTouching(TimeStone.class)) { removeTouching(TimeStone.class); } if (isTouching(PowerStone.class)) { removeTouching(PowerStone.class); } } }
Super_Hippo Super_Hippo

2019/3/28

#
Show the MyWorld-code. Maybe the healthbar in your world is not the same as the one you get with the getter-method.
Deveshwarreddy Deveshwarreddy

2019/3/29

#
Counter Counter = new Counter(); HealthBar healthBar = new HealthBar(); int score = 0; /** * Constructor for objects of class Level1. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 750, 1); prepare(); } public Counter getCounter() { return Counter; } public HealthBar getHealthBar() { return healthBar; } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Hero hero = new Hero(); addObject(hero,155,507); Villian villian = new Villian(); addObject(villian,817,479); //add health bar HealthBar healthBar = new HealthBar (); addObject(healthBar,193, 234); removeObject(healthBar); HealthBar healthBar2 = new HealthBar(); addObject(healthBar2,143,39); HealthBar healthBar3 = new HealthBar(); addObject(healthBar3,815,51); healthBar3.setLocation(794,37); healthBar3.setLocation(803,37); PowerStone powerStone = new PowerStone(); addObject(powerStone,51,460); SoulStone soulStone = new SoulStone(); addObject(soulStone,50,562); act(); Counter.act(); SpaceStone spaceStone = new SpaceStone(); addObject(spaceStone,57,305); TimeStone timeStone = new TimeStone(); addObject(timeStone,50,369); } public void act(){ showText("infinity stones =" + score, 459, 40); } public void increaseScore(){ score = score + 1; } }
danpost danpost

2019/3/29

#
Remove the following line from the prepare method:
HealthBar healthBar = new HealthBar();
You need to login to post a reply.