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

2014/4/15

help having problem with Asteroid Game

NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A bullet that can hit asteroids.
 * 
 * @author Poul Henriksen
 */
public class Bullet extends SmoothMover
{
    /** The damage this bullet will deal */
    private static final int damage = 16;
    
    /** A bullet looses one life each act, and will disappear when life = 0 */
    private int life = 30;
    
    public Bullet()
    {
    }
    
    public Bullet(Vector speed, int rotation)
    {
        super(speed);
        setRotation(rotation);
        addForce(new Vector(rotation, 15));
        Greenfoot.playSound("EnergyGun.wav");
    }
    
    /**
     * The bullet will damage asteroids if it hits them.
     */
    public void act()
    {
        if(life <= 0) {
            getWorld().removeObject(this);
        } 
        else {
            life--;
            move();
            checkAsteroidHit();
        }
    }
    
    /**
     * Check whether we have hit an asteroid.
     */
    private void checkAsteroidHit()
    {
        Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
        if (asteroid != null){
            getWorld().removeObject(this);
            asteroid.hit(damage);
            Space space World=(space)getWorld();
            Counter.add(1);
        }
    }
}
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
don't know what I am doing wrong!! :{
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
if give me the message "non-static method add(int) cannot be refaced from a static context Counter.add(1); with add fill on red Help
danpost danpost

2014/4/15

#
'Counter' is the name of one of your classes, not the name you gave an object of that class. You need to reference an object of that class for the method to execute on. The line before it does not look right, either -- it should probably be something like 'Space space = (Space)getWorld();'. After which you probably need to call a method on 'space' (the world object) to get the Counter object you need -- maybe something like 'Counter counter = space.getCounter();'. Then, you can use 'counter.add(1);'. If you do have a 'getCounter' method in your Space class, this all can be simplified to this:
((Space)getWorld()).getCounter().add(1);
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
thank you danpost now I have this one fill on red if (waveDelayCount >=protonWaveReload) what I do went state: "cannot Find the symbol"
danpost danpost

2014/4/15

#
Obviously, one or both of the variables ('waveDelayCount' and 'protonWaveReload') are not declared properly. Without seeing the entire method, and possibly the entire class code, I could not say more.
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rocket that can be controlled by the arrowkeys: up, left, right.
 * The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
 * 
 * @author Poul Henriksen
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;         // The minimum delay between firing the gun.
    private static final int protonReloadTime = 20;    // The minimum delay between proton wave bursts.

    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    private int protonDelayCount;               // How long ago we fired the proton wave the last time.
    
    private GreenfootImage rocket = new GreenfootImage("rocket.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
    private boolean touched=false;
    /**
     * Initilise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        protonDelayCount = 20;
     }

    /**
     * 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(); 
         imageSwitch();
         explode(); 
        
    }
    
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("up"));
        
        if (Greenfoot.isKeyDown("left")) 
        {
            setRotation(getRotation() - 5);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            setRotation(getRotation() + 5);
        }
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
        if (Greenfoot.isKeyDown("p")) 
    {
            ProtonWave();
        }
    }
      private void ProtonWave(){
         if (waveDelayCount >=protonWaveReload)
         {
           getWorld().addObject(new ProtonWave(), getX(), getY());
           waveDelayCount = 0;
}
    }   
    public void imageSwitch(){
        if (Greenfoot.isKeyDown("up")){
             setImage("rocketWithThrust.png");
              int rotation = getRotation();
              double force = .3;
               Vector ignite = new Vector (rotation, force);
               addForce(ignite);
            }
             else if (!Greenfoot.isKeyDown("up")){
                 setImage("rocket.png"); 
                }
            }
     /**
      * 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());
               bullet.move ();
               reloadDelayCount = 0;
            }
        }
         public void explode(){
             Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
             if (touched && asteroid == null)
             {
                 touched = false;
                }
        else if(!touched && asteroid != null)
    {
         getWorld().addObject( new Explosion(), getX(), getY());
         getWorld().removeObject(this);
         touched = true;
        }
    }
}
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
sorry about that! and thanks for your help in advance
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
I just found out the problem with the rocket. no I have problem with the proton wave
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;

/**
 * A proton wave that expands and destroys things in its path.
 * 
 * @author me
 * @version 1.0
 */
public class ProtonWave extends Actor
{
    /** The damage this wave will deal */
    private static final int DAMAGE = 30;
     private int imageCount = 0;
    /** How many images should be used in the animation of the wave */
    private static final int NUMBER_IMAGES= 60;
    
    /** 
     * The images of the wave. This is static so the images are not
     * recreated for every object (improves performance significantly).
     */
    private static GreenfootImage images;
        
    /**
     * Create a new proton wave.
     */
    public ProtonWave() 
    {
        initializeImages();
        setImage(images);
        Greenfoot.playSound("proton.wav");
    }
    
    /** 
     * Create the images for expanding the wave.
     */
    public static void initializeImages() 
    {
        if (images == null) 
        {
            GreenfootImage baseImage = new GreenfootImage("wave.png");
            images = new GreenfootImage();
            for (int i = 0; 
            While (i < NUMBER_IMAGES);

                int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES );
                images = new GreenfootImage(baseImage);
                images.scale(size, size);
                i++;
            }
        }
    }
    
    /**
     * Act for the proton wave is: grow and check whether we hit anything.
     */
    public void act()
    { 
        checkCollision();
        grow();
    } 
    private void grow()
      { if ((imageCount>= NUMBER_IMAGES))
          {
             getWorld().removeObject(this);
            }
            else
            {
                setImage(images);
                 imageCount++;
                }
            }
    /**
     * Explode all intersecting asteroids.
     */
    private void checkCollision()
    {
        int range = getImage().getWidth() / 2;
        List<Asteroid> asteroids = getObjectsInRange(range, Asteroid.class);     
        
        for (Asteroid a : asteroids) 
        {
            a.hit (DAMAGE);
        }
    }

}
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
my problem is with the line 46. asking for a .class expected !!
danpost danpost

2014/4/15

#
Remove 'for (' from the beginning of line 43. Remove the semi-colon at the end of line 44. Put an open curly bracket on line 45.
NINJAGO_KAi NINJAGO_KAi

2014/4/15

#
line 42 now is telling me has private access!
danpost danpost

2014/4/15

#
You must give some information in the form of arguments in the 'new GreenfootImage' call to let it know how to create one (width & height; or another image to copy; or text, size, and colors for text and background). Refer to the GreenfootImage API documentation to see what is necessary to construct one.
NINJAGO_KAi NINJAGO_KAi

2014/4/16

#
thank you thank you thank you!!
You need to login to post a reply.