LibGDX游戏引擎-7-演员类的使用(Actor)

qsuron 发布于 2014-03-18 libGDX框架 32 次阅读 无~ 2410 字 预计阅读时间: 11 分钟


完整的例子代码如下:

 

主类:

 

package com.qsuron;

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.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class MyDemo implements ApplicationListener{
SpriteBatch batch;
Image background;
Stage stage;

@Override
public void create() {

batch = new SpriteBatch();
background = new Image(new Texture(Gdx.files.internal("data/background.jpg")));
stage = new Stage(800, 480, false);
Gdx.input.setInputProcessor(stage);
Person p = new Person(100,170);
stage.addActor(background);
stage.addActor(p);
stage.addActor(p.btn_L);
stage.addActor(p.btn_R);
}

@Override
public void dispose() {
batch.dispose();
}

@Override
public void render() {

Gdx.gl.glClearColor(0,0,0, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

}

 

 

演员类:

package com.qsuron;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class Person extends Actor {
public float x;
public float y;
public float statetime;

//按钮
ImageButton  btn_L;
ImageButton  btn_R;

//人物当前状态(枚举类)
STATE state;

//对应的动画
Animation animation_left;
Animation animation_right;
Animation animation_idle;

//实时动画
TextureRegion currentFrame;

public Person(float x, float y) {
super();
this.x = x;
this.y = y;
this.statetime = 0;
this.show();
state = STATE.I;
}

public void show() {

//分割动画
Texture animation_file = new Texture(Gdx.files.internal("data/animation2-2.png"));
TextureRegion[][] animation_split_right = TextureRegion.split(animation_file,42,51);
TextureRegion[][] animation_split_left = TextureRegion.split(animation_file,42,51);
Texture animation_file2 = new Texture(Gdx.files.internal("data/idle.png"));
TextureRegion[][] animation_split_idle = TextureRegion.split(animation_file2,42,51);

//实例化动画图片组
TextureRegion[] textureRegion_right = new TextureRegion[30];
TextureRegion[] textureRegion_left = new TextureRegion[30];
TextureRegion[] textureRegion_idle = new TextureRegion[1];

//向右动画图片组
int i = 0;
for(TextureRegion[] animation_split_right_split:animation_split_right){
for(TextureRegion animation_pic:animation_split_right_split){
textureRegion_right[i] = animation_pic;
i++;
}
}

//向左动画图片组
i = 0;
for(TextureRegion[] animation_split_left_split:animation_split_left){
for(TextureRegion animation_pic:animation_split_left_split){
textureRegion_left[i] = animation_pic;
textureRegion_left[i].flip(true,false);
i++;
}
}

//原地动画图片组
textureRegion_idle[0] = animation_split_idle[0][0];

//合成动画
animation_right = new Animation(0.08f,textureRegion_right);
animation_right.setPlayMode(Animation.LOOP);
animation_left = new Animation(0.08f,textureRegion_left);
animation_left.setPlayMode(Animation.LOOP);
animation_idle = new Animation(1f,textureRegion_idle);
animation_idle.setPlayMode(Animation.LOOP);

//按钮图片读取
Texture button_file = new Texture(Gdx.files.internal("data/button.png"));
TextureRegion[][] button_split = TextureRegion.split(button_file,100,100);

//按钮实例化
btn_R = new ImageButton(
new TextureRegionDrawable(button_split[0][0]),
new TextureRegionDrawable(button_split[0][1]));
btn_L = new ImageButton(
new TextureRegionDrawable(button_split[0][2]),
new TextureRegionDrawable(button_split[0][3]));

//按钮位置
btn_R.setPosition(150,20);
btn_L.setPosition(20,20);

//按钮监听
btn_R.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
state = STATE.R;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
state = STATE.I;
super.touchUp(event, x, y, pointer, button);
}
});

btn_L.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
state = STATE.L;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
state = STATE.I;
super.touchUp(event, x, y, pointer, button);
}
});
}

//状态控制与动画选择
public void getFrame() {
if(this.state==STATE.L){
currentFrame = animation_left.getKeyFrame(statetime,true);
}else if(this.state==STATE.R){
currentFrame = animation_right.getKeyFrame(statetime,true);
}else if(this.state==STATE.I){
currentFrame = animation_idle.getKeyFrame(statetime,true);
}
}

//人物移动
public void update(){
if(state==STATE.L && this.x>20){
this.x -= 1f;
}else if(state==STATE.R && this.x<700){
this.x += 1f;
}
}

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
statetime += Gdx.graphics.getDeltaTime();
this.update();
this.getFrame();
batch.draw(currentFrame, x, y);
}

enum STATE {
L,R,I;
};

}

 

 

代码敲的累了,换个中文悠闲悠闲。
最后更新于 2014-03-18