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

2021/1/13

help boss crashes game

1
2
Sakuya Sakuya

2021/1/13

#
I dont know why the boss crashes the game but he somehow does if anyone could help me I´d apreaciat that, and also therse another thing I need help with at public void spawnBul 1 and 2 I wrote an // before redKnife because it shows me an error when its in use I included the code redKnife too.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sakuya here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sakuya extends Actor
{
    private int timer = 0;
    int health = 2000;
    private Counter counter;
    private redShot bulSpawn2, bulSpawn3, bulSpawn4;
    private Powerup[] pw = new Powerup[5];
    private redKnife[] bul = new redKnife[50];
    private SPortrait portrait;
    private GreenfootSound sakuyaSnd = new GreenfootSound("sakuyaBattle.mp3");
    private GreenfootSound stageSnd = new GreenfootSound("scarletMansion.mp3");
    private GreenfootSound winSnd = new GreenfootSound("winSound.mp3");
    private GreenfootSound bulfire = new GreenfootSound("enemybullet.wav");
    private GreenfootSound timerSnd = new GreenfootSound("countdown.wav");
    private GreenfootSound spellActSnd = new GreenfootSound("spellactivate.wav");
    private GreenfootSound spellCapSnd = new GreenfootSound("spellcapture.wav");
    private GreenfootSound bossDeathSnd = new GreenfootSound("bossDefeat.wav");
    private GreenfootSound deathSnd = new GreenfootSound("bossDefeat.wav");
    private boolean dead = false;
    private int steps = 20;
    private int invulnTimer = 0;
    private int destinX, destinY, distX, distY;
    private int patternTog = 1;
    private int spawnTog = 1;
    private int bossState = 1;
    private int animTimer = 0;
    private boolean start = false;
    private boolean shotsFired = false;
    private boolean spellActive = false;
    
    /**
     * Act - do whatever the Sakuya wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
           if(getY() < 150)
           {
               setLocation(getX(), getY() + 2);
           }
           if (invulnTimer <= 150)
           {
                invulnTimer++;
            }
            else
            {
                SakuyasWorld sk =(SakuyasWorld)getWorld();
                timer = 0;
                invulnTimer = 0;
                counter = new Counter();
                sk.addObject(counter, 510, 40);
                counter.setValue(30);
                stageSnd.stop();
                sakuyaSnd.playLoop();
                start = true;
            }
        
        if (start == true && dead == false)
        {
            if (bossState == 1)
            {
              if (timer%25 == 0)
                {
                    //Switch between 2 bullet patterns
                    if (patternTog == 1)
                    {
                        bulfire.play();
                        spawnBul();
                    }
                    if (patternTog == 2)
                    {
                        bulfire.play();
                        spawnBul2();
                    }
                }
                //Generates new destination target
                if (timer == 150)
                {
                    gen();
                    if (patternTog == 1)
                    {
                        patternTog = 2;
                    }
                    else
                    {
                        patternTog = 1;
                    }
                    timer = 0;
                }
                //Shifts to a new location in 16 moves
                if (steps < 16)
                {
                    setLocation(getX() + advX(), getY() + advY());
                }
                hitCheck();
                hpCheck();
                steps++;
                if (counter.getValue() == 0)
                {
                    health = 1500;
                }
                if (health <= 1500)
                {
                    bossState = 2;
                    counter.setValue(40);
                    bulfire.stop();
                }
                    if (timer < 60){
                        move(1);
                    }
                    if (timer >= 60 && timer < 120){
                        setLocation(getX(), getY() + 1);
                    }
                    if (timer >= 120 && timer < 180){
                        move(-1);
                    }
                    if (timer >= 180 && timer < 240){
                        setLocation(getX(), getY() - 1);
                    }
                    if (timer == 240){
                        timer = 0;
                    }
                    hitCheck();
                    hpCheck();
                    steps++;               
                }
            }
            timer++;
            if (timer%50 == 0){
                counter.setValue(counter.getValue() - 1);
                if (counter.getValue() <= 10){
                    timerSnd.play();
                }
            }     
        if (dead == true){
            if (animTimer == 1){
                bulfire.stop();
                deathSnd.play();
                setImage("explode1.png");
            }
            if (animTimer == 6){
                setImage("explode2.png");
            }
            if (animTimer == 11){
                setImage("explode3.png");
            }
            if (animTimer == 15){
                setImage("explode4.png");
            }
            if (animTimer == 19){
                SakuyasWorld sk =(SakuyasWorld)getWorld();               
                sk.removeObject(counter);
                sk.removeObject(this);
                sk.addScore(20000);
                bossDeathSnd.play();
                WinScreen win = new WinScreen();
                sk.addObject(win, 277, 258);
                sakuyaSnd.stop();
                winSnd.play();
                Greenfoot.stop();
            }
            animTimer++;
        
             
        }    
    }
      public void hpCheck()
      {
        if (health <= 0){
            SakuyasWorld sk =(SakuyasWorld)getWorld();
            sk.setScore(sk.getScore() + 99999);
            dead = true;
        }
      }
       public int getHealth()
      {
        return health;
      }
      public void spellLoc(){
        destinX = 277;
        destinY = 166;
        steps = 0;
    }
       public void spawnBul()
    {
        Actor reimu = (Actor)getWorld().getObjects(Reimu.class).get(0);
        SakuyasWorld sk =(SakuyasWorld)getWorld();
        for (int i = 0; i < 7; i++)
        {
           // bul[i] = new redKnife();
            sk.addObject(bul[i], getX(), getY());
            bul[i].setImage("cherryknife.png");
            bul[i].turnTowards(reimu.getX(), reimu.getY());
        }
        bul[0].turn(45);
        bul[1].turn(30);
        bul[2].turn(15);
        bul[4].turn(-15);
        bul[5].turn(-30);
        bul[6].turn(-45);
      }
     public void spawnBul2()
     {
        SakuyasWorld sk =(SakuyasWorld)getWorld();
        for (int i = 0; i < 10; i++){
            //bul[i] = new redKnife();
            sk.addObject(bul[i], getX(), getY());
            bul[i].setImage("cherryknife.png");
        }
        bul[0].setRotation(40);
        bul[1].setRotation(80);
        bul[2].setRotation(120);
        bul[3].setRotation(160);
        bul[4].setRotation(200);
        bul[5].setRotation(240);
        bul[6].setRotation(280);
        bul[7].setRotation(320);
        bul[8].setRotation(360);
    }
      public void gen(){
        destinX = Greenfoot.getRandomNumber(250) + 50;
        destinY = Greenfoot.getRandomNumber(100) + 50;
        steps = 0;
    }
    public int advX()
    {  
        distX = destinX - getX();        
        return distX/15;
    }
    public int advY()
    {
        distY = destinY - getY();
        return distY/15;
    }
     public void hitCheck()
    {
        Actor Shot = getOneIntersectingObject(Shot.class);
        if (Shot != null)
        {
          health = health - 1;
          getWorld().removeObject(Shot);
        }
    }

}

}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class redKnife here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class redKnife extends Bullet
{
     int speedy = 3;
    int speedx = 3;
    /**
     * Act - do whatever the redKnife wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
       public redKnife(int x , int y)
    {
        speedx = x;
        speedy = y;
    }
      public void act() 
    {
       setLocation(getX()+speedy, getY()+speedy);
       checkCollission();
    }     
    public void setSpeedx(int pSpeedx)
    {
        speedx = pSpeedx;
    }
    public void checkCollission()
    {
       if(isAtEdge())
       {
           getWorld().removeObject(this);
       }
    }
}
danpost danpost

2021/1/13

#
Remove lines 17 thru 21 in redKnife class.
Sakuya Sakuya

2021/1/14

#
Thanks that worked, but the boss still crashes the game when he spawns (pauses the game and makes it impossible to continue)
danpost danpost

2021/1/14

#
Sakuya wrote...
the boss still crashes the game when he spawns (pauses the game and makes it impossible to continue)
Clear terminal output, run to crash and copy/paste terminal output here.
Sakuya Sakuya

2021/1/14

#
java.lang.NullPointerException (red) at Sakuya.act(Sakuya.java:138) (red) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) this?
danpost danpost

2021/1/14

#
Change line 53 to:
else if (start == false)
Sakuya Sakuya

2021/1/14

#
still dosnt seam to work
danpost danpost

2021/1/14

#
Put the following line both after lines 47 and 51:
return;
Sakuya Sakuya

2021/1/14

#
It worked, it dosnt stop the programm while entering the screen but now it stops the game after appearing and dosnt proceed shoting
danpost danpost

2021/1/14

#
Sakuya wrote...
It worked, it dosnt stop the programm while entering the screen but now it stops the game after appearing and dosnt proceed shoting
Repeat the terminal process above.
Sakuya Sakuya

2021/1/14

#
java.lang.NullPointerException at Sakuya.act(Sakuya.java:140) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2021/1/14

#
Try replacing (rearranging) lines 45 thru 52 with the following:
if (invuTimer < 150)
{
    if (getX() < 150) setLocation(getX(), getY()+2);
    invuTimer++;
    return;
}
Sakuya Sakuya

2021/1/14

#
when I use the code the boss stays at the top of the screen and gives me this error, if I keep the code the boss gets in his place and the same error shows up. Idk why but now its instantly shows the error without the pausing, maybe its cus i left the programm open too long. java.lang.NullPointerException at Sakuya.act(Sakuya.java:140) (thats the code) counter.setValue(counter.getValue() - 1); at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2021/1/14

#
Show class again.
Sakuya Sakuya

2021/1/14

#
So I did some work on the code it dosnt crash anymore but it dosnt continue with the code after the boss apeard. Also I tested some stages before and in bossStage1 the boss shots all shots in one pille
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sakuya here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sakuya extends Actor
{
    private int timer = 0;
    int health = 2000;
    private Counter counter;
    private BulletSpawner bulSpawn, bulSpawn2, bulSpawn3, bulSpawn4;
    private Powerup[] pw = new Powerup[5];
    private Bullet[] bul = new Bullet[50];
    private SPortrait portrait;
    private GreenfootSound sakuyaSnd = new GreenfootSound("sakuyaBattle.mp3");
    private GreenfootSound stageSnd = new GreenfootSound("scarletMansion.mp3");
    private GreenfootSound winSnd = new GreenfootSound("winSound.mp3");
    private GreenfootSound bulfire = new GreenfootSound("enemybullet.wav");
    private GreenfootSound timerSnd = new GreenfootSound("countdown.wav");
    private GreenfootSound spellActSnd = new GreenfootSound("spellactivate.wav");
    private GreenfootSound spellCapSnd = new GreenfootSound("spellcapture.wav");
    private GreenfootSound bossDeathSnd = new GreenfootSound("bossDefeat.wav");
    private GreenfootSound deathSnd = new GreenfootSound("bossDefeat.wav");
    private boolean dead = false;
    private int steps = 20;
    private int invulnTimer = 0;
    private int destinX, destinY, distX, distY;
    private int patternTog = 1;
    private int spawnTog = 1;
    private int bossState = 1;
    private int animTimer = 0;
    private boolean start = false;
    private boolean shotsFired = false;
    private boolean spellActive = false;
    
    /**
     * Act - do whatever the Sakuya wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
         Actor Shot = getOneIntersectingObject(Shot.class);
            if (Shot != null)
           {
                health = health - 1;
                getWorld().removeObject(Shot);
           }
           if(getY() < 150)
           {
               setLocation(getX(), getY() + 2); 
               return;
           }
           if (invulnTimer <= 150)
           {
                invulnTimer++;
                return;
            }
            else if(start = false)
            {
                SakuyasWorld sk =(SakuyasWorld)getWorld();
                timer = 0;
                invulnTimer = 0;
                counter = new Counter();
                sk.addObject(counter, 510, 40);
                counter.setValue(30);
                stageSnd.stop();
                sakuyaSnd.playLoop();
                start = true;
            }
        
        if (start == true && dead == false)
        {
            if (bossState == 1)
            {
              if (timer%25 == 0)
                {
                    //Switch between 2 bullet patterns
                    if (patternTog == 1)
                    {
                        bulfire.play();
                        spawnBul();
                    }
                    if (patternTog == 2)
                    {
                        bulfire.play();
                        spawnBul2();
                    }
                }
                //Generates new destination target
                if (timer == 150)
                {
                    gen();
                    if (patternTog == 1)
                    {
                        patternTog = 2;
                    }
                    else
                    {
                        patternTog = 1;
                    }
                    timer = 0;
                }
                //Shifts to a new location in 16 moves
                if (steps < 16)
                {
                    setLocation(getX() + advX(), getY() + advY());
                }
                hitCheck();
                hpCheck();
                steps++;
                if (counter.getValue() == 0)
                {
                    health = 1500;
                }
                if (health <= 1500)
                {
                    bossState = 2;
                    counter.setValue(40);
                    bulfire.stop();
                }
                if(bossState == 2)
                { 

                    if (animTimer < 40)
                    {
                    if (timer < 40)
                    {
                      setLocation(getX(), getY() + 2);
                    }
                    if (timer == 60)
                    {
                       bulSpawn.setLocation(getX(), getY());
                     }
                     if (timer > 80)
                    {
                       setLocation(getX(), getY() - 2);
                    }
                    hitCheck();
                    hpCheck();
                    steps++;               
                }
                  if (counter.getValue() == 0)
                  {
                        health = 0;
                        dead = true;
                  }
            }
            timer++;
            if (timer%50 == 0){
                counter.setValue(counter.getValue() - 1);
                if (counter.getValue() <= 10){
                    timerSnd.play();
                }
            }     
        if (dead == true){
            if (animTimer == 1){
                bulfire.stop();
                deathSnd.play();
                setImage("explode1.png");
            }
            if (animTimer == 6){
                setImage("explode2.png");
            }
            if (animTimer == 11){
                setImage("explode3.png");
            }
            if (animTimer == 15){
                setImage("explode4.png");
            }
            if (animTimer == 19){
                SakuyasWorld sk =(SakuyasWorld)getWorld();               
                sk.removeObject(counter);
                sk.removeObject(this);
                sk.addScore(20000);
                bossDeathSnd.play();
                WinScreen win = new WinScreen();
                sk.addObject(win, 277, 258);
                sakuyaSnd.stop();
                winSnd.play();
                Greenfoot.stop();
            }
            animTimer++;
        
             
         }  
        }
       }
     }
    
      public void hpCheck()
      {
        if (health <= 0){
            SakuyasWorld sk =(SakuyasWorld)getWorld();
            sk.setScore(sk.getScore() + 99999);
            dead = true;
        }
      }
       public int getHealth()
      {
        return health;
      }
      public void spellLoc(){
        destinX = 277;
        destinY = 166;
        steps = 0;
    }
       public void spawnBul()
    {
        Actor reimu = (Actor)getWorld().getObjects(Reimu.class).get(0);
        SakuyasWorld sk =(SakuyasWorld)getWorld();
        for (int i = 0; i < 7; i++)
        {
            bul[i] = new Bullet();
            sk.addObject(bul[i], getX(), getY());
            bul[i].setImage("cherryknife.png");
            bul[i].turnTowards(reimu.getX(), reimu.getY());
        }
        bul[0].turn(45);
        bul[1].turn(30);
        bul[2].turn(15);
        bul[4].turn(-15);
        bul[5].turn(-30);
        bul[6].turn(-45);
      }
     public void spawnBul2()
     {
        SakuyasWorld sk =(SakuyasWorld)getWorld();
        for (int i = 0; i < 10; i++){
            bul[i] = new Bullet();
            sk.addObject(bul[i], getX(), getY());
            bul[i].setImage("cherryknife.png");
        }
        bul[0].setRotation(40);
        bul[1].setRotation(80);
        bul[2].setRotation(120);
        bul[3].setRotation(160);
        bul[4].setRotation(200);
        bul[5].setRotation(240);
        bul[6].setRotation(280);
        bul[7].setRotation(320);
        bul[8].setRotation(360);
    }
    public void gen(){
        destinX = Greenfoot.getRandomNumber(250) + 50;
        destinY = Greenfoot.getRandomNumber(100) + 50;
        steps = 0;
    }
    public int advX()
    {  
        distX = destinX - getX();        
        return distX/15;
    }
    public int advY()
    {
        distY = destinY - getY();
        return distY/15;
    }
     public void hitCheck()
    {
        Actor Shot = getOneIntersectingObject(Shot.class);
        if (Shot != null)
        {
          health = health - 1;
          getWorld().removeObject(Shot);
        }
    }

}
There are more replies on the next page.
1
2