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

2013/1/12

Run Time error

1
2
BradH BradH

2013/1/13

#
ok, so now the bullet hits the guardian, but there are two problems first the there is no change in the value of the guardian counter, and when the bullet disappears I get a nullpointer error so there must be an error with the order I have my bullet class in (so I will check that out). What I do not know is why the guardiancounter is not subtracting 1. Here is my gun code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gun here.
 * 
 * @author (BradH) 
 * @version (a version number or a date)
 */

public class Gun extends Weapons
{   
    private Spacemarinemodel1 spacemarinemodel1;   private SniperCounter snipercounter;   
    public Gun(Spacemarinemodel1 spacemarinemodel1, SniperCounter pointSniperCounter) 
    { 
        this.spacemarinemodel1 = spacemarinemodel1;       
        snipercounter = pointSniperCounter;     }      

    private GuardianCounter guardiancounter;
    //here so it knows the bullet1
    public Gun(GuardianCounter pointGuardianCounter)
    {
        guardiancounter = pointGuardianCounter;
    }

    public void act() { 
        if(Greenfoot.isKeyDown("l"))
        {
            if(getWorld().getObjects(Bullet1.class).size()< 1)  

                fire();

        }

        //switch weapon to Sniper
        if((Greenfoot.isKeyDown("2")))
            getWorld().addObject(new Sniper(spacemarinemodel1, snipercounter), getX(), getY());
        //remove the first weapon 

        if((Greenfoot.isKeyDown("2")))
        {getWorld().removeObject(this);
        }

        int spacemarinemodel1X = spacemarinemodel1.getX();   
        int spacemarinemodel1Y = spacemarinemodel1.getY();   
        // Modify the xOffset and   
        // yOffset to make the gun   
        // appear in the correct   
        // position.   
        int xOffset = 27;   
        int yOffset = -8;   
        int x = spacemarinemodel1X + xOffset;   
        int y = spacemarinemodel1Y + yOffset;   
        setLocation(x, y);  

    }
    /**
     * fire the gun
     */
    private void fire()
    {  GuardianCounter guardiancounter;
        guardiancounter = new GuardianCounter();

        Ammo Bullet1 = new Bullet1(guardiancounter);
        getWorld().addObject(Bullet1, getX() , getY());

        Bullet1.move(45);
    }

}


vonmeth vonmeth

2013/1/13

#
That is just creating a new GuardianCounter every time you fire now. The problem stems from the problem we had earlier in another topic. You have two constructors:
public Gun(Spacemarinemodel1 spacemarinemodel1, SniperCounter pointSniperCounter)   
    {   
        this.spacemarinemodel1 = spacemarinemodel1;         
        snipercounter = pointSniperCounter;
    }        

    public Gun(GuardianCounter pointGuardianCounter)  
    {  
        guardiancounter = pointGuardianCounter;  
    }  
When a Gun is created it uses one or the other constructor. If it uses the first constructor (which I'm sure it does), it has no idea about the GuardianCounter you created and added to the world wherever else. Remove the bit I talked about, so fire looks like this again:
private void fire()  
    {  
        Ammo Bullet1 = new Bullet1(guardiancounter);  
        getWorld().addObject(Bullet1, getX() , getY());  
  
        Bullet1.move(45);  
    } 
Combine the two constructors:
public Gun(Spacemarinemodel1 spacemarinemodel1, SniperCounter pointSniperCounter, GuardianCounter pointGuardianCounter)   
    {   
        this.spacemarinemodel1 = spacemarinemodel1;         
        snipercounter = pointSniperCounter;
        guardiancounter = pointGuardianCounter;  
    }        
Edit: You will have to then change a bit in other places to handle this change to the constructor, and probably do the same for the Sniper constructor, so you have a constant reference to the GuardianCounter (like you had to do in the other topic).
BradH BradH

2013/1/13

#
I combined them in my Gun class but there is still no change in the value of the guardiancounter, I checked my guardiancounter class and it has a subtract method so I do not know what is wrong. So now wouldn't the problem be in the Bullet1 actor?
vonmeth vonmeth

2013/1/13

#
The problem is likely you are not passing the same GuardianCounter object that is being displayed to your Bullet1 actor, or your GuardianCounter is not updating to show the change in value.
BradH BradH

2013/1/13

#
I will work on getting it to work, thank you for the help.
You need to login to post a reply.
1
2