How do I let the rocket move correctly?

edited January 2016 in Questions about Code

Uhm, hey. Well I am new to processing and for a school project we need to make something interactive. I want to make a rocket that moves from the left to the right and blows up meteorites that come falling from above. (Sorry, for bad english btw).

I do not have a lot of code right at the moment, because I do not know how to fix what I already have. I got to questions:

  1. How do I fix that the rocket still moves after it reached the ends of the frame?

  2. How do I fix that the rocket won't leave the frame? The rocket stops when I told him to stop, but if I press the keys again it will slowly leave. Because my dX changes from 0 to 5.

Can someone please help me?

Here is my code:

//The picture of the galaxy was found on https://en.wikipedia.org/wiki/Carina_Dwarf_Spheroidal_Galaxy. It is a picture of a Carina Dwarf Speroidal Galaxy. It was discovered in 1977.
//The picture of the rocket was found on http://pics-about-space.com/nasa-space-ship-clip-art?p=2.


PImage galaxy;    //image of a galaxy I used
PImage rocket;    //image of a rocket I used
int x=250;  //beginning of the rocket when first clicked on play
int dX = 5; //the number used for dX

void setup(){
  size(750,900); //the size of the frame

  //the galaxy image
  galaxy = loadImage("galaxy background.jpg"); //the image I am going to use as a galaxy background needs to be loaded
  galaxy.resize(750,900); //the image has to have the same size as the file, so I resized the image

  //the rocket image
  rocket = loadImage("spaceship.png"); //the image I am going to use as therocket needs to be loaded
  rocket.resize(300,250); //the image needs to be resized
}

void draw(){
    background(galaxy); //the file has a galaxy background
  //the rocket
  image(rocket,x,650); //the x is variable, so the rocket can move. 

  keyPressed(); //i don't know why this is here, but a friend of me told me to put it there
}

void keyPressed(){ //everything in here happens when a key is pressed
  if (key == CODED){  //now I can go code some keys
    if (keyCode == RIGHT){ //the arrow to the right is now coded
      x = x + dX; //this is the formula of the x
    }
    if (x+200>width || x+100<0){ //the rocket won't leave the frame 
      dX=0; //--> there must be something else to stop my rocket from moving
    } 
    if (keyCode == LEFT){ //the arrow to the left is now coded
      x = x - dX; //this is the formula of the x
    }
    if (x+200>width || x+100<0){ //the rocket won't leave the frame
      dX=0; //--> there must be something else to stop my rocket from moving
    }
  }

}

void keyReleased(){ //everything in here happens when a key is released
  if (key == CODED){
    if (keyCode == RIGHT){
      x=x+dX;
      dX=0;
    }
    else{ 
       dX=5;
  }
  if (keyCode == LEFT){
    dX=0;
  }
  else {
    dX=5;
  }
  }
}
Tagged:

Answers

  • edited January 2016 Answer ✓
    1. Line 27 - you don't need it, despite what your friend say.
    2. You don't need keyReleased() in this case
    3. Your keyPressed() may look like this:

      void keyPressed() {
       if (key == CODED) {
        if (keyCode == RIGHT) {
         x = x + dX;
        } else if (keyCode == LEFT) {
         x = x - dX;
        }
       }
       if (x + rocket.width > width) {
        x = width - rocket.width;
       }
       if (x < 0) {
        x = 0;
       }
      }
      

    and it would work. However, I recommend doing it a different way. You can set your x to be equal x+dx in a draw() and in keyPressed only set dx = 1 or dx = -1 and in keyReleased() you can just say dx = 0

    It is a rule on this forum not to provide complete solutions, so please, try that yourself and if you have problems ask specific questions.

  • Thank you Ater, that worked. You are my hero, thank you so much

  • edited January 2016

    It is a rule on this forum not to provide complete solutions, ...

    Actually the actual rules don't prohibit full solutions! Merely advises so:
    https://forum.Processing.org/two/discussion/8042/processing-forum-rules-and-advices

    HOMEWORK: ... but we all expect some work from you. Ie. you should show some code proving you worked on the topic, ...

    Of course, chances are very slim for some1 to come up w/ full solutions right off the bat! :-\"

Sign In or Register to comment.