猿问

使用按键()方法的图像更改问题

我正在尝试制作涂鸦跳跃游戏,目前我无法找到一种方法,一旦它落在地面上,就将静止图像更改为腿部图像,然后又变回静止图像。它为一方这样做,但不是为双方。我尝试过使用嵌套if,但随后它不会检测到按钮按下。代码如下:


GameObject game;


PImage doodle;


void setup() {

  size(640, 800);

  smooth(4);

  frameRate(10);


  doodle = loadImage("https://i.imgur.com/ytwebph.png");

  game = new Doodle(new PVector(width/2, height-doodle.height*2), doodle);


  game.setWidthAndHeight(new PVector(width, height));

}

void draw() {


  background(200);

  game.display();

  game.move();

}


void keyPressed() {

  game.setMove(keyCode, true);

}


void keyReleased() {

  game.setMove(keyCode, false);

}


protected class Doodle extends GameObject {


  protected float velocityY, gravity, time;

  protected float groundPosition;

  protected int facing = 0; //0 = right; 1 = left


  protected Doodle(PVector position, PImage picture) {

    super(position, picture);

    gravity = 20;

    time = 0.4;

    velocityY = 35*gravity*time;

    super.setSpeed(10);


    groundPosition = position.y - picture.height;

  }


  public void move() {

    if (isRight || position.y < groundPosition) {

      this.picture = doodleImg[0];

      facing = 0;

    } else if (isLeft || position.y < groundPosition) {

      this.picture = doodleImg[2];

      facing = 1;

    }


    position.x = position.x + speed*(int(isRight) - int(isLeft));


    //border control

    if (position.x+picture.width/2 <= 0) {

      position.x = this.getWidthAndHeight().x-picture.width/2;

    } else if (position.x+picture.width/2 >= this.getWidthAndHeight().x) {

      position.x = 0-picture.width/2;

    }


    //jump

    velocityY -=  gravity * time;

    position.y -=  velocityY * time;

    if (position.y >  groundPosition) {

      if (facing == 0) {

        this.picture = doodleImg[1];

      } else if (facing == 1) {

        this.picture = doodleImg[3];

      }

      position.y =  groundPosition;

      velocityY = 35;

    }

  }

}



海绵宝宝撒
浏览 79回答 1
1回答

GCT1015

首先设置状态,取决于 和:facingisRightisLeftif (isRight) {&nbsp; &nbsp; facing = 0;} else if (isLeft) {&nbsp; &nbsp; facing = 1;}设置图像取决于的状态和垂直位置:facingposition.yif (position.y < groundPosition) {&nbsp; &nbsp; this.picture = doodleImg[facing==0 ? 0 : 2];} else {&nbsp; &nbsp; this.picture = doodleImg[facing==0 ? 1 : 3];}https://i.stack.imgur.com/btjae.gif public void move() {&nbsp; &nbsp; if (isRight) {&nbsp; &nbsp; &nbsp; &nbsp; facing = 0;&nbsp; &nbsp; } else if (isLeft) {&nbsp; &nbsp; &nbsp; &nbsp; facing = 1;&nbsp; &nbsp; }&nbsp; &nbsp; if (position.y < groundPosition) {&nbsp; &nbsp; &nbsp; &nbsp; this.picture = doodleImg[facing==0 ? 0 : 2];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; this.picture = doodleImg[facing==0 ? 1 : 3];&nbsp; &nbsp; }&nbsp; &nbsp; position.x = position.x + speed*(int(isRight) - int(isLeft));&nbsp; &nbsp; //border control&nbsp; &nbsp; if (position.x+picture.width/2 <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; position.x = this.getWidthAndHeight().x-picture.width/2;&nbsp; &nbsp; } else if (position.x+picture.width/2 >= this.getWidthAndHeight().x) {&nbsp; &nbsp; &nbsp; &nbsp; position.x = 0-picture.width/2;&nbsp; &nbsp; }&nbsp; &nbsp; //jump&nbsp; &nbsp; velocityY -=&nbsp; gravity * time;&nbsp; &nbsp; position.y -=&nbsp; velocityY * time;&nbsp; &nbsp; if (position.y >&nbsp; groundPosition) {&nbsp; &nbsp; &nbsp; &nbsp; position.y =&nbsp; groundPosition;&nbsp; &nbsp; &nbsp; &nbsp; velocityY = 35;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答