libgdx学习记录4——舞台Stage

libgdx总的来说是一个框架,而不是一个成熟的游戏引擎。Stage是其中一个比较好的封装,里面自带Camera、SpriteBatch等常用渲染绘图工具。

下面是一个简单的添加图片,并让镜头左右上下移动以显示整副图片的例子。

 1 package com.fxb.newtest;
 2 
 3 import com.badlogic.gdx.ApplicationAdapter;
 4 import com.badlogic.gdx.Gdx;
 5 import com.badlogic.gdx.graphics.GL10;
 6 import com.badlogic.gdx.graphics.Texture;
 7 import com.badlogic.gdx.scenes.scene2d.Stage;
 8 import com.badlogic.gdx.scenes.scene2d.ui.Image;
 9 
10 public class Lib003_Stage extends ApplicationAdapter{
11 
12     Stage stage;
13     Image img;
14     float leftX = 0;
15     float bottomY = 0;
16     
17     enum Direct{ move_right, move_left, move_up, move_down };
18     Direct direct = Direct.move_right;
19     
20     @Override
21     public void create() {
22         // TODO Auto-generated method stub
23         stage = new Stage( Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
24         img = new Image( new Texture( Gdx.files.internal( "data/pal_4.jpg" ) ) );
25         
26         img.setPosition(0,0);
27         stage.addActor( img );
28     }
29 
30     @Override
31     public void render() {
32         // TODO Auto-generated method stub
33         Gdx.gl.glClearColor( 1, 1, 1, 1 );
34         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
35         
36             
37         if(  direct==Direct.move_right ){
38             if( leftX + stage.getWidth()<img.getRight() ){
39                 stage.getCamera().translate( 2f, 0, 0 );
40                 leftX += 2f;
41             }
42             else{
43                 direct = Direct.move_up;
44             }
45         }        
46         else if( direct == Direct.move_left ){
47             if( leftX > 0 ){
48                 stage.getCamera().translate( -2f, 0, 0 );
49                 leftX -= 2f;                
50             }
51             else{
52                 direct = Direct.move_down;
53             }
54         }
55         else if( direct == Direct.move_up ){
56             if( bottomY + stage.getHeight() < img.getTop() ){
57                 stage.getCamera().translate( 0, 1.5f, 0 );
58                 bottomY += 1.5f;                
59             }
60             else{
61                 direct = Direct.move_left;
62             }
63         }
64         else if( direct == Direct.move_down ){
65             if( bottomY > 0 ){
66                 stage.getCamera().translate( 0, -1.5f, 0 );
67                 bottomY -= 1.5f;                
68             }
69             else{
70                 direct = Direct.move_right;
71             }
72         }        
73         
74         stage.act();
75         stage.draw();
76     }
77 
78     @Override
79     public void dispose() {
80         // TODO Auto-generated method stub
81         super.dispose();
82     }
83 
84 }

运行效果:

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