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

2016/3/25

Made shield bar, when added to world through another class it sort of bugs out.

Jalapena42 Jalapena42

2016/3/25

#
When the shieldEnergyBlue class is added to the world through the selection menu, it functions normally. However when it is added through my shipblue class it skips frames and energy and doesn't work, I have no idea what's wrong. Here's the code for my shipblue.class.
import greenfoot.*;

/**
 * Write a description of class shipred here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class shipblue extends Actor
{
    private GreenfootImage blueMoving = new GreenfootImage("shipbluemoving.png");
    private GreenfootImage shipblue = new GreenfootImage("shipblue.png");
    private int shotTimer = 0;
    private int rotate = 0;
    private int onspawn = 0;
    public static int bluewin = 0;
    private int winframes = 0;
    public static GreenfootSound laser = new GreenfootSound("laser.wav");
    private GreenfootSound engine = new GreenfootSound("engine.wav");
    private GreenfootSound winSFX = new GreenfootSound("victory.wav");
    public static boolean shieldBlue = false;
    public boolean ShieldBlue(){
        return shieldBlue;
    }

    public GreenfootSound laser(){
        return laser;
    }

    public int BlueWin(){
        return bluewin;
    }

    /**
     * Act - do whatever the shipred wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        if (shotTimer < 30){
            shotTimer++;

        }
        spawn();
        movement();
        destroy();
        win();
        shield();

    }

    public void movement()
    {
        if (countdown.begin == 1){
            if (Greenfoot.isKeyDown("8"))
            {
                move(3);
                setImage(blueMoving);
                engine.setVolume(55);
                engine.play();
            }
            else {
                setImage(shipblue);
                engine.stop();
            }
            if (Greenfoot.isKeyDown("4"))
            {
                turn(-3);
            }
            if (Greenfoot.isKeyDown("6"))
            {
                turn(3);
            }
            if (shotTimer == 30){
                if (Greenfoot.isKeyDown("enter"))
                {
                    shooting();
                }

            }
        }
    }

    public void shooting(){
        getWorld().addObject(new blueBullet(getRotation()),getX(), getY());
        laser.setVolume(75);
        laser.play();
        shotTimer = 0;

    }

    private void spawn(){
        if (onspawn == 0){  
            setRotation(180);
            onspawn++;
        }
    }

    private void destroy(){
        if (blueHealth.blueHP == 0){
            Actor explosion = new explosion();
            shipred.redwin++;
            getWorld().addObject(new explosion(), getX(), getY());
            getWorld().removeObject(this);
        }
    }

    private void win(){
        if (bluewin == 1){
            winframes++;
            winSFX.setVolume(45);
            winSFX.play();
            if (winframes == 30){
                blueWin bluewin = new blueWin();
                playAgain playagain = new playAgain();
                menu menu = new menu();
                getWorld().addObject(bluewin,300,200);
                getWorld().addObject(playagain,300,320);
                getWorld().addObject(menu,300,370);
                getWorld().removeObject(this);
            }
        }
    }
    
    private void shield(){
        if(shieldBlue == true){
            Actor shield = new shieldEnergyBlue();
            getWorld().addObject(new shieldEnergyBlue(),540,55);
            
        }
    }
}
Here's the code for my energy powerup class (makes shieldBlue = true)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class shieldPowerup here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class shieldPowerup extends Actor
{
    /**
     * Act - do whatever the shieldPowerup wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GreenfootImage image = new GreenfootImage("shieldPowerup.png");
        image.scale(25,25);
        setImage(image);
        if(isTouching(shipblue.class)){
            shipblue.shieldBlue = true;
            shieldEnergyBlue.energy = 100;
            shieldEnergyBlue.drain = 0;
            getWorld().removeObject(this);
            
        }
    }    
}
Heres the code for the shieldEnergyBlue
import greenfoot.*;
import java.util.*;
import java.awt.*;

/**
 * Write a description of class shieldEnergy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class shieldEnergyBlue extends Actor
{
    public static int energy = 100;
    public static int drain = 0;
    private int frame = 0;
    public int Energy(){
        return energy;
    }

    public int Drain(){
        return drain;
    }

    public void act() 
    {
        shield();
    }

    public void shield(){

        drain++;
        if(energy >= 0 && drain%10 == 0){

            String nmbr= "" + energy;
            setImage("shieldEnergy/" + nmbr + ".png");
            energy--;
        }
        if(energy == 0){
            getWorld().removeObject(this);
            shipblue.shieldBlue = false;
        }

    }
}
When the energy bar is added from the shipblue class when shieldBlue == true it sort of bugs out. If you wish to see exactly what I mean, go to my scenario and add the power up somewhere on the screen when the game starts, then make blue collect the power up(control blue with "8,4,and 6" on the num pad.) http://www.greenfoot.org/scenarios/16251 P.S. yes I made 100 different images for all 100 phases of the shield energy. P.S.S Also there are some sound problems that have been created since i've switched from wav to mp3. Any help is appreciated
Super_Hippo Super_Hippo

2016/3/25

#
Why do you have so many variables static? For example in the 'shieldEnergyBlue' class which should start with an uppercase letter (like every class). Every shield has its own 'energy' and 'drain' field. If you have it static, every shield is sharing the same variable.
Jalapena42 Jalapena42

2016/3/25

#
You're right Super_Hippo, i',m fairly new to Greenfoot and coding in general so I don't know much of the do's and don'ts. I changed them to non-static and that seems to have solved the problem of it skipping frames, but now the animation of the bar decreasing only begins when my redbullet class touches shipblue while shieldBlue =true. I want the animation to begin as soon as I pick up the shield power up. Red Bullet code:
import greenfoot.*;

/**
 * Write a description of class redBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class redBullet extends Actor
{
    private int direction;
    private int speed;
    private GreenfootSound hit = new GreenfootSound("hit.mp3");
    /**
     * Act - do whatever the redBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public redBullet(int dir)
    {
        direction = dir;
        speed = 15;
    }

    public void act()
    {
        setRotation(direction);
        move(speed);
        if (isAtEdge())
        {
            getWorld().removeObject(this);
            return;
        }
        if(shipblue.shieldBlue == false){
            if (isTouching(shipblue.class)){
                blueHealth.blueHP--;
                hit.setVolume(90);
                hit.play();
                getWorld().removeObject(this);
            }
        }
        if(shipblue.shieldBlue == true){
            if (isTouching(shipblue.class)){
                shipblue.shieldBlue = false;
                hit.setVolume(90);
                hit.play();
                getWorld().removeObject(this);
            }
        }
    }

}
Super_Hippo Super_Hippo

2016/3/26

#
Okay, let's remove every 'static'. Control the intersecting between objects in the class which is the reason for the intersection. So the ship touches a power up and not the other way around. In your game, I don't see a shield yet for some reason. For the bullet, the intersection code could look like this:
ShipBlue ship = (ShipBlue) getOneIntersectingObject(ShipBlue.class); //change classname to start with uppercase letter
if (ship != null)
{
    if (ship.shield) ship.removeShield();
    else ship.removeHealth();
    //play sound
    getWorld().removeObject(this);
}
By the way, you don't need the rotation variable. Remove lines 11+21 and move line 27 to 21. Since the speed is always 15, you don't have to set it in the constructor. You can do this in line 12. I have a question. It looks like the shield is destroyed after it absorbs a hit (line 44) or after the time ran out (shieldEnergyBlue class). The shield itself doesn't get removed when it was hit though. How is the creating of power up objects and the shield itself related to each other? Another thing is the shield-method in the shipblue class. Why are you adding new shieldEnergyBlue objects until shieldBlue isn't true anymore which is only when it got hit? That is causing the problem you are facing right know. When there is a shield, pause the scenario and drag the shield. You will see that there are many shields over each other.
danpost danpost

2016/3/26

#
I wrote a Value Display Tutorial scenario that ends with display values in non-textual ways, including a bar. Sample code is also included.
You need to login to post a reply.