Making an object stop, and move forward when key pressed (power bar)

Hello, I’m trying to code a ball that, when the program is executed, moves side to side. When a key is pressed (and held down), a power bar appears displaying how much power the ball is going to shoot with when the key is released.

So far i’ve got the ball moving side to side and the power bar functioning, i’m only struggling with the code that shoots the ball up.

i have written a function for the ball moving up which i know works (i have tested the code on its own), but when i call the function it doesn’t work.

Any help would be greatly appreciated :smiley:

boolean keyReleased = false;

float newEllipseX;
float newEllipseY = 465;
float newSpeedEllipse = 3;
float speedEllipse = 3;

float ellipseX = 50;
float ellipseY = 465;

float powerBar = 0;
float velocity = 0;


void setup(){
  size(500, 500);
}


void drawPlayer(){
  fill(225);
  ellipse(ellipseX, 465, 20, 20);
  ellipseX = ellipseX + speedEllipse;
  
  if(ellipseX > width || ellipseX < 0) {
      speedEllipse = speedEllipse * -1.0;
    }
}


void keyReleased() {
  velocity = powerBar;
  velocity = velocity/10;
  powerBar = 0;
  newEllipseX = ellipseX;
  fill(0);
  
  ellipse(newEllipseX, newEllipseY, 20, 20);
  newEllipseY = newEllipseY - newSpeedEllipse;
  newSpeedEllipse = newSpeedEllipse - velocity;
  
  if (newSpeedEllipse < 0){
    newSpeedEllipse = 0;
  }
}


void draw(){  
  noStroke();
  background(0, 100, 0);
 
  fill(50);
  noStroke();
  ellipse(400, 100, 35, 35);
  
  drawPlayer();
   
  if (keyPressed == true){
    
    ellipseX = ellipseX - speedEllipse;
     
    rectMode(CORNER);
    fill(225);
    noStroke();
    rect(25, 15, 25, 140);
     
    fill(200, 0, 0);
    rectMode(CORNER);
    rect(25, 155, 25, powerBar);
    powerBar = powerBar - 0.5 ;
    
    if (powerBar < -140) {
      powerBar = 0;
    }
    
    //keyReleased();

    
  } 
 
  
   
   
} 
  


hi,

nice code so far…
if i well understood, you want your ball to gain an acceleration (powerBar value) when keyreleased is triggered?
if so:

void drawPlayer(){
  fill(225);
  ellipse(ellipseX, 465, 20, 20);
  ellipseX = ellipseX + speedEllipse;
  
  if(ellipseX > width || ellipseX < 0) {
      speedEllipse = speedEllipse * -1.0;
    }
}

this is drawing of your ball, and where its movement is set by adding speedEllipse each frame;

so now the question (answer) is , when keyReleased occur how do you manipulate this speedEllipse value?

if you do println(speedEllipse); at the end of keyReleased, does it change as expected?

for the other code you have in keyRelease: according to keyRelease reference,
" The keyReleased() function is called once every time a key is released."
what do you expect from this?

Hello again, Thank you for your quick response.

I am struggling to find a way to identify when a key has been released, resulting with me not being able to run the code that moves the ball vertical.

I also added the line println(speedEllipse); and saw that the speed value isn’t changing from 3. Since the code that changes the speed is also triggered after a key has been released, this is also linked to my first problem.

I was wondering if you could provide more guidance on how i can detect when a key has been released and how to run this section of the code:

velocity = powerBar;
  velocity = velocity/10;
  powerBar = 0;
  newEllipseX = ellipseX;
  fill(0);
  
  ellipse(newEllipseX, newEllipseY, 20, 20);
  newEllipseY = newEllipseY - newSpeedEllipse;
  newSpeedEllipse = newSpeedEllipse - velocity;
  
  if (newSpeedEllipse < 0){
    newSpeedEllipse = 0;
  }
}

Thank you once again :smiley:

You can use a boolean variable to store whether the key has been released

I named it ballHasBeenShot

boolean ballHasBeenShot = false; 

//boolean keyReleased = false;

float newEllipseX;
float newEllipseY = 465;
float newSpeedEllipse = 3;
float speedEllipse = 3;

float ellipseX = 50;
float ellipseY = 465;

float powerBar = 0;
float velocity = 0;


void setup() {
  size(500, 500);
}


void drawPlayer() {

  if (ballHasBeenShot) {
    // FLY UP 
    fill(0);
    ellipse(newEllipseX, newEllipseY, 20, 20);
    newEllipseY = newEllipseY - newSpeedEllipse;
    newSpeedEllipse = newSpeedEllipse - velocity;

    if (newSpeedEllipse < 0) {
      newSpeedEllipse = 0;
    }
  } else {
    // go left / right before shooting 
    fill(225);
    ellipse(ellipseX, 465, 20, 20);
    ellipseX = ellipseX + speedEllipse;

    if (ellipseX > width || ellipseX < 0) {
      speedEllipse = speedEllipse * -1.0;
    }
  }
}


void keyReleased() {
  velocity = powerBar;
  velocity = velocity/10;
  powerBar = 0;
  newEllipseX = ellipseX;
  ballHasBeenShot=true;
}


void draw() {  
  noStroke();
  background(0, 100, 0);

  fill(50);
  noStroke();
  ellipse(400, 100, 35, 35);

  drawPlayer();

  if (keyPressed) {
    ellipseX = ellipseX - speedEllipse;

    rectMode(CORNER);
    fill(225);
    noStroke();
    rect(25, 15, 25, 140);

    fill(200, 0, 0);
    rectMode(CORNER);
    rect(25, 155, 25, powerBar);
    powerBar = powerBar - 0.5 ;

    if (powerBar < -140) {
      powerBar = 0;
    }
  }
}
//