libgdx学习记录5——演员Actor

Actor也是libgdx中非常重要的一个元素,一般与stage配合一起使用。Actor能够设置大小,位置,旋转和动画等。

我们自定义的Actor一般需要继承于Actor,并且重写其中的act和draw方法。

自定义的actor是一个图片。

 1 class MyActor extends Actor{
 2     TextureRegion region;
 3     
 4     public MyActor(){
 5         Texture texture = new Texture( Gdx.files.internal( "data/badlogic.jpg" ) );
 6         region = new TextureRegion( texture );
 7         setSize( region.getRegionWidth()/2, region.getRegionHeight()/2 );
 8         setOrigin( getWidth()/2, getHeight()/2 );
 9     }
10 
11     @Override
12     public void act(float delta) {
13         // TODO Auto-generated method stub
14         super.act(delta);
15     }
16     
17     @Override
18     public void draw(SpriteBatch batch, float parentAlpha) {
19         // TODO Auto-generated method stub
20         batch.draw( region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation() );
21         super.draw(batch, parentAlpha);
22     }
23 
24     public void dispose(){
25         region.getTexture().dispose();
26     }
27 }

主类,包含stage:

 1 package com.fxb.newtest;
 2 import com.badlogic.gdx.ApplicationAdapter;
 3 import com.badlogic.gdx.Gdx;
 4 import com.badlogic.gdx.graphics.Color;
 5 import com.badlogic.gdx.graphics.GL10;
 6 import com.badlogic.gdx.graphics.Texture;
 7 import com.badlogic.gdx.graphics.g2d.BitmapFont;
 8 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 9 import com.badlogic.gdx.graphics.g2d.TextureRegion;
10 import com.badlogic.gdx.scenes.scene2d.Actor;
11 import com.badlogic.gdx.scenes.scene2d.InputEvent;
12 import com.badlogic.gdx.scenes.scene2d.InputListener;
13 import com.badlogic.gdx.scenes.scene2d.Stage;
14 import com.badlogic.gdx.scenes.scene2d.ui.Image;
15 
16 public class Lib004_Actor extends ApplicationAdapter{
17 
18     BitmapFont font;
19     Stage stage;
20     MyActor actor;
21     //String strShow;
22     int count;
23     
24     @Override
25     public void create() {
26         // TODO Auto-generated method stub
27         stage = new Stage();
28         font = new BitmapFont();
29         font.setColor( Color.DARK_GRAY );
30         
31         actor = new MyActor();
32         stage.addActor( actor );
33         actor.setPosition( stage.getWidth()/2-actor.getWidth()/2, stage.getHeight()/2-actor.getHeight()/2 );
34         
35         count = 0;
36         actor.addListener(new InputListener(){
37             @Override
38             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
39                 // TODO Auto-generated method stub
40                 count++;
41                 return true;
42             }            
43         });
44         
45         Gdx.input.setInputProcessor( stage );
46     }
47 
48     @Override
49     public void render() {
50         // TODO Auto-generated method stub
51         Gdx.gl.glClearColor( 1, 1, 1, 1 );
52         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
53         
54         stage.act();
55         stage.draw();
56         
57         SpriteBatch batch = stage.getSpriteBatch();
58         batch.begin();
59         //batch.draw( font, actor.getX(),  );
60         font.draw( batch, "You have clicked  " + count + "  times!", actor.getX()-25, actor.getY()-20 );
61         batch.end();
62         
63     }
64 
65     @Override
66     public void dispose() {
67         // TODO Auto-generated method stub
68         super.dispose();
69     }
70 
71 }

运行结果:

posted @ 2014-05-20 21:38  丛林小阁楼  阅读(1433)  评论(0编辑  收藏  举报