So when i use key "Z" a counter starts. How to do?
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){ } } }
if ((waveDelay == 0 || --waveDelay == 0) && Greenfoot.isKeyDown("space")) startWave();
if ((waveDelay == 0 || --waveDelay == 0) && Greenfoot.isKeyDown("space")) startWave();