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

2013/12/11

Increasing another class' interger in another class

1
2
JasonZhu JasonZhu

2013/12/11

#
private Rocket rocket;

    public Asteroid()
    {
        this(50);
        this.rocket=rocket;
    }

    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
//         rocket.score = rocket.score+20;        
        if(size <= 16) 
        {
            getWorld().removeObject(this);            
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();

            getWorld().removeObject(this);
        }
    }
I get a null pointer exception when the commented line is uncommented. Am I writing it wrong?
danpost danpost

2013/12/11

#
Line 6 has no meaning (both 'this.rocket' and 'rocket' refer to the same field because there is no local field in the method declared as 'rocket'). Therefore 'this.rocket', whose value is 'null', is setting itself to 'null'. There is no rocket object referenced by the field 'rocket', so 'rocket.score' has no meaning.
JasonZhu JasonZhu

2013/12/11

#
Okay! Thanks.
JasonZhu JasonZhu

2013/12/11

#
It appears that I'm still having trouble increasing the value of the integer within my Rocket class from a different class. I keep getting Null Pointer Exceptions
danpost danpost

2013/12/11

#
You need to show your current code (preferably the entire class, not just bits and pieces). I would guess that your 'rocket' field is still 'null' (not yet being set to any Rocket object).
JasonZhu JasonZhu

2013/12/11

#
public class Asteroid extends SmoothMover
{
    /** Size of this asteroid */
    private int size;

    /** When the stability reaches 0 the asteroid will explode */
    private int stability;

    private Rocket rocket;

    public Asteroid()
    {
        this(50);
    }

    public Asteroid(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(360), 2));
        setSize(size);
    }

    public Asteroid(int size, Vector speed)
    {
        super(speed);
        setSize(size);
    }

    public void act()
    {         
        move();
    }

    /**
     * Set the size of this asteroid. Note that stability is directly
     * related to size. Smaller asteroids are less stable.
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this asteroid. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }

    /**
     * Hit this asteroid dealing the given amount of damage.
     */
    public void hit(int damage) {
        stability = stability - damage;
        if(stability <= 0) 
            breakUp ();         
    }

    /**
     * Break up this asteroid. If we are still big enough, this will create two
     * smaller asteroids. If we are small already, just disappear.
     */
    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
        //         rocket.score = rocket.score+20;        
        if(size <= 16) 
        {
            getWorld().removeObject(this);            
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();

            getWorld().removeObject(this);
        }
    }
}
This is my Asteroid class where I want the Rocket's score to increase when the Asteroid breaks up.
public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;         // The minimum delay between firing the gun.
    private static final double VELOCITY = 0.2;
    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    private GreenfootImage rocket = new GreenfootImage("rocket.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
    private boolean withWave;

    public int bullets;
    public int fuel;
    public int score;

    /**
     * Initilise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        withWave = false;
        bullets=100;
        fuel=200;
        score=0;
    }

    /**
     * Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
     * accelerating and shooting when the right keys are pressed.)
     */
    public void act()
    {
        checkKeys();
        reloadDelayCount++;
        move();
        Actor collide = getOneIntersectingObject(Asteroid.class);
        if(collide!=null){
            getWorld().addObject(new Explosion(),getX(),getY());            
            getWorld().removeObject(this);
        }    
    }

    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
        if(Greenfoot.isKeyDown("up")&&fuel>0){
            Vector THRUST = new Vector(getRotation(),VELOCITY);
            addForce(THRUST);
            setImage(rocketWithThrust);
            fuel--;
        }else{
            setImage(rocket);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-5);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(5);
        }
        if(Greenfoot.isKeyDown("z")){
            if(!withWave){
                getWorld().addObject(new ProtonWave(),getX(),getY());
                withWave = true;
                fuel=fuel-50;
            }                               
        }else{
            withWave=false;
        }
    }

    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Bullet bullet = new Bullet (getMovement().copy(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullets--;
            bullet.move ();
            reloadDelayCount = 0;
        }
    }

}
That is my Rocket class
danpost danpost

2013/12/11

#
I think the problem is that your 'score' is in the Rocket class. I would think that it is the user that scores, not the rocket; and, therefore, the score should be in the world class. Another reason for this is because when the Rocket object is removed from the world, so is the score value (unless you retain a reference to the Rocket object in your world class).
JasonZhu JasonZhu

2013/12/11

#
I am still getting the problem even with the space holding the score.
danpost danpost

2013/12/11

#
You will need to cast the world return with 'getWorld' to the Class that you named your world (space or Space):
((Space)getWorld()).score += 20;
JasonZhu JasonZhu

2013/12/11

#
Will give it a try, thanks Dan!
JasonZhu JasonZhu

2013/12/11

#
It worked perfectly Dan! One last question before I'm done this project:
    private void checkPowerUp()
    {        
        Actor powerUp = getOneIntersectingObject(Supply.class);
        if(powerUp!=null){
            if(powerUp(1)){
                bullets=bullets+50;
            }
            if(powerUp(0)){
                fuel=fuel+100;
            }
            getWorld().removeObject(powerUp);
        }                
    }
My Supply Class has an int type parameter. How do I check what type of Supply it is from an intersect? Supply class:
    public Supply(int type)
    {
        addForce(singlePush);
        life = 500;
        switch(type){
            case 1: 
            setImage("gas.png");break;
            case 2:
            setImage("bullets.png");
        }
    }
danpost danpost

2013/12/11

#
You need an instance field in the Supply class to hold its type so you have something to check the value of. In the constructor, set the field to the value of 'type'. Then, the 'powerUp' actor in the 'checkPowerUp' method would need to be cast as a Supply type before you can access the value of that field. This is similar to how we cast the returned value of 'getWorld' as a Space type.
JasonZhu JasonZhu

2013/12/11

#
Thanks so much Dan
JasonZhu JasonZhu

2013/12/12

#
I'm sorry to bring this back up, but do you mean this?
    public int type;
    
    public Supply(type)
    {
        addForce(singlePush);
        life = 500;
        switch(type){
            case 1: 
            setImage("gas.png");break;
            case 2:
            setImage("bullets.png");break;
        }
    }
danpost danpost

2013/12/12

#
danpost wrote...
In the constructor, set the field to the value of 'type'.
You did not perform this action.
public int type;
    
public Supply(int type) // you were missing 'int' on this line
{
    this.type = type; // you need to add this line
    addForce(singlePush); // as used here, 'singlePush' should be an instance field with its value already set
    life = 500;
    switch(type){
        case 1: 
        setImage("gas.png");break;
        case 2:
        setImage("bullets.png");break;
    }
}
Adding line 5 sets the instance field called 'type' to the value of the local field called 'type'. Also, not line 3.
There are more replies on the next page.
1
2