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

2020/3/27

Help!! Exercise 9.73 in book! Asteroids!.

Daav3r Daav3r

2020/3/27

#
So when i use key "Z" a counter starts. How to do?
danpost danpost

2020/3/27

#
Only start proton wave when counter is at zero and the 'z' key is pressed. Set counter to some positive value at that time. As an aside, decrement the counter when counter value is not zero.
Daav3r Daav3r

2020/3/29

#
Where should I write it? In rocket class or where?
danpost danpost

2020/3/29

#
Daav3r wrote...
Where should I write it? In rocket class or where?
Uh-huh..
Daav3r Daav3r

2020/3/29

#
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 Kölling
 * 
 * @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 = 50;

    private int reloadDelayCount;               // How long ago we fired the gun the last time.

    private int protonDelayCount;

    int protonCounter=2;

    private GreenfootImage rocket = new GreenfootImage("rocket.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");

    /**
     * Initialise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        addToVelocity(new Vector(13, 0.7));      // initially slowly drifting
        reloadDelayCount = 5;

    }

    /**
     * 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()
    {
        move();
        checkKeys();
        reloadDelayCount++;
        checkCollision();
        //startProtonWave();
        protonDelayCount++;
        reloadDelayCount++; 

        Space space = (Space) getWorld();
        GreenfootImage background = space.getBackground();
        background.fillRect(420 , 460, 150, 20);    
    }

    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        //9.25
        ignite(Greenfoot.isKeyDown("up"));

        if (Greenfoot.isKeyDown("left")) 
        {
            turn(-5);
        }
        //9.21-22
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(5);
        }
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
        if (Greenfoot.isKeyDown("z")) 
        {
            startProtonWave();
            protonIndicator();
        }
    }

    /**
     * Should the rocket be ignited?
     */
    //9.26
    private void ignite(boolean boosterOn) 
    {
        //9.27
        if (boosterOn) 
        {
            setImage (rocketWithThrust);
            addToVelocity (new Vector(getRotation(), 0.3));
        }
        else 
        {
            setImage(rocket);        
        }
    }

    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if(reloadDelayCount >= gunReloadTime) 
        {
            Bullet bullet = new Bullet (getVelocity(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullet.move ();
            reloadDelayCount = 0;
        }
    }
    //9.28
    private void checkCollision(){
        //9.32
        Asteroid a = (Asteroid) getOneIntersectingObject(Asteroid.class);
        if (a != null){
            //9.33, 9.44-45
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            space.removeObject(this);
            space.gameOver();
        }
    }//9.58
    private void startProtonWave()
    {
        //9.61
        if (protonDelayCount >= protonReloadTime)
        {
            ProtonWave wave = new ProtonWave();
            getWorld().addObject (wave, getX(), getY());

            protonDelayCount = 0;
        }
    }

    private void protonIndicator() {

        protonCounter++;

        if(protonCounter < 0){

        }
    }
}
    
    
    
Daav3r Daav3r

2020/3/29

#
The protonIndicator, what am I supposed to write there?
danpost danpost

2020/3/29

#
danpost wrote...
Only start proton wave when counter is at zero and the 'z' key is pressed. Set counter to some positive value at that time. As an aside, decrement the counter when counter value is not zero.
I did write that, but you can also do the following:
if ((waveDelay == 0 || --waveDelay == 0) && Greenfoot.isKeyDown("space")) startWave();
(these are my field and method names) This line will run the delay counter and complete all checks to start a wave -- altogether. The startWave method should add a wave into the world and reset the delay field.
Daav3r Daav3r

2020/3/30

#
sry but I dont get it.
danpost danpost

2020/3/30

#
danpost wrote...
if ((waveDelay == 0 || --waveDelay == 0) && Greenfoot.isKeyDown("space")) startWave();
This line says ( if the counter is zero OR (if not) decreasing it puts it at zero ) AND the space key is down, then start a wave.
Daav3r wrote...
sry but I dont get it.
What don't you get?
Daav3r Daav3r

2020/3/30

#
I dont understood the waweDelay, but I succeeded, so thank you once again for help. Instead I created a new class and I mostly i worked in it.
danpost danpost

2020/3/30

#
Daav3r wrote...
I dont understood the waweDelay
It is like a replacement for your protonDelayCount -- except it counts down, not up.
Daav3r Daav3r

2020/3/30

#
Not really, hard to explain but like a simple indicator. Using updateimage
danpost danpost

2020/3/30

#
danpost wrote...
It is like a replacement for your protonDelayCount -- except it counts down, not up.
This was not a question. It was explaining my waveDelay field of which you said:
I dont understood the waweDelay
However, I think I understood what you meant by:
Daav3r wrote...
hard to explain but like a simple indicator. Using updateimage
Daav3r Daav3r

2020/3/31

#
Aaa hahahah okey xd
You need to login to post a reply.