2014年6月26日星期四

【libgdx】血条的制作

本文使用libGdx 0.9.9

】首先准备2张图片: 空血 & 满血
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

private int hpWidth = 344;
private int hpHeight = 24;

@Override
public void create()
{

    batch = 
new SpriteBatch();
    
// 载图
    textureFu = new Texture(Gdx.files.internal("data/images/hp_full.png"));
    
// 抗锯齿
    textureFu.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    textureEp = 
new Texture(Gdx.files.internal("data/images/hp_empty.png"));
    textureEp.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    
//把满血图转为可截取的TextureRegion,默认为从右向左截取;
    region = new TextureRegion(textureFu, 00, hpWidth, hpHeight);

    
// 监听按钮
    Gdx.input.setInputProcessor(this);
}


@Override
public void render()
{
    Gdx.gl.glClearColor(.4f, .4f, .4f, .4f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    
// textureEp 空血条不需要动
    batch.draw(textureEp, 30030034424);

    
// 动态调整满血条宽度
    region.setRegionWidth(hpWidth);
    batch.draw(region, 
300300, hpWidth, 24);

    batch.end();
}

运行
恩? 太生硬? 加入动画好了。
另外实现了InputProcessor接口,监听键盘事件,用于调试。
 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//复制一个血长
private int hpWidthC = hpWidth;

@Override
public void render()
{
    ...
    ..
    ...
    
//动画
    handelInput();
}


public void handelInput()
{
    
if (hpWidth > hpWidthC && hpWidth > 0)
    {
        
//据说这样写可以保证各计算机运算速度一致?
        hpWidth -= Gdx.graphics.getDeltaTime() * 100;
    }
}

@Override
public boolean keyDown(int keycode)
{

    
switch (keycode)
    {

    
case Input.Keys.A:
        hpWidthC -= 
80//模拟掉血
        break;

    }
    
return true;
}

运行







没有评论:

发表评论