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

2016/10/10

How do I make my life meter animate?

ginger--ale ginger--ale

2016/10/10

#
I'm working on a project that involves a life meter - nothing complicated, just a switch of images whenever the character loses a life (-1 life shows an image of 4 hearts rather than 5) - but it doesn't want to do it. I've already done the lower-case class entry, since I'd forgotten it before, but it still doesn't want to do it. Any help?
danpost danpost

2016/10/10

#
ginger--ale wrote...
I'm working on a project that involves a life meter - nothing complicated, just a switch of images whenever the character loses a life (-1 life shows an image of 4 hearts rather than 5) - but it doesn't want to do it.
My Value Display Tutorial scenario may be of help here. First, look at 'Using a basic Actor object to understand the 'SimpleActor' class. Then, look at 'Non-textual representations of a value' with interest in 'A simple counting set object'.
ginger--ale ginger--ale

2016/10/13

#
I am on a school computer, which doesn't allow me to look at the file you gave me.
danpost danpost

2016/10/13

#
ginger--ale wrote...
I am on a school computer, which doesn't allow me to look at the file you gave me.
Alright. I will just explain what probably is the best way to go about making a life counter that uses an image that repeats. I think the best way is to have an Actor subclass that builds one image that the life image is drawn on the value of life times. For example, you can make the image the height of one life image and the width of one life image times the maximum number of lives. Then use the value of the lives field when updating the image (clear and redraw the life image the new number of times across the image of the actor). I have a support class for this; but, it is not documented. Nor is it anywhere in any of my scenarios.
ginger--ale ginger--ale

2016/10/20

#
public void removeLife() { lives--; MyWorld myworld = (MyWorld)getWorld(); Life life = myworld.getLife(); if(lives == 4) { life.setImage("life4.png"); setLocation(317, 28); } if(lives == 3) { life.setImage("life3.png"); setLocation(317, 28); } if(lives == 2) { life.setImage("life2.png"); setLocation(317, 28); } if(lives == 1) { life.setImage("life1.png"); setLocation(317, 28); } if(lives == 0) { life.setImage("life0.png"); dead(); Greenfoot.stop(); } } i have this code for it, and my teacher said it should work, but it isn't.
danpost danpost

2016/10/20

#
Maybe the issue is in your MyWorld class. Please post its code (from imports to final closing curly bracket).
ginger--ale ginger--ale

