我的目标是将角色从以下位置循环:
0-3 when DOWN key is pressed |
4-7 when LEFT key is pressed |
8-11 when RIGHT key is pressed |
12-15 when UP key is pressed |
我的程序现在用箭头键响应,特别是用“向下键”,但它每次完成整个循环时都会开始循环。我能做些什么来解决这个问题呢?
PImage[] p = new PImage[16];
int frameCounter = 0;
int current;
int walkTo = 15;
int walkFrom;
void setup(){
frameRate(60);
size(200,200);
imageMode(CENTER);
ashWalk();
}
void draw() {
background(255);
image(p[current], width/2, height/2);
if(frameCounter % 8 == 0) {
if (current > walkTo-1)current = walkFrom;
current++;
}
frameCounter++;
}
void ashWalk(){
for(int i = 0; i < walkTo+1; i++){
p[i] = loadImage("Pokemon"+i+".png");
}
}
void keyPressed() {
ashWalk();
if (key == CODED) {
if (keyCode == DOWN){
walkFrom = 0;
walkTo = 3;
}
else if(keyCode == LEFT){
walkFrom = 4; //what I would possibly want is to start looping in this number
walkTo = 7;
}
else if(keyCode == RIGHT){
walkFrom = 8;
walkTo = 11;
}
else if(keyCode == UP){
walkFrom = 12;
walkTo = 15;
}
}
}
发布于 2020-10-07 02:15:29
据我所知,ashWalk
负责初始化镜像数组0-15。因此,我不认为在keyPressed
中每次都需要调用它。
在draw
中,您必须验证当前是否仍在walkFrom-walkTo的范围内。如果不是(这可能是通过按键更新walkFrom-walkTo时的情况),您必须使用walkFrom重置当前:
if (current >= walkTo || current < walkFrom)
current = walkFrom;
else
current++;
如果在绘制当前图像之前计算此值,则当前图像始终在正确的范围内。
https://stackoverflow.com/questions/64217779
复制相似问题