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

2015/4/7

Spawning Boss Glitch

Programmer1998 Programmer1998

2015/4/7

#
I have a code where I spawn my Boss when my player has collected 25 coins, but there is a glitch when the Boss appears in the game? I'm not really sure what went wrong! Can someone check if there is anything wrong with my code?
    public void spawnBoss()
    {
        if(Mario.coinsCollected_ == 20)
        {
            addObject(bar, 552, 127);
            Boss boss = new Boss();
            addObject(boss, 1000, 340);
            bossMusic.playLoop(); //starts the new music 
            Music.stop(); //stops the original music to play bossfight music
        }
    }
winnerpig winnerpig

2015/4/7

#
What's wrong with your program?Can you describe the error information in detail?
Programmer1998 Programmer1998

2015/4/7

#
Whenever I collect the amount of coins thats needed for the boss to spawn, the boss appears, but is very glitchy. The boss starts moving incredibly slowly and delays the entire game. There is something like a black shadow that moves forward before the actual Boss moves which makes it very slow.. sorry that's as best as I can describe it. I'm not sure where in the code I went wrong!
winnerpig winnerpig

2015/4/7

#
could you post the code of Boss class ?
Programmer1998 Programmer1998

2015/4/7

#
Here you go:
import greenfoot.*;

/**
 * The final enemy Mario has to overcome to win the game.
 * Is spawned after Mario collects 25 coins.
 * 
 * @author (Cathy Liu) 
 * @version (06/04/2015)
 */
public class Boss extends AnimatedActor
{
    private int speed_;
    private double spawn_;
    private int orbTimer_;
    public static int health_ = 20;
    private int yDir = 1;
   
    public Boss()
    {
        super();
        speed_ = 2;
        health_ = 20;
        spawn_ = 0.009;
        orbTimer_ = 0;
    }
    
    /**
     * Shows the speed in which the boss is moving.
     * @return boss' speed
     */
    public int getSpeed()
    {
        return speed_;
    }
    
    /**
     * Shows how much health the Boss still has.
     * @return Boss' health.
     */
    public int getHealth()
    {
        return health_;
    }
    
    /**
     * Act - do whatever the Boss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        super.act();
        boundary();
        spawn();
        animateBossOpen();
    }   
    
    /**
     * The movements and boundaries of the Boss.
     * When reaching X coordinate of 716, the Boss only moves up and down.
     * Code credit to danpost on greenfoot.org
     */
    public void boundary() 
    {
        //this code allows the boss to move up and down on the y-axis within a boundary
        if (getX() > 716)// if X is larger than 716, the Boss can still move
        { 
            move(-speed_);
        }
        else
        { // move vertically
            setLocation(getX(), getY()+yDir*speed_);
            if ((yDir > 0 && getY() >= 465) || (yDir < 0 && getY() <= 237))
            { // reverse directions
                yDir = -yDir;
            }
        }
    }
    
    /**
     * The Boss spawns orbs that attacks Mario.
     */
    public void spawn()
    {
        if(Math.random() < spawn_)
        {   
            animateBossOpen();
            
            GreenfootImage BossImg = new GreenfootImage("boss0.gif");
            World w = getWorld();
            
            Orb orb = new Orb(w.getWidth() + BossImg.getWidth()/2, getY());
            w.addObject(orb, getX() - BossImg.getWidth()/2 - getImage().getWidth()/2, getY());
            
            orbTimer_ = 12;
            Greenfoot.playSound("orbfire.wav");
        }
    }
}
danpost danpost

2015/4/7

#
When you experience this, did you try pausing it and moving the boss around manually? You may be creating a multitude of bosses, one on top of another.
Programmer1998 Programmer1998

2015/4/7

#
Yes, I believe that's what I'm experiencing, but I can't seem to fix it. I've tried manually adding in the boss, and it works perfectly fine that way!
danpost danpost

2015/4/7

#
Programmer1998 wrote...
Yes, I believe that's what I'm experiencing, but I can't seem to fix it. I've tried manually adding in the boss, and it works perfectly fine that way!
Check the other discussion thread.
You need to login to post a reply.