Java 处理、加载图像并在每次图像更改时显示

Java 处理、加载图像并在每次图像更改时显示,java,processing,Java,Processing,标题可能有点混乱,但我正试图从一个精灵表中获得一个行走动画。它有8种不同的行走姿势,似乎每次它改变,我必须再次加载精灵表作为一个apose,只是修剪原始的,如果你得到我的意思,否则它不会显示 PImage Body; int WidthSpacing = 64; int HeightSpacing = 64; int XCharacter = 1; int YCharacter = 10; int WalkingCounter = 0; int WalkingSpeed = 2; void

标题可能有点混乱,但我正试图从一个精灵表中获得一个行走动画。它有8种不同的行走姿势,似乎每次它改变,我必须再次加载精灵表作为一个apose,只是修剪原始的,如果你得到我的意思,否则它不会显示

PImage Body;

int WidthSpacing = 64;
int HeightSpacing = 64;
int XCharacter = 1;
int YCharacter = 10;

int WalkingCounter = 0;
int WalkingSpeed = 2;

void setup()
{
background(200);
size (350, 240);
Body = loadImage("\\Sprites\\Player\\Male\\Default\\Light.png");
}

void draw()
{
WalkAnimation();
}

void WalkAnimation()
{
WalkingCounter++;

if (WalkingCounter == 1 * WalkingSpeed) { XCharacter = 1; LoadBody(); }
if (WalkingCounter == 2 * WalkingSpeed) { XCharacter = 2; LoadBody(); }  
if (WalkingCounter == 3 * WalkingSpeed) { XCharacter = 3; LoadBody(); }
if (WalkingCounter == 4 * WalkingSpeed) { XCharacter = 4; LoadBody(); }
if (WalkingCounter == 5 * WalkingSpeed) { XCharacter = 5; LoadBody(); }
if (WalkingCounter == 6 * WalkingSpeed) { XCharacter = 6; LoadBody(); }
if (WalkingCounter == 7 * WalkingSpeed) { XCharacter = 7; LoadBody(); }
if (WalkingCounter == 8 * WalkingSpeed) { XCharacter = 8; LoadBody(); WalkingCounter = 0; }    
}

void LoadBody()
{
background(200);
Body = loadImage("\\Sprites\\Player\\Male\\Default\\Light.png");
int X = XCharacter * WidthSpacing;
int Y = YCharacter * HeightSpacing;
Body = Body.get(X, Y, WidthSpacing, HeightSpacing);
Body.resize(200, 200);
image(Body, 150, 5);
}

这将是可以的,如果它不是处理需要大约20帧加载到一个图像,所以它不可能为我得到一个平稳的行走速度。任何想法都是值得欣赏的

好吧,我正在裁剪原始图像并将其保存为临时图片,我是个白痴

我知道你已经解决了你的问题,但我将用一些可能让你的生活更轻松的东西来回答:不要调用
绘图()中的
loadImage()
函数
功能
改为从
setup()
函数调用它

draw()
调用
loadImage()
函数会导致图像一次又一次地加载,每秒加载60次,因此这是非常浪费的。只需在启动时加载它们即可

您可能需要创建一个
数组
数组列表
,其中包含
图像
,每个图像都加载了一个精灵。从
setup()
函数加载所有元素,然后只需更改要绘制的
ArrayList
的索引即可更改精灵

下面是一个示例,说明如果采用这种方法,代码可能会是什么样子:

//use an array to hold your sprites
PImage[] bodies = new PImage[8];

//keep track of current index
int spriteIndex = 0;

//switch sprite every X frames
int walkingSpeed = 2;

void setup() {
  size (350, 240);

  int widthSpacing = 64;
  int heightSpacing = 64;
  int YCharacter = 10;

  //loop through every sprite
  for (int i = 0; i < 8; i ++) {

    //you could probably cut down on the loads further, 
    //but 8 at the beginning is much better than X per second
    bodies[i] = loadImage("\\Sprites\\Player\\Male\\Default\\Light.png");

    //use the loop variable to figure out where the sprite is in the main image
    int x = (i+1) * widthSpacing;
    int y = YCharacter * heightSpacing;

    //get the sprite
    bodies[i] = bodies[i].get(x, y, widthSpacing, heightSpacing);

    //resize the sprite
    bodies[i].resize(200, 200);
  }
}

void draw() {
  //draw the background first
  background(200);

  //use existing frameCount variable and modulus to switch sprites every X frames
  if (frameCount % walkingSpeed == 0) {

    //increment the sprite index
    spriteIndex++;

    //if it goes too high, reset it to zero
    if (spriteIndex == 8) {
      spriteIndex = 0;
    }
  }

  //just draw the image, you don't have to keep loading it!
  image(bodies[spriteIndex], 150, 5);
}

谢谢,我会把它们放在数组列表中,但是加载图像只有在精灵每5帧改变一次时才会被调用,所以如果你看到我所说的,它不会多次加载相同的图像mean@Will是的,它将多次加载同一图像。您应该只为每个图像调用一次
loadImage()
。在这里,每次更改时都要加载它,这是非常低效的。只需将图像保存在内存中,并在需要时将其调出即可。即使你只是每5帧更改一次,那仍然是每秒12次不必要的加载。哦,我明白你的意思了,是的,我现在在设置中加载了一次,每次更改都会裁剪它,而不是我上面发布的代码,谢谢:)@这看起来仍然很低效。只需在前面进行所有加载和裁剪。我将尝试编辑我的答案以包含一些示例代码。@我将编辑我的答案以包含代码示例。
Animation animation1, animation2;

float xpos;
float ypos;
float drag = 30.0;

void setup() {
  size(640, 360);
  background(255, 204, 0);
  frameRate(24);
  animation1 = new Animation("PT_Shifty_", 38);
  animation2 = new Animation("PT_Teddy_", 60);
  ypos = height * 0.25;
}

void draw() { 
  float dx = mouseX - xpos;
  xpos = xpos + dx/drag;

  // Display the sprite at the position xpos, ypos
  if (mousePressed) {
    background(153, 153, 0);
    animation1.display(xpos-animation1.getWidth()/2, ypos);
  } else {
    background(255, 204, 0);
    animation2.display(xpos-animation1.getWidth()/2, ypos);
  }
}



// Class for animating a sequence of GIFs

class Animation {
  PImage[] images;
  int imageCount;
  int frame;

  Animation(String imagePrefix, int count) {
    imageCount = count;
    images = new PImage[imageCount];

    for (int i = 0; i < imageCount; i++) {
      // Use nf() to number format 'i' into four digits
      String filename = imagePrefix + nf(i, 4) + ".gif";
      images[i] = loadImage(filename);
    }
  }

  void display(float xpos, float ypos) {
    frame = (frame+1) % imageCount;
    image(images[frame], xpos, ypos);
  }

  int getWidth() {
    return images[0].width;
  }
}