gpt4 book ai didi

java - 使用 keypressed() 方法的图像更改问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:51 25 4
gpt4 key购买 nike

我正在尝试制作一款涂鸦跳跃游戏,目前我找不到一种方法可以让静止图像在落地后变为腿部图像,然后再变回静止图像。它对一方面有作用,但对双方都没有作用。我尝试过使用嵌套 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;
}
}
}

public class GameObject {
public PVector position, widthAndHeight;
public int size, speed;
public PImage picture;
public boolean isLeft, isRight;

public PImage[] doodleImg = new PImage[6];

public GameObject(PVector position, PImage picture) {
this.position = position;
this.picture = picture;
widthAndHeight = new PVector(0, 0);
speed = 1;

//0,1 right; 2,3 left; 4,5 shoot;
doodleImg[0] = loadImage("https://i.imgur.com/ytwebph.png");
doodleImg[1] = loadImage("https://i.imgur.com/Y0cFSFK.png");
doodleImg[2] = loadImage("https://i.imgur.com/FL3IhU5.png");
doodleImg[3] = loadImage("https://i.imgur.com/YuqWihj.png");
}

public void move() {
position.x = 0;
}

public void display() {
image(picture, position.x, position.y);
}

public boolean setMove(int keycode, boolean isPressed) {
switch(keycode) {
case LEFT:
return isLeft = isPressed;

case RIGHT:
return isRight = isPressed;

default:
return isPressed;
}
}


protected PVector getWidthAndHeight() {
return widthAndHeight;
}

void setSpeed(int speed) {
this.speed = speed;
}


public void setWidthAndHeight(PVector newWidthAndHeight) {
widthAndHeight = newWidthAndHeight;
}
}

最佳答案

首先设置faceing状态,依赖于isRightisLeft:

if (isRight) {
facing = 0;
} else if (isLeft) {
facing = 1;
}

根据faceing的状态和垂直位置设置图像position.y:

if (position.y < groundPosition) {
this.picture = doodleImg[facing==0 ? 0 : 2];
} else {
this.picture = doodleImg[facing==0 ? 1 : 3];
}
<小时/>

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;
}
}

关于java - 使用 keypressed() 方法的图像更改问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55531532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com