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

2017/5/14

Java Error, shooting a bullet/fireball

Simson187 Simson187

2017/5/14

#
Hello Everybody, I just want to shoot a Fireball in the direction, the actor faces to. My code is probably wrong and I dont know how to fix it. If I Run the Program Java says "java.lang.NullPointerException" on the "if-line" on the second code in line xx (considering which Direction it is) Fireball(engl.) = Feuerball Player(engl.) = Spieler
public void checkArrow(){ // in class "Player"
            if(Greenfoot.isKeyDown("space")) {
                if (this.getRotation() == 0){
                    getWorld().addObject(new Pfeil(), getX()+1, getY());
                }
                if (this.getRotation() == 90){
                    getWorld().addObject(new Pfeil(), getX(), getY()+1);
                }
                if (this.getRotation() == 180){
                    getWorld().addObject(new Pfeil(), getX()-1, getY());
                }
                if (this.getRotation() == 270){
                    getWorld().addObject(new Pfeil(), getX(), getY()-1);
                }
                if (this.getRotation() == 315 % 360){
                    getWorld().addObject(new Pfeil(), getX()+1, getY()-1);
                }
                if (this.getRotation() == 45 % 360){
                    getWorld().addObject(new Pfeil(), getX()+1, getY()+1);
                }
                if (this.getRotation() == 135 % 360){
                    getWorld().addObject(new Pfeil(), getX()-1, getY()+1);
                }
                if (this.getRotation() == 225 % 360){
                    getWorld().addObject(new Pfeil(), getX()-1, getY()-1);
                }
           
        
            }
    }
public void shootingDirection(){ // in class "Fireball"
        
        Spieler aktSpieler = (Spieler) this.getOneObjectAtOffset(getX(),getY(),Spieler.class);
        
                if (aktSpieler.getRotation() == 0){
                    setLocation(getX() + speed, getY());
                }
                if (aktSpieler.getRotation() == 90){
                    this.setLocation(getX(), getY() + speed);
                }
                if (aktSpieler.getRotation() == 180){
                    this.setLocation(getX() - speed, getY());
                }
                if (aktSpieler.getRotation() == 270){
                    this.setLocation(getX(), getY() - speed);
                }
                if (aktSpieler.getRotation() == 315 % 360){
                   this.setLocation(getX() + speed, getY() - speed);
                }
                if (aktSpieler.getRotation() == 45 % 360){
                    this.setLocation(getX() + speed, getY() + speed);
                }
                if (aktSpieler.getRotation() == 135 % 360){
                    this.setLocation(getX() - speed, getY() + speed);
                }
                if (aktSpieler.getRotation() == 225 % 360){
                    this.setLocation(getX() - speed, getY() - speed);
                }
        }
I already tested out something with " != null " but it didnt works. Help me!
danpost danpost

2017/5/14

#
Nowhere are you giving the fireballs a direction to move in (x and y speeds) or turning them to face a direction to move in (rotation). The shootingDirection method will only work so long as the fireball is still touching the player; then, the error will occur. Since you want them to travel in the direction the player is facing (or along its rotation), it is easy enough to set the rotation of the fireball to the same. You just need to create the fireball first; then change its rotation and add it to the world. Replace lines 3 through 26 of the 'checkArrow' method with this:
Pfeil pfeil = new Pfeil();
pfeil.setRotation(this.getRotation());
this.getWorld().addObject(pfeil, this.getX(), this.getY();
and remove the 'shootingDirection' method. In the Pfeil class, use the 'move' method instead of 'setLocation' to move the fireballs.
Simson187 Simson187

2017/5/14

#
Thank you very much! It finally works without any errors! But now I see two little problems: -the Fireball shoots very slow, how i can get the speed like before? The Fireball should move every two cells. -and the Fireball is spawning inside the Actor, I fixed that problem before but I dont know how bring it on the new code Regards
danpost danpost

2017/5/14

#
You can move the fireball a bit after adding it into the world. After the 3-liner above, add:
move(1); // adjust distance as needed
In the fireball act method, use:
move(2);
to move 2 cells.
Simson187 Simson187

2017/5/14

#
Thank u but u mean "pfeil.move(1);" :)
danpost danpost

2017/5/14

#
Simson187 wrote...
Thank u but u mean "pfeil.move(1);" :)
Yes -- of course.
You need to login to post a reply.