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

2013/1/5

Constructor issue

BradH BradH

2013/1/5

#
this is the code for my sniper class (the same as my Gun class but different keys to switch the weapons) both have a constructor (Spacemarinemodel1) because they need to attach to the object Spacemarinemodel1 when they enter the world. Once i compile this I get the syntax error constructer can not be applied to given typed, required Spacemarinemodel1. I think that there is an issue with both these classes (Sniper and Gun) having this same constructor, is there anyway that I can fix this or do I need to find a new way to get these objects to attach to the Spacemarine?
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; 
  
    public Gun(Spacemarinemodel1 spacemarinemodel1) {   
        this.spacemarinemodel1 = spacemarinemodel1;   
    }   
  
  
    
  
    
    public void act() { 
        
        Actor Bullet1;
        Bullet1 = getOneObjectAtOffset(0, 0, Bullet1.class);
        
           if("l".equals(Greenfoot.getKey()))
           {
           if(getWorld().getObjects(Bullet1.class).size()<=3)  
                
                  fire();
                
        }
           
       

        
       
        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);  
        
        
          //switch weapon to Sniper
        if((Greenfoot.isKeyDown("2")))
        getWorld().addObject(new Sniper(), getX(), getY());
        //remove the first weapon 
                if((Greenfoot.isKeyDown("2")))
                getWorld().removeObject(this);
                return;
}
    /**
     * fire the gun
     */
    private void fire()
    {
        Ammo Bullet1 = new Bullet1();
        getWorld().addObject(Bullet1, getX() , getY());
        
       Bullet1.move(45);
    }
}
        
vonmeth vonmeth

2013/1/5

#
The problem is likely you are not passing off the spacemarine object when you are creating a Gun or Sniper. I can see an example of that in your code above. "getWorld().addObject(new Sniper(), getX(), getY());" It would need to be something like: "getWorld().addObject(new Sniper(spacemarinemodel1), getX(), getY());"
BradH BradH

2013/1/5

#
Ok, that makes sense, thanks
You need to login to post a reply.