We are about to switch to a new forum software. Until then we have removed the registration on this forum.
here are the variables in the main tab
public float bellyXloc, bellyYloc;
public float bellyWidth, bellyHeight;
public float accelerationX,accelerationY;
public float decelerationX, decelerationY;
public PVector location, acceleration, deceleration;
public float max_speedx,max_speedy ;
public float gravity;
public color characterColor;
public Boolean isLeft=false , isRight=false , isUp= false ;
constructor
bellyXloc=width/2;
bellyYloc=height/2; //-(1.6*bellyHeight);
bellyWidth=width/20;
bellyHeight= height*0.1;
characterColor = color(152,223,234);
location = new PVector(bellyXloc,bellyYloc);
accelerationX=1.3;
accelerationY=0;
max_speedx = 5;
max_speedy = 3;
acceleration = new PVector(accelerationX,accelerationY);
decerelationX = 0.75;
decerelationY = 0.75;
deceleration = new PVector(decelerationX, decelerationY);
gravity = .3;
a = new AlienCharacter(location,bellyWidth,bellyHeight, color(152,223,234),
acceleration, max_speedx, max_speedy, decerelation ,gravity);
in the class void update
void update() {
if ( isRight ) {
location.add(acceleration);
if ( acceleration.x > max_speedx ) {
max_speedx = acceleration.x;
}
}
else if ( isLeft ) {
location.sub(acceleration);
if ( acceleration.x < -max_speedx ) {
max_speedx = acceleration.x;
}
}
else { //neither right or left pressed, decelerate
if ( acceleration.x > 0 ) {
acceleration.x -= deceleration.x;
if ( acceleration.x < 0 ) {
acceleration.x = 0;
}
}
else if ( acceleration.x < 0 ) {
acceleration.x += deceleration.x;
if ( acceleration.x > 0 ) {
acceleration.x = 0;
}
}
}
if ( isUp ) {
location.sub(acceleration);
if ( acceleration.y-0.5 < -max_speedy ) {
max_speedy = acceleration.y*deceleration.y;
}
}
and in the main tab
void keyPressed() {
switch(keyCode) {
case RIGHT: isRight = true; break;
case LEFT: isLeft = true; break;
case UP: isUp = true; break;
}
}
void keyReleased() {
switch(keyCode) {
case RIGHT: isRight = false; break;
case LEFT: isLeft = false; break;
case UP: isUp = false; break;
}
}
The problem that character doesn't react to keyboard I don't where might be the problem
Answers
Here's some basic movement.
It is hard to say what's wrong with your object not moving unless you post a ready-to-go snippet of code that we can copy, paste, and run to see you issue.
Here is the full one
ctrl-t in the pde editor will indent the code for you, which aids readability.
Lines 307-318 are the problem. If neither key is pressed, acceleration.x rapidly becomes 0 because of the logic there. And then what value added/subtracted from the location? Yep, acceleration!
Removing the offending lines causes your figure to move.
thanks I got it, it solved now, i add two new global variable speedX=0 speedY=0 PVector speed(speedX,speedY) however than again my question would if I jump and due to the gravity I fall back how can I stop my character to stay on the ground?
You would need to add a conditional statement to check to see it your character is on the ground. Your character is on the ground when its y location reaches or exceeds a certain value. At that point, you will want to reassign that limit to the y position, and probably 0 out the y velocity.
I added isOnGround as public boolean in class constructor I declared as false
I try to stop the character to go above the screen but it doesn't work and neither the pervious statement above maybe because speed is PVector so speed.y is doesn't have a value or is it automatically 0?
How can fix first the this that the character will stop at the moment on the ground since at the moment i don't have a game grid, and 2nd how can I stop the character go above the screen ? Thanks