2016-03-20 30 views
0

標題可能有點混淆,但即時通訊試圖從精靈表獲取步行動畫。它有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幀所以它不可能在圖像中加載我順利拿到了行走速度這將是確定的。任何想法appriciated

回答

0

好吧,我被裁剪的原始圖像,並將其保存到自己作爲放在附近一個臨時PImage,即時通訊白癡

0

我看你已經能解決問題,但我會用一些可能會讓你的生活變得更輕鬆的東西來回答:不能從draw()函數調用loadImage()函數!改爲從setup()函數調用它。

draw()調用loadImage()函數會導致您的圖像一次又一次地被加載,每秒60次,所以這非常浪費。只需在啓動時加載它們。

您可能想要創建一個arrayArrayListPImages,每個加載一個精靈。從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中的示例,您可以通過處理編輯器查看FileExamples查看示例。

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

謝謝,我將它們放在數組列表,但加載圖像時,精靈變化,每5幀所以它不加載相同的圖像不止一次如果你看到我的意思 – Will

+0

纔會被調用@會的,它會多次加載相同的圖像。您應該只爲每個圖像調用一次'loadImage()'。這裏你每次改變它都會加載它,這實際上效率很低。只需將圖像保存在內存中,並在需要時將其交換出去。即使你只改變每5幀,每秒仍然有12個不必要的負載。 –

+0

哦,我明白你的意思了,是的,我現在在設置中加載一次,每當它改變而不是我上面發佈的代碼時,就裁剪它,謝謝:) – Will

相關問題