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

2020/2/25

Bullet doesn't fire. It just stays where it is and then causes and error.

1
2
3
footpickle footpickle

2020/2/25

#
Hi. When I run my scenario, instead of waiting until the space bar is pressed, my bullet is summoned. it then stays where it is until it despawns. I get this error(my bullet is called Bomb): java.lang.NullPointerException at Bomb.act(Bomb.java:39) 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) I have absolutely no idea what is wrong. it says this is wrong:
if(life <= 0) 
        {
            getWorld().removeObject(this);
        }
        else 
        {
            life--;
            
        }
particularly the line:
getWorld().removeObject(this);
Thanks :)
danpost danpost

2020/2/25

#
Will need to see the entire act method, at minimum.
footpickle footpickle

2020/2/25

#
What do you mean by the "act method"? Like the "public void act" thing? if so, this is it:
public void act()
    {
        
        checkKeys();
        
        // adjusting the rotation
        int dr = 0;
        if (Greenfoot.isKeyDown("right")) dr++;
        if (Greenfoot.isKeyDown("left")) dr--;
        turn(dr*QVAL);
        // adjusting the speed
        int ds = 0;
        if (Greenfoot.isKeyDown("up")) ds++;
        if (Greenfoot.isKeyDown("down")) ds--;
        speed += ds;
        // limiting the speed
        if (speed < 0) speed = 0;
        if (speed > 5*QVAL) speed = 5*QVAL;
        // moving
        move(speed);
        
     }   
danpost danpost

2020/2/26

#
footpickle wrote...
What do you mean by the "act method"? Like the "public void act" thing? if so, this is it: << Code Omitted >>
That does not look like it was from the Bomb class.
footpickle footpickle

2020/2/26

#
Ah. Ok, here it is then
public void act() 
     {
       
       
       
        if (isAtEdge())
        {
            getWorld().removeObject(this);
        }
        
        if(life <= 0) 
        {
            getWorld().removeObject(this);
        }
        else if (isTouching(Boss.class))
        {
            getWorld().removeObject(this);
        }
        else
        {
            life--;
        }
     }  
This IS from the bomb class :)
footpickle footpickle

2020/2/26

#
The error doesn't come up anymore (Which is good) but the bullet/bomb doesn't actually move. it just stays on the plane that is meant to fire the bomb. I don't know what to do?
footpickle footpickle

2020/2/26

#
Oh wait the error is back :\ I'M SO BAD
danpost danpost

2020/2/26

#
footpickle wrote...
Oh wait the error is back :\ I'M SO BAD
You are missing an else on line 11.
footpickle footpickle

2020/2/26

#
Ohhh... Thanks SO MUCH!
footpickle footpickle

2020/2/26

#
But it doesnt fire on space being pressed, it just stays on the plane... anything wrong with this?
private void checkKeys()
    {
        if (Greenfoot.isKeyDown("space"))
        {
         fire();
        }
        
    }
        private void fire()
     {
         
         getWorld().addObject(bomb, getX(), getY());
         bomb.setRotation(getRotation());
         bomb.move(speed*3);
     }
footpickle footpickle

2020/2/26

#
Is there a "do nothing" method? if so, that would be quite useful
danpost danpost

2020/2/27

#
footpickle wrote...
Is there a "do nothing" method? if so, that would be quite useful
Line 14 to move the bomb should be in the Bomb act method (without the "bomb.").
footpickle footpickle

2020/2/27

#
Thanks for that tip, I will change it and see if it works! :)
footpickle footpickle

2020/2/27

#
IT WORKED! THANK YOU! The only problem is the bomb rapid fires... It is now WAY too overpowered. How do I add a reload like function?
footpickle footpickle

2020/2/27

#
is there a way to set a variable value to 0, for example when the actor hits the world edge? Currently my bomb rapid fires into the world edge and then stays there until it dies, which is irritating.
There are more replies on the next page.
1
2
3