Need help with this code
in
Programming Questions
•
1 year ago
I'm trying to make a 2D scroller, and I took code from another game I was working on. However when I transferred the code over and made a couple small changes it no longer works... All I'm trying to do is make the character sprite move and its not working at all. Here's the player tab code anyone see why the velocity isn't increasing?
- class Player {
PVector position, velocity;
Boolean isOnLand;
Boolean isFacingRight;
int animationDelay;
int animationFrame;
static final float WALK_SPEED = 5.0;
static final float FRICTION = 0.4;
static final float STANDING = 1.0;
static final int ANIMATION_DELAY = 3;
Player() {
isOnLand = true;
isFacingRight = true;
position = new PVector();
velocity = new PVector();
reset();
}
void reset() {
animationDelay = 0;
animationFrame = 0;
velocity.x = 0;
velocity.y = 0;
}
void inputCheck() {
//if (theKeyboard.holdS && theKeyboard.holdRight) {
// velocity.x += speed*2;
// }
//if (theKeyboard.holdS && theKeyboard.holdLeft) {
// velocity.x -= speed*2;
// }
if (theKeyboard.holdLeft) {
velocity.x -= WALK_SPEED;
}
else if (theKeyboard.holdRight) {
velocity.x += WALK_SPEED;
}
// if (theKeyboard.holdDown) {
// velocity.y -= speedsHere;
// }
else if (theKeyboard.holdUp) {
velocity.y += WALK_SPEED;
}
//velocity.x *= friction;
}
void checkForWalls() {
int charWidth = character_sprite.width; // image size
int charHeight = character_sprite.height;
int wallDistance = int(charWidth * 0.5);
int ceilingDistance = int(charHeight * 0.95);
PVector rightLow, rightHigh, leftLow, leftHigh, topSide;
leftHigh = new PVector();
rightHigh = new PVector();
leftLow = new PVector();
rightLow = new PVector();
topSide = new PVector();
leftHigh.x = leftLow.x = position.x - wallDistance; // left edge of player
rightHigh.x = rightLow.x = position.x + wallDistance; // right edge of player
leftLow.y = rightLow.y = position.y-0.3 * charHeight; // shin high
leftHigh.y = rightHigh.y = position.y-0.8 * charHeight; // shoulder high
topSide.x = position.x; // center of char
topSide.y = position.y-ceilingDistance; // top of char
if (theWorld.worldSquareAt(topSide) == World.TILE_KILLBLOCK ||
theWorld.worldSquareAt(leftHigh) == World.TILE_KILLBLOCK ||
theWorld.worldSquareAt(leftLow) == World.TILE_KILLBLOCK ||
theWorld.worldSquareAt(rightHigh) == World.TILE_KILLBLOCK ||
theWorld.worldSquareAt(rightLow) == World.TILE_KILLBLOCK ||
theWorld.worldSquareAt(position) == World.TILE_KILLBLOCK)
{
//gameover = true;
return;
}
if (theWorld.worldSquareAt(topSide)==World.TILE_SOLID) {
if (theWorld.worldSquareAt(position)==World.TILE_SOLID) {
position.sub(velocity);
velocity.x=0.0;
velocity.y=0.0;
}
else {
position.y = theWorld.bottomOfSquare(topSide)+ceilingDistance;
if (velocity.y < 0) {
velocity.y = 0.0;
}
}
}
if ( theWorld.worldSquareAt(leftLow)==World.TILE_SOLID) {
position.x = theWorld.rightOfSquare(leftLow)+wallDistance;
if (velocity.x < 0) {
velocity.x = 0.0;
}
}
if ( theWorld.worldSquareAt(leftHigh)==World.TILE_SOLID) {
position.x = theWorld.rightOfSquare(leftHigh)+wallDistance;
if (velocity.x < 0) {
velocity.x = 0.0;
}
}
if ( theWorld.worldSquareAt(rightLow)==World.TILE_SOLID) {
position.x = theWorld.leftOfSquare(rightLow)-wallDistance;
if (velocity.x > 0) {
velocity.x = 0.0;
}
}
if ( theWorld.worldSquareAt(rightHigh)==World.TILE_SOLID) {
position.x = theWorld.leftOfSquare(rightHigh)-wallDistance;
if (velocity.x > 0) {
velocity.x = 0.0;
}
}
}
void move() {
position.add(velocity);
checkForWalls();
}
void draw() {
int guyWidth = character_sprite.width;
int guyHeight = character_sprite.height;
if (velocity.x < -STANDING) {
isFacingRight = false;
}
else if (velocity.x > STANDING) {
isFacingRight = true;
}
pushMatrix();
translate(position.x, position.y);
if (isFacingRight == false) {
scale(-1, 1);
}
translate(-guyWidth / 2, -guyHeight);
if (abs(velocity.x) < STANDING) {
image(character_sprite, 0, 0);
}
popMatrix();
}
}
1