2012-02-09 343 views
0

以下代碼旨在使用圖像條和畫布上的一些指甲在屏幕上繪製數字,但不顯示。無法剪裁/裁剪圖像

1)當我評論/刪除「setSourceRect」時,它顯示完整的圖像。 2)initnails功能未顯示任何

package com.myGame.mavenproject3.core; 
import org.jbox2d.collision.shapes.CircleShape; 
import playn.core.*; 
import static playn.core.PlayN.assetManager; 
import static playn.core.PlayN.graphics; 

public class mavenproject3 implements Game { 
Canvas canvas; 
    private float radius; 
    Image pointsFontImage; 
GroupLayer pointsLayer; 
@Override 
    public void init() { 
// create and add background image layer 
int width = 640; 
int height = 480; 
CanvasImage bgImage = graphics().createImage(width, height); 
canvas = bgImage.canvas(); 
canvas.setFillColor(0xff87ceeb); 
canvas.fillRect(0, 0, width, height); 
ImageLayer bg = graphics().createImageLayer(bgImage); 
graphics().rootLayer().add(bg); 
pointsLayer = graphics().createGroupLayer(); 
    pointsLayer.setScale(3.0f, 3.0f); 
    pointsFontImage = assetManager().getImage("images/font.png"); 
    graphics().rootLayer().add(pointsLayer); 
    } 
    @Override 
    public void paint(float alpha) { 
// the background automatically paints itself, so no need to do 
anything here! 
    } 
int points = 50; 
    @Override 
    public void update(float delta) { 
    initNails(); 
    float x = 0f; 
    pointsLayer.clear(); 
    for (char c : Integer.toString(points).toCharArray()) { 
    ImageLayer digit = graphics().createImageLayer(pointsFontImage); 
    digit.setSourceRect(((c - '0' + 9) % 10) * 16, 0, 16, 16); 
    pointsLayer.add(digit); 
    digit.setTranslation(x, 0f); 
    x += 16f; 
    } 
    } 
    @Override 
    public int updateRate() { 
    return 25; 
    } 
    public void initNails() { 
    for (int x = 100; x < 300 - 100; x += 50) { 
    for (int y = 150; y < 450; y+= 100) { 
     canvas.setFillColor(0xffaaaaaa); 
     canvas.fillCircle(x, y, radius); 
    CircleShape circleShape = new CircleShape(); 
    circleShape.m_radius = 5f*2; 
    circleShape.m_p.set(x*2, 
         y*2); 
    } 
    } 
    } 
} 

回答

0

除了設置源rect,需要設置用於圖像層的目標尺寸,否則會是相同的原始圖像的尺寸。原始圖像爲160x16,但要渲染單個字符,所以你應該有16×16:

ImageLayer digit = graphics().createImageLayer(pointsFontImage); 
digit.setSourceRect(((c - '0' + 9) % 10) * 16, 0, 16, 16); 
digit.setWidth(16); 
digit.setHeight(16); 
pointsLayer.add(digit); 
digit.setTranslation(x, 0f); 

我也無法抗拒指出,這是巨大低效的多種方式。

update()方法每秒運行60次(在理想的世界中)。您無需在每一幀創建和銷燬圖像層。您應該爲每個潛在的數字創建一次圖像層,然後每幀更新其源代碼。您也以極其昂貴的方式獲取您的points價值的數字。

下面的代碼是不太可能讓一個遊戲開發商自己哭,晚上睡覺:

int points = 50; 

final float DIGIT_WIDTH = 16; 
final float DIGIT_HEIGHT = 16; 
final int MAX_DIGITS = 5; 

@Override 
public void update(float delta) { 
    initNails(); 

    // peel off the right-most digit of points and update its layer; 
    // creating the layer on demand if we don't have one yet 
    int ll = 0; 
    for (int p = points; p > 0 || ll == 0; p /= 10, ll++) { 
    ImageLayer digit; 
    if (pointsLayer.size() <= ll) { 
     digit = graphics().createImageLayer(pointsFontImage); 
     digit.setWidth(DIGIT_WIDTH); 
     digit.setHeight(DIGIT_HEIGHT); 
     digit.setTranslation((MAX_DIGITS-ll-1) * DIGIT_WIDTH, 0); 
     pointsLayer.add(digit); 
    } else { 
     digit = (ImageLayer)pointsLayer.get(ll); 
     digit.setVisible(true); 
    } 
    int d = p%10, ix = ((d + 9) % 10); 
    digit.setSourceRect(ix * DIGIT_WIDTH, 0, DIGIT_WIDTH, DIGIT_HEIGHT); 
    } 
    // hide layers for leading zeros 
    while (ll < pointsLayer.size()) { 
    pointsLayer.get(ll++).setVisible(false); 
    } 
}