libgdx 学习笔记(三)四个方向键控制物体移动

libgda入口类:Cuboc 

public class Cuboc extends AndroidApplication {
/** Called when the activity is first created. */

@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialize(new ActorDemo(), true);
}
}



ActorDemo 类:


import android.util.Log;


import com.badlogic.cuboc.demo.actor.FirstActor;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
/***
 * 四个方向键移动
 * @author cui.li
 *
 */
public class ActorDemo implements ApplicationListener, ClickListener {
private static String UP = "up";
private static String DOWN = "down";
private static String LEFT = "left";
private static String RIGHT = "right";
// 舞台
private Stage stage;
// 演员
private Actor firstActor;
private Texture texture;
private Button buttonUp, buttonDown, buttonLeft, buttonRight;
private NinePatch patch1, patch2;
private int screenWidth;
private int screenHeight;


@Override
public void create() {
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),
true);
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
firstActor = new FirstActor("ceshi");
buttonUp = initButton(UP, 68, 130);
buttonDown = initButton(DOWN, 68, 0);
buttonLeft = initButton(LEFT, 0, 68);
buttonRight = initButton(RIGHT, 130, 68);


buttonUp.setClickListener(this);
buttonDown.setClickListener(this);
buttonLeft.setClickListener(this);
buttonRight.setClickListener(this);


stage.addActor(firstActor);
stage.addActor(buttonUp);
stage.addActor(buttonDown);
stage.addActor(buttonLeft);
stage.addActor(buttonRight);


Gdx.input.setInputProcessor(stage);


}


@Override
public void resize(int width, int height) {
Log.i("", "width: " + width + " height: " + height);
}


@Override
public void render() {
// 清屏
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();


}


@Override
public void pause() {


}


@Override
public void resume() {


}


@Override
public void dispose() {
// 释放占用的资源
stage.dispose();
}


public Button initButton(String name, int x, int y) {
if (name.equals(UP)) {
texture = new Texture(Gdx.files.internal("data/texture/up_alt.png"));
} else if (name.equals(DOWN)) {
texture = new Texture(
Gdx.files.internal("data/texture/down_alt.png"));
} else if (name.equals(LEFT)) {
texture = new Texture(
Gdx.files.internal("data/texture/back_alt.png"));
} else if (name.equals(RIGHT)) {
texture = new Texture(
Gdx.files.internal("data/texture/forward_alt.png"));
}
patch1 = new NinePatch(texture, 0, 0, 0, 0);
Button button = new Button(new ButtonStyle(patch1, patch1, patch1, 0f,
0f, 0f, 0f), name);
button.x = x;
button.y = y;
button.width = 128;
button.height = 128;
return button;
}


@Override
public void click(Actor button, float x, float y) {
Actor actor = null;
if (button.equals(buttonUp)) {
actor = button.parent.findActor("ceshi");
if (actor.y + actor.height <= screenHeight) {
actor.y += 10;
}
} else if (button.equals(buttonDown)) {
actor = button.parent.findActor("ceshi");
if (actor.y >= 0) {
actor.y -= 10;
}

} else if (button.equals(buttonLeft)) {
actor = button.parent.findActor("ceshi");
if (actor.x - 10 >= 0) {
actor.x -= 10;
}
} else if (button.equals(buttonRight)) {
actor = button.parent.findActor("ceshi");
if (actor.x + actor.width <= screenWidth) {
actor.x += 10;
}
}
}


}


FirstActor 类:

public class FirstActor extends Actor {


private Texture texture;



@Override
public void draw(SpriteBatch batch, float arg1) {

batch.draw(texture, this.x, this.y);


}


@Override
public Actor hit(float arg0, float arg1) {
if (x > 0 && y > 0 && this.height > y && this.width > x) {
return this;
} else {
return null;
}
}


@Override
public boolean touchDown(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}


@Override
public void touchDragged(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub


}


@Override
public void touchUp(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub


}


public FirstActor(String name) {
super(name);
texture = new Texture(Gdx.files.internal("data/texture/bt.png"));
this.height = texture.getHeight();
this.width = texture.getWidth();
}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ada

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值