为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

海绵宝宝撒 2022-09-22 13:53:47

我正在尝试制作涂鸦跳跃游戏,目前我无法找到一种方法,一旦它落在地面上,就将静止图像更改为腿部图像,然后又变回静止图像。它为一方这样做,但不是为双方。我尝试过使用嵌套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;

    }

  }

}



查看完整描述

1 回答

?
GCT1015

TA贡献1497条经验 获得超4个赞

首先设置状态,取决于 和:facingisRightisLeft


if (isRight) {

    facing = 0;

} else if (isLeft) {

    facing = 1;

}

设置图像取决于的状态和垂直位置:facingposition.y


if (position.y < groundPosition) {

    this.picture = doodleImg[facing==0 ? 0 : 2];

} else {

    this.picture = doodleImg[facing==0 ? 1 : 3];

}

https://i.stack.imgur.com/btjae.gif

public void move() {


    if (isRight) {

        facing = 0;

    } else if (isLeft) {

        facing = 1;

    }


    if (position.y < groundPosition) {

        this.picture = doodleImg[facing==0 ? 0 : 2];

    } else {

        this.picture = doodleImg[facing==0 ? 1 : 3];

    }


    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) {

        position.y =  groundPosition;

        velocityY = 35;

    }

}


查看完整回答
反对 回复 2022-09-22

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信