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

时间:2016-03-20 22:10:59

标签: java processing

标题可能有点令人困惑,但我试图从精灵表中获取一个步行动画。它有8个不同的行走位置,似乎每次它改变我必须再次加载精灵表作为只要裁剪原始如果你得到我的意思,否则它不会显示。

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帧来加载图像,这样就可以了,所以我不可能获得平稳的步行速度。任何想法都是适当的

2 个答案:

答案 0 :(得分:0)

好的,我正在裁剪原始图像并将其保存为自身与临时PImage相关,我是个白痴

答案 1 :(得分:0)

我发现您已经解决了问题,但我会回答一些可能让您的生活更轻松的事情:不要拨打{{1}来自draw()函数的函数!改为从setup()函数调用它。

loadImage()调用draw()功能会导致您的图像一遍又一遍地加载,每秒60次,所以这真的很浪费。只需在启动时加载它们即可。

您可能想要创建arrayArrayList PImages,每个都加载一个精灵。从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);
}

我还没有真正测试过这段代码,但这显示了基本方法。

您还可以查看this Processing example中的示例,您可以通过转到File - >来处理编辑器查看该示例。 Examples

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