I am trying to get my Rocket class to move with vector movement. I have no smooth mover class and am using vector version 2.1. My rocket will inch forward when the up arrow is pressed but nothing more than move(x) happens. My code is below, any help would be great!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; /** * Write a description of class Rocket here. * * @author (your name) * @version (a version number or a date) */ public class Rocket extends Actor { /** * Act - do whatever the Rocket wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int rotation; private int gunDelay = 0 ; public static int minGunDelay = 25 ; private Vector movement; private double x; private double y; public static boolean dead = false ; public Rocket() { double j = . 3 ; Vector drift = new Vector( 0 ,. 3 ); drift.add(drift); minGunDelay= 25 ; dead = false ; } public void act() { Shoot(); rotation = getRotation(); move(); borderTravel(); gunDelay++; kill(); } public void Shoot() { //spawns a new Gun object if (gunDelay >= minGunDelay) { if ( "space" .equals(Greenfoot.getKey())) { getWorld().addObject( new Gun(rotation), getX(), getY()); gunDelay = 0 ; } } } public void move() { //makes the wocket turn or boost when a certain key is pressed if (Greenfoot.isKeyDown( "left" )) { setRotation( getRotation() - 5 ); } if (Greenfoot.isKeyDown( "right" )) { setRotation( getRotation() + 5 ); } if (Greenfoot.isKeyDown( "up" )) { Vector drift = new Vector(getRotation(),. 3 ); drift.add(drift); x = getX() + drift.getX(); y = getY() + drift.getY(); setLocation(( int ) x, ( int ) y); } } public void borderTravel() { //makes the enemies go through the border and spawn on the other side int x = getX(); int y = getY(); int width = getWorld().getWidth(); int height = getWorld().getHeight(); if (x == 0 ) { setLocation(width- 2 , y); } if (x == width - 1 ) { setLocation( 1 ,y); } if (y == 0 ) { setLocation(x, height- 2 ); } if (y == height- 1 ) { setLocation(x, 1 ); } } public void kill() { //detects when the gun is close to an asteroid and shoots removes them both List<Enemy1> enemies = this .getObjectsInRange( 25 , Enemy1. class ); for (Enemy1 e : enemies) { if (enemies.size()> 0 ) { dead = true ; getWorld().removeObject(enemies.get( 0 )); getWorld().removeObject( this ); } } } } |