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

2015/11/29

I need help doing the healthbar.

Kullie Kullie

2015/11/29

#
/** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { GifImage myGif = new GifImage("walk1.gif"); GifImage myGif2 = new GifImage("walk2.gif"); GifImage myGif3 = new GifImage("walk3.gif"); GifImage myGif4 = new GifImage("walk4.gif"); GifImage myGif5 = new GifImage("HitV.gif"); int leben = 150; Blob blob = new Blob(); /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { laufenVerbessert(); zerstören(); bar(); } /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void walk() { if(Greenfoot.isKeyDown("w")) { setRotation(270); move(2); setImage( myGif3.getCurrentImage()); } if(Greenfoot.isKeyDown("s")) { setRotation(90); move(2); setImage( myGif4.getCurrentImage()); } if(Greenfoot.isKeyDown("a")) { setRotation(180); move(2); setImage( myGif2.getCurrentImage()); } if(Greenfoot.isKeyDown("d")) { setRotation(0); move(2); setImage( myGif.getCurrentImage()); } if("space".equals(Greenfoot.getKey())) { shoot(); } } public void shoot() { Bullet bullet = new Bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.setRotation(getRotation()); } /** * wandVorne; wandRechts; wandLinks; aus dem Roboter Szenario übernommen und an die Situation angepasst. */ public boolean rockVorne() { int lRichtung = this.getRotation(); if (lRichtung == 0 && this.getOneObjectAtOffset(1, 0, Rock.class)!= null) { return true; } if (lRichtung == 90 && this.getOneObjectAtOffset(0, 1, Rock.class)!= null) { return true; } if (lRichtung == 180 && this.getOneObjectAtOffset(-1, 0, Rock.class)!= null) { return true; } if (lRichtung == 270 && this.getOneObjectAtOffset(0, -1, Rock.class)!= null) { return true; } return false; } /** * Der Sensor ueberprueft, ob sich rechts der Laufrichtung eine Wand befindet. */ public boolean rockRechts() { int lRichtung = this.getRotation(); if (lRichtung == 0 && this.getOneObjectAtOffset(0, 1, Rock.class)!= null) { return true; } if (lRichtung == 90 && this.getOneObjectAtOffset(-1, 0, Rock.class)!= null) { return true; } if (lRichtung == 180 && this.getOneObjectAtOffset(0, -1, Rock.class)!= null) { return true; } if (lRichtung == 270 && this.getOneObjectAtOffset(1, 0, Rock.class)!= null) { return true; } return false; } /** * Der Sensor ueberprueft, ob sich links der Laufrichtung eine Wand befindet. */ public boolean rockLinks() { int lRichtung = this.getRotation(); if (lRichtung == 0 && this.getOneObjectAtOffset(0, -1, Rock.class)!= null) { return true; } if (lRichtung == 90 && this.getOneObjectAtOffset(1, 0, Rock.class)!= null) { return true; } if (lRichtung == 180 && this.getOneObjectAtOffset(0, 1, Rock.class)!= null) { return true; } if (lRichtung == 270 && this.getOneObjectAtOffset(-1, 0, Rock.class)!= null) { return true; } return false; } /** * Ein Beispiel einer Methode - ersetzen Sie diesen Kommentar mit Ihrem eigenen * * @param y (Beschreibung des Parameters) * @return (Beschreibung des Rückgabewertes) */ public boolean kannBewegen() { if(!rockVorne()) { return true; } else { return false; } } public void laufenVerbessert() { if (kannBewegen() == true) { walk(); } else { turn(180); move(100); } } public void zerstören() { if(isTouching((Blob.class))) { leben--; } } /** * Kommt aus Animal. */ public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } public int getLeben() { return leben; } public void bar() { if(leben == 100) { Healthbar healthbar = new Healthbar(); healthbar.setImage("Leben1.png"); } } WHY doesn't the Healthbar Actor image change? thx for help,
danpost danpost

2015/11/29

#
The last line of the code you posted changes the value of a Healthbar object; however, this particular Healthbar object was just created by the previous line of code. Also, this Healthbar object is never added into the active world to be visible in (not that adding this new Healthbar object into the world is something you want to do); and as soon as the 'bar' method has completed its execution (which is immediately, as there is no more code in the method), that new Healthbar object is flagged for garbage collection, as no reference to it would exist (note: adding an Actor object into a world sets up a reference to that actor in the World object it is added to). I will presume you already have a Healthbar object in your world and you are wanting its image to change. It, first off, should only be set to a new image when the value of 'leben' changes (only on the change of its value to one that requires a different image -- no every 'act' cycle). I guess what I am saying is that you should call the 'bar' method after 'lebel--;' in your 'zerstören' method; not directly from the 'act' method. For a better understanding of displaying values, refer to my Value Display Tutorial scenario.
Super_Hippo Super_Hippo

2015/11/29

#
In the 'bar' method, you create a new (!) Healthbar object. If you have one Healthbar object in the world and you want a reference to it, you can use this line instead:
Actor healthbar = (Actor) getWorld().getObjects(Healthbar.class).get(0);
Casting it to 'Actor' instead of 'Healthbar' is enough if you only use Actor methods (like 'setImage' on it). If you want to add Healthbar class methods, then you have to change 'Actor' to 'Healthbar'. Edit: I didn't see that there was already a reply.
You need to login to post a reply.