Processing 如何为精灵表动画选择行?
您好,我是java/processing新手,我正在尝试将这个精灵表设置为动画,但我遇到的问题是,我无法解决,如何在精灵表的行数之间更改我的图形 我试过创建一个变量,当你按某个键时,它会改变顺序,但我不知道,这是否是因为,它在函数设置中没有改变值或什么Processing 如何为精灵表动画选择行?,processing,Processing,您好,我是java/processing新手,我正在尝试将这个精灵表设置为动画,但我遇到的问题是,我无法解决,如何在精灵表的行数之间更改我的图形 我试过创建一个变量,当你按某个键时,它会改变顺序,但我不知道,这是否是因为,它在函数设置中没有改变值或什么 int nx = 6; int ny = 4; int cs; int ts = nx * ny; int y = 0; int dir = 0; PImage[] sprites; void setup() { size(600,60
int nx = 6;
int ny = 4;
int cs;
int ts = nx * ny;
int y = 0;
int dir = 0;
PImage[] sprites;
void setup()
{
size(600,600);
sprites = new PImage[ts];
PImage spritesheet = loadImage("https://denysalmaral.com/wp-
content/uploads/2017/02/trump_run.png");
int w = spritesheet.width / nx;
int h = spritesheet.height / ny;
int index = 0;
move(index, w, h, sprites, spritesheet);
}
void draw()
{
background(0);
ms();
draw_sprite();
frameRate(15);
}
void draw_sprite()
{
pushMatrix();
image(sprites[cs], 0, y,100,100);
popMatrix();
}
void ms()
{
cs += 1;
cs %= ts;
}
void move(int index, int w, int h, PImage [] sprites, PImage spritesheet)
{
for(int y = 0; y < ny; y++)
{
for(int x = 0; x < nx; x++)
{
sprites[index] = spritesheet.get(x * w, dir * h, w, h);
index++;
}
}
}
intnx=6;
int-ny=4;
int cs;
int ts=nx*ny;
int y=0;
int dir=0;
PImage[]精灵;
无效设置()
{
尺寸(600600);
sprites=新皮麦格[ts];
PImage spritesheet=loadImage(“https://denysalmaral.com/wp-
content/uploads/2017/02/trump_run.png);
int w=spritesheet.width/nx;
int h=spritesheet.height/ny;
int指数=0;
移动(索引、w、h、精灵、精灵表);
}
作废提款()
{
背景(0);
ms();
绘制精灵();
帧率(15);
}
void draw_sprite()
{
pushMatrix();
图像(精灵[cs],0,y,100100);
popMatrix();
}
void ms()
{
cs+=1;
cs%=ts;
}
无效移动(整数索引、整数w、整数h、PImage[]精灵、PImage精灵表)
{
对于(int y=0;y
我希望在spritesheet的行数之间进行更改,但它只是停留在dir=0
它不会改变,例如:dir=2
sprites[index] = spritesheet.get(x * w, dir * h, w, h);
为此:
sprites[index] = spritesheet.get(x * w, y * h, w, h);
将所有行添加到精灵表
如果要根据按下的键更改行,请将其添加到void keyPressed()中:
改变
image(sprites[cs], 0, y,100,100);
到
编辑:您还必须进行更改
cs %= ts;
到
这是因为如果不这样做,您将遍历所有行,但您只希望一次遍历一行;image()中的行已更改。您好,谢谢您的帮助!我只是有一些简短的问题。在图像中(精灵[cs+dir*(ny+2)],0,y,100100);如果dir=2或大于2,它将超出数组边界。是的,很抱歉,我忘记添加一些内容。在ms()中,更改cs%=ts;至cs%=nx;这应该可以解决问题。另外,请阅读
image(sprites[cs + dir * (ny + 2)], 0, y,100,100);
cs %= ts;
cs %= nx;