Some errors to be fixed
in
Programming Questions
•
7 months ago
Hi,
Frist of all, sorry for the vague title, it was the closest I could come to describing all my problems. First of all, the rotation doesn't work, (using the ship created in ShipManager.Start()) by which I mean it will rotate, but just in little, seemingly random ways whenever I move the mouse. It should rotate to face the mouse, but I probably messed something up with the translation. Secondly, the movement keys don't work correctly. I can press one key, but the ship just glides off-screen and doesn't respond to any other keys. Lastly, I know this error is known, but on line 133 of the Ship class, (When un-commented, of course) it produces a "Cannot make a static reference to a non-static field" error. Also, any suggestions with any improvements/suggestions/clean-ups to the code would be much appreciated, as I'm still learning how to use iterators and arraylists correctly. Thanks for reading!
Code: (WIP, wanted to share it to get the errors fixed before I continued)
Main Sketch:
- LaserManager LaserManage;
- ShipManager ShipManage;
- void setup() {
- size(500,500);
- LaserManage = new LaserManager();
- ShipManage = new ShipManager(width/2,0,width,height);
- ShipManage.Start();
- }
- void draw() {
- background(0);
- LaserManage.Manage();
- ShipManage.Manage();
- }
Ship class:
- class Ship {
- PVector Ploc,Pvel,Pacc,NewLoc; //Defines the vectors for movement, location, velocity, acceleration, and (For AI) the pathfinder location.
- String Name; //Name of the ship
- boolean IsAI; //IsAI: Whether the ship is run by an AI and will pathfind, or will use controls.
- float Health,Mass,Size,Inertia,Team,MaxSpeedFront,AccFront,MaxSpeedSide,AccSide,MaxSpeedBack,AccBack; //Health: Ammount of health the ship has. MaxSpeedFront-AccBack: Defines the maximum speeds/accelerations of the ship.
- int LaserCharge,LaserCount,MissileCharge,MissileCount; //Charnging times of the various weapons.
- PImage Image; //Image used for the ship.
- PVector Mouse; //These are various vectors constantly defined and used in some calculations.
- Ship(String n, float x, float y, int team, int s, int m, boolean ai, PImage i, int h, float in, float sf, float ss, float sb, float af, float as, float ab) {
- //Name, x starting pos, y starting pos, team, size (Per 16 pixels), mass (Per 1000kg), AI, image used, starting health.
- Ploc = new PVector(x,y);
- Pvel = new PVector(0,0);
- Pacc = new PVector(0,0);
- Name = n;
- IsAI = ai;
- Health = h;
- Mass = m*1000;
- Size = s*16;
- Inertia = in;
- Team = team;
- MaxSpeedFront = sf;
- AccFront = af;
- MaxSpeedBack = sb;
- AccBack = ab;
- MaxSpeedSide = ss;
- AccSide = as;
- LaserCharge = 0;
- LaserCount = 0;
- MissileCharge = 0;
- MissileCount = 0;
- Image = i;
- Mouse = new PVector(mouseX,mouseY);
- }
- void MovementHuman() {
- if(keyPressed) {
- if(key=='w' || key=='W'&&Pacc.y>-MaxSpeedFront) {
- Pacc.y = Pacc.y-(AccFront/(Inertia*Mass))*MaxSpeedFront;
- }
- if(key=='s' || key=='S'&&Pacc.y<MaxSpeedBack) {
- Pacc.y = Pacc.y+(AccBack/(Inertia*Mass))*MaxSpeedBack;
- }
- if(key=='a' || key=='A'&&Pacc.x>-MaxSpeedSide) {
- Pacc.x = Pacc.x-(AccSide/(Inertia*Mass))*MaxSpeedSide;
- }
- if(key=='d' || key=='D'&&Pacc.x<MaxSpeedSide) {
- Pacc.x = Pacc.x+(AccSide/(Inertia*Mass))*MaxSpeedSide;
- }
- }
- else {
- if(Pacc.y>0) {
- Pacc.y = Pacc.y/((AccFront/(Inertia*Mass))*MaxSpeedFront);
- }
- else if(Pacc.y<0) {
- Pacc.y = Pacc.y/((AccBack/(Inertia*Mass))*MaxSpeedBack);
- }
- if(Pacc.x>0) {
- Pacc.x = Pacc.x/((AccSide/(Inertia*Mass))*MaxSpeedSide);
- }
- else if(Pacc.x<0) {
- Pacc.x = Pacc.x/((AccSide/(Inertia*Mass))*MaxSpeedSide);
- }
- }
- }
- int MovementTimer = 0; //Time until next pathfind.
- void MovementAI() { //"Pathfinds" the AI, moving it to a random location.
- MovementTimer++;
- if(MovementTimer>150) { //Pathfinds.
- NewLoc.set(random(PathfindBoundary1.x,PathfindBoundary2.x),random(PathfindBoundary1.y,PathfindBoundary2.y),0);
- MovementTimer = 0;
- }
- }
- void Update() {
- Pacc.normalize();
- if(Pvel.x<MaxSpeedSide&&Pvel.x>-MaxSpeedSide&&Pvel.y<MaxSpeedFront&&Pvel.y>-MaxSpeedBack) {
- Pvel.add(Pacc);
- }
- Ploc.add(Pvel);
- Pacc.mult(0);
- Mouse.set(mouseX,mouseY,0);
- }
- void DisplayHuman() {
- imageMode(CENTER);
- pushMatrix();
- translate(Ploc.x,Ploc.y);
- float Rotation;
- Rotation = PVector.angleBetween(Ploc,Mouse);
- rotate(PVector.angleBetween(Ploc,Mouse));
- image(Image,0,0,Size,Size);
- popMatrix();
- }
- void DisplayAI() {
- imageMode(CENTER);
- float Rotation; //Rotation of the ship.
- Rotation = PVector.angleBetween(Ploc,NewLoc);
- pushMatrix();
- rotate(Rotation);
- image(Image,Ploc.x,Ploc.y,Size,Size);
- popMatrix();
- }
- void CheckEdges() { //Checks if the ship is outside of the box and bounces it back.
- if(Ploc.x<0+Size/2) {
- Ploc.x++;
- }
- else if(Ploc.x>width-Size/2) {
- Ploc.x--;
- }
- if(Ploc.y<0+Size/2) {
- Ploc.y++;
- }
- else if(Ploc.y>height-Size/2) {
- Ploc.y--;
- }
- }
- boolean MouseOver() { //Checks if the mouse if over the ship.
- if(mouseX<Ploc.x+Size/2&&mouseX>Ploc.x-Size/2&&mouseY<Ploc.y+Size/2&&mouseY>Ploc.y-Size/2) {
- return true;
- }
- else {
- return false;
- }
- }
- boolean IsActive() { //Checks if the ship is active.
- if(Health>0) {
- return true;
- }
- else {
- return false;
- }
- }
- PVector TargetLoc = new PVector(0,0);
- void ShootLaserHuman() { //Fires the laser of the ship.
- TargetLoc.set(Mouse);
- //LaserManager.Lasers.add(new Laser(Ploc.x,Ploc.y,1,4,TargetLoc.x,TargetLoc.y,Laser1,255));
- }
- }
Laser class:
- class Laser {
- PVector Ploc,Pvel; //Locaxtion and velocity of the laser.
- int Speed;
- float Color,Timer,Range,Damage; //Defines the speed, color, and time of travel of the laser.
- PImage Image; //Image used for the laser.
- Laser(float x, float y, int d, int s, float vx, float vy, float r, PImage i, float c) {
- // Starting x and y positions, damage, speed (in pixles), velocity x and y, range, image to be used, tinting color (Grayscale).
- Ploc = new PVector(x,y);
- Pvel = new PVector(vx,vy);
- Speed = s;
- Color = c;
- Range = r;
- Damage = d;
- Image = i;
- }
- void Display() { //Displays the laser.
- tint(Color);
- image(Image,Ploc.x,Ploc.y,4,8);
- }
- void Update() { //Moves the laser and updates the timer.
- Ploc.add(Pvel);
- Pvel.normalize();
- Pvel.mult(Speed);
- Timer++;
- }
- boolean IsActive() { //Checks if the laser is active, if it is ruled inactive it will be deleted.
- if(Timer<Range) {
- return true;
- }
- else {
- return false;
- }
- }
- }
Ship manager class:
- import java.util.Iterator;
- PVector PathfindBoundary1;
- PVector PathfindBoundary2;
- class ShipManager {
- ArrayList<Ship> Ships;
- ShipManager(float x1, float y1, float x2, float y2) {
- Ships = new ArrayList<Ship>();
- PathfindBoundary1 = new PVector(x1,y1);
- PathfindBoundary2 = new PVector(x2,y2);
- }
- void Start() {
- Ships.add(new Ship("Player",width/2,height/2,1,4,1000,false,loadImage("Spaceship1.png"),5,0.1,1,1,1,2,2,2));
- }
- void Manage() {
- Iterator<Ship> ShipIterator = Ships.iterator();
- while(ShipIterator.hasNext()) {
- Ship ItShip = ShipIterator.next();
- ItShip.Update();
- if(ItShip.IsAI=true) {
- ItShip.DisplayHuman();
- ItShip.MovementHuman();
- }
- else {
- ItShip.DisplayAI();
- ItShip.DisplayHuman();
- }
- if(!ItShip.IsActive()) {
- ShipIterator.remove();
- }
- }
- }
- void TargetPainter() {
- Iterator<Ship> ShipIterator = Ships.iterator();
- while(ShipIterator.hasNext()) {
- Ship ItShip = ShipIterator.next();
- if(ItShip.MouseOver()) {
- scale(ItShip.Size);
- noFill();
- rectMode(CENTER);
- point(ItShip.Ploc.x,ItShip.Ploc.y);
- if(ItShip.Team==1) {
- stroke(0,255,0);
- }
- else {
- stroke(255,0,0);
- }
- line(ItShip.Ploc.x+32,ItShip.Ploc.y,ItShip.Ploc.x-32,ItShip.Ploc.y);
- line(ItShip.Ploc.x,ItShip.Ploc.y+32,ItShip.Ploc.x,ItShip.Ploc.y-32);
- rect(ItShip.Ploc.x,ItShip.Ploc.y,16,16);
- scale(1);
- }
- }
- }
- }
Laser manager class:
- import java.util.Iterator;
- class LaserManager {
- ArrayList<Laser> Lasers;
- LaserManager() {
- Lasers = new ArrayList<Laser>();
- }
- void Manage() {
- Iterator<Laser> LaserIterator = Lasers.iterator();
- while(LaserIterator.hasNext()) {
- Laser ItLaser = LaserIterator.next();
- ItLaser.Display();
- ItLaser.Update();
- if(!ItLaser.IsActive()) {
- LaserIterator.remove();
- }
- }
- }
- }
1