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

2012/9/3

Errors

buggy213 buggy213

2012/9/3

#
I have been trying to make a game where two people fight but it is not working!Heres the code: world class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class world here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class world extends World
{
BazookaMan bazookaman;
boolean x = bazookaman.x;

    /**
     * Constructor for objects of class world.
     * 
     */
    public world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 500, 1); 

        prepare();
        CheckAlive(x);
       }
    
       
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        BazookaMan bazookaman = new BazookaMan();
        addObject(bazookaman, 40, 35);
        bazookaman.setLocation(41, 35);
        bazookaman.setLocation(57, 32);
        bazookaman.setLocation(44, 34);
    }
   void CheckAlive(boolean x){
       if(x = true){
    removeObject(bazookaman);}
}
}
p1
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BazookaMan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BazookaMan extends Mover
{
Bazooka bazooka;
 public boolean x;
 
    
    public void act() 
    {
        if (Greenfoot.isKeyDown("left")){
            setRotation(getRotation()- 5);
            
        }
        if (Greenfoot.isKeyDown("right")){
            setRotation(getRotation()+ 5);
            
        }
        if (Greenfoot.isKeyDown("z")){
            move(1);
            
        }
         if ("space".equals(Greenfoot.getKey())){
            fire();
            
        }
    }    
    private void fire(){
        
        bazooka = new Bazooka();
        getWorld().addObject(bazooka,getX(),getY());
        bazooka.setRotation(getRotation());
        
            
        
    }
   
    
}
projectile
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bazooka here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bazooka extends Mover
{
    /**
     * Act - do whatever the Bazooka wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10.0);
    }    
}
no 2p yet
danpost danpost

2012/9/3

#
In the 'world' class: The first thing that strikes me is that in line 12 you are trying to use an un-initialized variable to get the value of a field from. Remove line 34, and replace what is in line 11 with what was in line 34. You can also remove lines 36 and 37, as they are being over-riden by line 38. Also, remove line 24 and lines 40 through 43. The constructor of the world should not have to check this. In the 'BazookaMan' class: You do not need to hold a reference to any Bazooka objects created here. A local reference in the fire() method is all you need. Remove line 11 and change line 36 to 'Bazooka bazooka = new Bazooka();'. As far as the keys being used to control p1, usually when 'left' and 'right' arrows are used to change the rotation, the 'up' key is used to move. p2 will probably need the left side of the keyboard, so having p1 use 'z' would be in the way of p2, who would usually use 'a' and 'd' for rotation, and 'w' for move. In the 'Bazooka' class: I do not see any constructors. I will assume you have a default image set for it. Hope this helps
nccb nccb

2012/9/3

#
Line 41 of that code, you're using a single equals sign in the if statement, which makes it an assignment, not an equality check. You need two equals:
if (x == true)
danpost danpost

2012/9/3

#
Looking back at the 'world' class, you can also remove line 38 and change line 35 to addObject(bazookaman, 44, 34); which places the object where you want.
Gevater_Tod4711 Gevater_Tod4711

2012/9/5

#
Line 41 is the wrong line like nccb just said, but your variable x is from type boolean, so you also can write if (x) { ... }. It is the same effect, this one is just shorter.
You need to login to post a reply.