2016/10/20

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author Eli Hart * @version 10.6.2016 */ public class MyWorld extends World { private int currentLevel = 1; private Lily lily = new Lily(); private Life life = new Life(); private int x = Greenfoot.getRandomNumber(560); private int x1 = Greenfoot.getRandomNumber(560); private int x2 = Greenfoot.getRandomNumber(560); private int y = Greenfoot.getRandomNumber(440); private int y1 = Greenfoot.getRandomNumber(440); private int y2 = Greenfoot.getRandomNumber(440); GreenfootSound sound = new GreenfootSound("music02.mp3"); private GreenfootImage bg0 = new GreenfootImage("background0.png"); private GreenfootImage bg1 = new GreenfootImage("background1.png"); private GreenfootImage bg2 = new GreenfootImage("background2.png"); private GreenfootImage bg3 = new GreenfootImage("background3.png"); private GreenfootImage bg4 = new GreenfootImage("background4.png"); private GreenfootImage bg5 = new GreenfootImage("background5.png"); private GreenfootImage bg6 = new GreenfootImage("background6.png"); private GreenfootImage bg7 = new GreenfootImage("background7.png"); private GreenfootImage bg8 = new GreenfootImage("background8.png"); private GreenfootImage bg9 = new GreenfootImage("background9.png"); /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x480 cells with a cell size of 1x1 pixels. super(600, 480, 1); startUp(); } //place where the stuff for the first level/start up goes private void startUp() { setBackground(bg0); addObject(lily, 317, 28); addObject(life, 543, 15); addObject(new Monster1(), 48, 450); addObject(new Bear(), 337, 221); addObject(new Bear(), 481, 68); addObject(new Bear(), 125, 102); addObject(new Exit(), 557, 428); lily.sound1.stop();//stops music playing in Lily tab //sound.playLoop();//plays music over and over and over and--- } //sets up the things needed for each new level public void setUpLevel() { if(currentLevel == 2) { setBackground(bg1); addObject(new Monster1(), 319, 444); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 60; } if(x1 == x2) { x2 = 60; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 3) { setBackground(bg2); addObject(new Monster0(), 524, 442); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 60; } if(x1 == x2) { x2 = 40; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 4) { setBackground(bg3); addObject(new Monster2(), 164, 192); addObject(new Monster0(), 428, 393); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 60; } if(x1 == x2) { x2 = 40; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 5) { setBackground(bg4); addObject(new Monster2(), 164, 192); addObject(new Monster2(), 428, 393); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 40; } if(x1 == x2) { x2 = 60; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 6) { setBackground(bg5); addObject(new Monster2(), 164, 192); addObject(new Monster3(), 428, 393); addObject(new Monster2(), 164, 192); addObject(new Monster3(), 428, 393); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 40; } if(x1 == x2) { x2 = 60; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 7) { setBackground(bg6); addObject(new Monster3(), 164, 192); addObject(new Monster3(), 428, 393); addObject(new Monster4(), 420, 390); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 40; } if(x1 == x2) { x2 = 60; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 8) { setBackground(bg7); addObject(new Monster4(), 164, 192); addObject(new Monster4(), 428, 393); addObject(new Monster5(), 420, 390); addObject(new Monster5(), 420, 390); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 40; } if(x1 == x2) { x2 = 60; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 9) { setBackground(bg8); addObject(new Monster5(), 164, 192); addObject(new Monster5(), 428, 393); addObject(new Bear(), x, y); addObject(new Bear(), x1, y1); addObject(new Bear(), x2, y2); if(x == x1) { x1 = 50; } if(x == x2) { x2 = 40; } if(x1 == x2) { x2 = 60; } if(y == y1) { y1 = 30; } if(y == y2) { y2 = 60; } if(y1 == y2) { y2 = 40; } } if(currentLevel == 10) { setBackground(bg9); showText("Congrats. You Won. Please Leave.",317, 68); addObject(new Banner(), 306, 235); addObject(new Confetti(), 295, 133); addObject(new Bear(), 328, 319); addObject(new Bear(), 314, 350); addObject(new Bear(), 340, 350); addObject(new Bear(), 328, 379); addObject(new Bear(), 292, 379); addObject(new Bear(), 362, 379); addObject(new Bear(), 274, 406); addObject(new Bear(), 309, 406); addObject(new Bear(), 344, 406); addObject(new Bear(), 379, 406); addObject(new Bear(), 256, 435); addObject(new Bear(), 289, 435); addObject(new Bear(), 326, 435); addObject(new Bear(), 360, 435); addObject(new Bear(), 396, 435); sound.stop(); Greenfoot.stop(); } } //increases level by one public void increaseLevel() { currentLevel++; setUpLevel(); } //helps the monsters chase you public Lily getLily() { return lily; } //grabs the life thing and HOPEFULLY ANIMATES(it probably won't) //SIDENOTE: it didn't animate. at all. public Life getLife() { return life; } } here you go, don't mind my little side comments there.
danpost danpost

2016/10/20

#
Okay. I have determined that the Life object in the world is indeed the one that you are accessing in the 'removeLife' method in the other class. Now, let us look at the Life class. It could also be that 'removeLife' is not being called from anywhere (or the condition on it are too strict); but, I hope that is not the case.
ginger--ale ginger--ale

2016/10/21

#
OH MY GOD i just found the reason behind it not animating Life had an image set of "lifefull.png" in its act method, which was disabling the animation
ginger--ale ginger--ale

2016/10/21

#
it works now, thank you
You need to login to post a reply.