0

currently i am taking a java class, and out professor is introducing it with the language processing so we get the idea of how things work. he moentioned that processing comes from java, so that's the reason i posted it here.
i made a really simple game somewhat of a clone of galaga/space invaders, but now i would like to break it up into classes and i am having some trouble with moving the ship(a triangle).
when ever i hit the arrow key the ship just keeps moving continuously.
it works fine when its not broken into classes
hopefully somebody has some knowledge of the language

//***this is the main class***///
//SIZE
int width = 400;
int height = width;

//SHIP CENTER COORDINATES + OBEJECT
int shipCenterX = 200;
int shipCenterY = 380;
ship shipA;

//INITIALIZE GAME
void setup()
{
  size(width, height);
  shipA = new ship(shipCenterX, shipCenterY, width, height);
}


//DRAW GAME
void draw()
{
  background(0);
  shipA.draw();
  shipA.keyPressed();
} 
---------------------------------------------
---------------------------------------------

///***the ship class***///
class ship
{ 
  int x, y, w, h;

  ship(int x, int y, int w, int h)
  {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }

  void draw()
  {
    fill(255);
    triangle(x - 10, y + 10, x, y, x + 10, y + 10);
  }

  //TRIANFGLE MOVEMENT
  void keyPressed()
  {
    if (key == CODED)
    {
      switch(keyCode)
       {
        case LEFT:
        x -= 3;
        if (x < 0)
        {
         x = width;
        } 
        break;
        //
        case RIGHT:
        x += 3;
        if (x > width)
        {
          x = 0;
        }
     }
    }
  }
}

Edited by mike_2000_17: Fixed formatting

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by juan92
0

derrrr i figured it out duh!
the draw method in processing is a loop so it would keep on adding to the x value continously

Edited by juan92: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.