】首先准备2张图片: 空血 & 满血
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, 0, 0, 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, 300, 300, 344, 24); // 动态调整满血条宽度 region.setRegionWidth(hpWidth); batch.draw(region, 300, 300, hpWidth, 24); batch.end(); } |
Java Code
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; } |
运行
没有评论:
发表评论