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

2020/1/10

Varying PowerBar!

LightningFast LightningFast

2020/1/10

#
I'm making a power bar, on the side of the screen, and basically each time the spacebar is hit, a boo sound comes on (already done) and it makes the powerbar go from bottom to up. I want it to decrease over time if nothing happens. How can I do this???
LightningFast LightningFast

2020/1/10

#
I've tried creating a blank white rectangle (separate actor class) but how would I have the varying bars (changing colors towards bottom or top of powerbar)
danpost danpost

2020/1/10

#
LightningFast wrote...
I've tried creating a blank white rectangle (separate actor class) but how would I have the varying bars (changing colors towards bottom or top of powerbar)
PowerBar class codes .. so far?
LightningFast LightningFast

2020/1/10

#
import greenfoot.*; public class PowerBar extends Actor { public void act() { GreenfootImage image = new GreenfootImage(50, 500); image.setColor(Color.WHITE); image.fill(); setImage(image); } //https://www.greenfoot.org/topics/379 }
LightningFast LightningFast

2020/1/10

#
import greenfoot.*; public class Boss1 extends World { Reka r = new Reka(); Hero h = new Hero(); Pie p = new Pie(); PowerBar pb = new PowerBar(); public Boss1() { super(1000,540, 1); addObject(r, 500,50); addObject(h, 500,500); addObject(p, 475,50); addObject(pb, 950,270); } }
LightningFast LightningFast

2020/1/10

#
here's the world class if you need it...
danpost danpost

2020/1/10

#
LightningFast wrote...
I've tried creating a blank white rectangle (separate actor class) but how would I have the varying bars (changing colors towards bottom or top of powerbar)
Your power bar needs a value for how full it is. Then, you can color the correct portion of the bar depending on that value.
LightningFast LightningFast

2020/1/10

#
so a counter? can I make the fill rectangle change color and height (length)?
danpost danpost

2020/1/10

#
LightningFast wrote...
so a counter? can I make the fill rectangle change color and height (length)?
You can draw on any rectangular portion of the image or draw a separate image on it at any given location.
LightningFast LightningFast

2020/1/10

#
okay so I've tried something... and it's giving an error... Hero class: import greenfoot.*; public class Hero extends Actor { GreenfootSound booSound = new GreenfootSound("Boo.wav"); int powerAdd = 0; public void act() { checkKeys(); } public void checkKeys() { int x = getX(); if (Greenfoot.isKeyDown("space")) { booSound.play(); powerAdd++; PowerBar p = new PowerBar(); p.PowerBar(powerAdd); } if (Greenfoot.isKeyDown("left")) x=x-3; if (Greenfoot.isKeyDown("right")) x=x+3; setLocation(x,getY()); } } World Class: import greenfoot.*; public class Boss1 extends World { Reka r = new Reka(); Hero h = new Hero(); Pie p = new Pie(); PowerBar pb = new PowerBar(); public Boss1() { super(1000,540, 1); addObject(r, 500,50); addObject(h, 500,500); addObject(p, 475,50); addObject(pb, 950,270); } } PowerBar class: import greenfoot.*; public class PowerBar extends Actor {//https://www.youtube.com/embed/oJHP18bhLT0?autoplay=1 int powerAdd; public PowerBar(int powerAdd){ this.powerAdd = powerAdd; } int power = 0; int powerBarWidth = 50; int powerBarHeight = 500; int pixelsPerPowerPoint = (int)powerBarHeight/power; public void act() { GreenfootImage image = new GreenfootImage(powerBarWidth, powerBarHeight); image.setColor(Color.WHITE); image.fill(); setImage(image); image.drawRect(0,0,powerBarWidth,powerAdd); } //https://www.greenfoot.org/topics/379 }
danpost danpost

2020/1/10

#
You won't get anywhere drawing white on white. Another thing is that you have two powerAdd fields (not usually a good idea to have more than one field containing a specific value). Also, there is no need to create a new image every act step. And, you do not really need the fields for height and width. Use a constructor to create a base image and a bar image. Then you can use a combination of fill and drawImage to update the image. I am a bit confused. You are creating a PowerBar object in your Boss1 world. Then, you have your Hero object create another one each time the space is pressed -- and these are never added into the world (nor are the previous ones removed).
LightningFast LightningFast

2020/1/12

#
yea I can see the confusion... I realised I wasn't really done and didn't understand really much from the tutorial video I was watching. To be honest, I was thinking of like passing the "powerAdd" like a score system to a constructor, like using OOP. I will retry and check in if I run into any problems.
You need to login to post a reply.