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 @   丛林小阁楼  阅读(664)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示