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

2019/2/24

How can I make my actor disappear when score = 0?

PostieMalonie PostieMalonie

2019/2/24

#
I've tried to code in a method that allows the actor to be removed when the score counter is equal to 0 but I can't seem to get it working.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is the boss found on the "Level3World2.class" level. 
 * The player fights this boss, when the score counter gets to 0 the boss will disappear and
 * a door will appear at the side of the level allowing the player to continue on ward.
 */
public class Boss extends Actor
{
    GifImage Gif = new GifImage("BossTransform.gif"); // Grabs GIF files and plays them when certain buttons are pressed.
    GifImage Right = new GifImage("Boss-Walking-Right.gif");
    GifImage Left = new GifImage("Boss-Walking.gif");
    private int direction = 2;
    boolean ifPlayed = false;
    int repeats = 0;   
    int score = 0;
    /**
     * After "Run" is clicked, the 'checkKeys' function constantly checks for what keys are being pressed
     * and if the key pressed has a corresponding function to it.
     */
    public void act()  //Event Loop, this is a loop as it's constantly checking for something to happen.
    {
        play();
        bossMovement();
        damageright();
        damageleft();
        // moveAround();
        // health();
        // nohealth();
    }

    /**
     * After the transforming GIF has been played, the boss will begin to move around. 
     */
    public void bossMovement()
    {
        if (ifPlayed){
            moveAround();
        }
    }   

    /**
     * This plays a GIF of a bad guy transforming, the boss will not begin to move until this GIF
     * has been polayed fully.
     */
    public void play()
    {
        if (!ifPlayed)
        {
            setImage(Gif.getCurrentImage());
            repeats++; 
            if (repeats >= 100){
                ifPlayed = true;
            }
        } 
    }

    /**
     * When the boss touches the side of the level, he will change his GIF to show him walking
     * the correct direction.
     */
    private void moveAround()
    {
        if ((direction == -2 && getX() <= 5) || (direction == 2 && getX() >= getWorld().getWidth()-5))
        {
            direction = -direction;
            if (direction == 2) setImage(Right.getCurrentImage());
            else setImage(Left.getCurrentImage());;
        }
        setLocation(getX()+direction, getY());
    }

    public void damageright()
    {       
        if (isTouching(Bullet.class))
        {
            removeTouching(Bullet.class);
            ((Level3World2) getWorld()).bosshealthdmg();
        }
    }

    public void damageleft()
    {       
        if (isTouching(LeftBullet.class))
        {
            removeTouching(LeftBullet.class);
            ((Level3World2) getWorld()).bosshealthdmg();
        }
    }
    
    // public void nohealth()
    // {
        // if (target == 0)
        // {
            // getWorld().removeObject(this);
        // }
    // }
}

// public void health()
// {
// if (isTouching(Bullet.class))
// {
// score++;
// }
// }
danpost danpost

2019/2/24

#
PostieMalonie wrote...
I've tried to code in a method that allows the actor to be removed when the score counter is equal to 0 but I can't seem to get it working. << Code Omitted >>
Whose score should be zero? -- the boss'?? It looks like you increase the boss' score when it is hit by a bullet; though, its health does go down. At least, that is what you have code for (with some commented out). The only place I see any actor being removed is in the commented out nohealth method. However, it depends on a target value, which seems to be out of nowhere (not declared or used elsewhere in the class). Where are you comparing a score counter to zero and what actor is to be removed?
PostieMalonie PostieMalonie

2019/2/24

#
The current actor called "Boss" when hit by a bullet the counter I have goes down by 10, I want the boss to be removed when the counter is equal to zero. I'm not very good at coding.
danpost danpost

2019/2/24

#
My question is this -- why is there a score field in the Boss class? and for that matter, why is not the health field IN the Boss class. A class describes an object and should contain within that class the states (as fields) and behaviors (as methods) belonging to objects created from that class.
PostieMalonie PostieMalonie

2019/2/24

#
I'm using the score counter as a simple health counter.
danpost danpost

2019/2/24

#
PostieMalonie wrote...
I'm using the score counter as a simple health counter.
Its value appears to BE zero from the start and does not appear to be adjusted to a positive health value anywhere. And if it is for boss health, why are you going to the world to impart damage (decrease health?)?
You need to login to post a reply.