libGDX——舞台类的使用

1.Stage类

API定义:包含拥有层次结构的一个二维场景,场景中有许多演员。期处理视图和分配的输入事件。舞台负责操作视角,和处理分配输入事件。

功能作用:一个Stage可以 充满整个屏幕。设置视角(一般是浮点型和布尔类型),同时设置阶段内使用的相机,调配 Actor、Group 与 Screen 之间的关系转换(包括坐标)。一个Stage必须负责接收输入事件,同时将它分配给演员。这通常是 通过Stage的gdx.input.setinputprocessor来实现。一个inputmultiplexer可用来处理输入事件的不同阶段,即之前或之后。如果一个演员通过返回TRUE从输入的方法中处理一个事件,那么Stage的输入方法也会返回true,导致随后的inputprocessors不接收事件。

使用方法: Stage(float width, float height, boolean keepAspectRatio, SpriteBatch batch)  实例化大家都会,这里我主要讲解下这4个参数。

(1)第一个参数:设置舞台的宽度。

(2)第二个参数:设置舞台的高度。

(3)第三个参数:是负责是否让舞台铺满屏幕,如果这个值是ture的话,舞台的尺寸就是你设置的大       小。如果是false的话,就铺满屏幕。

(4)第四个参数:就是传入你所声明的Spirtibatch。

方法原理:Stage类中有许多方法,比较常用的有: act()、draw()、addlistener()、clear()、getroot()等方法。由于Stage类是和Actor类一同使用的,所要这些方法都是和演员类有直接的关系。这里我们拿act()方法来举例子,通过看包内的源码,我们可以知道stage调用act方法,实际上是通 知这个舞台内所有演员按照一定的规则,有序的调用自身的act()方法,其实stage.act()只是做了一件通知的事情,它通知所有的演员,你们需要调用act()了,有了这个通知所有的演员才去act(), 而不是stage去act。这样大家应该更能理解stage其实是负责管理,而不是一个实际的Actor了吧。


2.功能分析

这次我们通过一个游戏中的商店系统来实现多舞台的使用,同时了解舞台是如何切换的。一个简单的游戏商店需要有4个元素:
(1)商店按钮,按钮应该在游戏的画面内部。
(2)商店货架,陈列出售的物品。
(3)商品,出售的商品,点击商品需要有返回效果。
(4)购买结束状态,这里就弹出“购买成功”作为结束状态。

使用素材:
libGDX开发教程(九)-- <wbr>舞台类的使用 libGDX开发教程(九)-- <wbr>舞台类的使用

3.创建项目

我们还是使用上次教程的超级玛丽的例子,这次我们加入一个开始界面,和一个商店系统。

(1)首先加入一个开始画面,这里我们使用Image控件,将它铺满屏幕。同时我们再使用一个image控件,作为新游戏按钮,这里土豆只拿了一个图片加入监听的方式,为了节约时间。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用

(2)使用一个布尔类型的变量来控制舞台的切换,这里我们就使用GameIsRunning。为了是开始界面的舞台能启动,我们重写button的touchdown方法,将GameIsRunning赋值为true。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用

(3)在render()方法中进行判断,然后将openStage画出来。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用

libGDX开发教程(九)-- <wbr>舞台类的使用

(4)通过上面的办法,我们就初步的实现了一个双舞台的切换,首先是在开始舞台上,随后通过点击“新游戏”进入主游戏界面。

我们要做的游戏商店,上面也提到了,并不是仅仅有2个舞台,他有3个舞台以上,所以我们需要在主游戏舞台上面,再引入变量,来控制第二、第三个舞台的切换。其实原理是一样的。这里我们引入ShopIsRunning和SuccessIsRunning分别来控制shop舞台和success舞台。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用


(5)切换的原理和上面的一样。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用

libGDX开发教程(九)-- <wbr>舞台类的使用


(6)加入监听,同时通过重写touchdown方法来修改变量,从而控制舞台切换。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用

libGDX开发教程(九)-- <wbr>舞台类的使用

libGDX开发教程(九)-- <wbr>舞台类的使用

(7)游戏初始化和设置图片位置。

如图:

libGDX开发教程(九)-- <wbr>舞台类的使用

libGDX开发教程(九)-- <wbr>舞台类的使用

4.完整代码

注意:这个项目由于刚开始时间仓促,写的不是很好,特此土豆仔2014年2月3日,更改了一个新的游戏例子,总体代码还是相似,部分进行了修改游戏的项目也做了大的修改,所有大家不要疑惑,这里讲新的游戏代码共享给大家,希望大家能更好的理解游戏的Stage的使用。

完整代码:

(1)主类MyGame中的代码。

package  com. potato;

import  com.badlogic.gdx.ApplicationListener;
import  com.badlogic.gdx.Gdx;
import  com.badlogic.gdx.graphics.GL10;
import  com.badlogic.gdx.graphics.OrthographicCamera;
import  com.badlogic.gdx.graphics.Texture;
import  com.badlogic.gdx.graphics.Texture.TextureFilter;
import  com.badlogic.gdx.graphics.g2d.Sprite;
import  com.badlogic.gdx.graphics.g2d.SpriteBatch;
import  com.badlogic.gdx.graphics.g2d.TextureRegion;
import  com.badlogic.gdx.scenes.scene2d.Stage;
import  com.badlogic.gdx.scenes.scene2d.ui.Image;
import  com.sun.corba.se.impl.orbutil.closure.Constant;

public  class  MyGame  implements  ApplicationListener  {

     GameStage  gameStage;
     StartStage  startStage;
     StoreStage  storeStage;
    
    
     @Override
     public  void  create()            
           gameStage  new  GameStage();
           startStage  new  StartStage();
           storeStage  new  StoreStage();
          
     }

     public  void  selectStageRender(){
          
           if(Constants. Stageflag  ==  Constants. StartStageOn){
                 Gdx. input. setInputProcessor(startStage);
                 startStage. act();
                 startStage. draw();
           } else  if  (Constants. Stageflag  ==  Constants. GameStageOn{
                 Gdx. input. setInputProcessor(gameStage);
                 gameStage. act();
                 gameStage. draw();
           } else  if  (Constants. Stageflag  ==  Constants. StoreStageOn{
                 Gdx. input. setInputProcessor(storeStage);
                 storeStage. act();
                 storeStage. draw();
           }
     }
     @Override
     public  void  dispose()  {

     }

     @Override
     public  void  render()            
           Gdx. gl. glClearColor( 1 1 1 1);
           Gdx. gl. glClear(GL10. GL_COLOR_BUFFER_BIT);
          
           selectStageRender();
          
     }

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

     @Override
     public  void  pause()  {
     }

     @Override
     public  void  resume()  {
     }
}

(2)资源类,Constants类中代码。

package  com. potato;

public  class  Constants  {
     public  static  int  Stageflag  1;
     public  static  int  StartStageOn  1;
     public  static  int  GameStageOn  2;
     public  static  int  StoreStageOn  3;
}

(3)StartStage类中代码。

package  com. potato;

import  com.badlogic.gdx.Gdx;
import  com.badlogic.gdx.graphics.Texture;
import  com.badlogic.gdx.graphics.g2d.TextureRegion;
import  com.badlogic.gdx.scenes.scene2d.InputEvent;
import  com.badlogic.gdx.scenes.scene2d.InputListener;
import  com.badlogic.gdx.scenes.scene2d.Stage;
import  com.badlogic.gdx.scenes.scene2d.ui.Image;

public  class  StartStage  extends  Stage  {

     Texture  texture;
     TextureRegion  startRegion;
     Image  startImage;
     Image  newGameBtn;
     TextureRegion  newGameRegion;
    
     public  StartStage()  {
           super();
           init();
     }

     public  void  init()  {

           texture  new  Texture(Gdx. files. internal( "data/start.png"));
           startRegion  new  TextureRegion(texture,  0 0 800 480);
           startImage  new  Image(startRegion);
           startImage. setSize( 480 320);

           newGameRegion  new  TextureRegion(texture,  924 0 100 50);
           newGameBtn  new  Image(newGameRegion);
           newGameBtn. setPosition( 40 230); 
          
           this. addActor(startImage);
           this. addActor(newGameBtn);
          
           newGameBtn. addListener( new  InputListener(){

                 @Override
                 public  boolean  touchDown(InputEvent  event,  float  x,  float  y,
                             int  pointer,  int  button)  {
                       Constants. Stageflag  Constants. GameStageOn;
                      
                       return  true;
                                 
           });
     }
}

(4)GameStage类中代码。

package  com. potato;

import  com.badlogic.gdx.Gdx;
import  com.badlogic.gdx.graphics.Texture;
import  com.badlogic.gdx.graphics.g2d.TextureRegion;
import  com.badlogic.gdx.scenes.scene2d.InputEvent;
import  com.badlogic.gdx.scenes.scene2d.InputListener;
import  com.badlogic.gdx.scenes.scene2d.Stage;
import  com.badlogic.gdx.scenes.scene2d.ui.Image;

public  class  GameStage  extends  Stage  {

     // 背景图片截图->backdrop;
     Image  backdrop;
     Mario  mario;
     Texture  texture;
     TextureRegion  backdropRegion;
     Image  mushroom;
     TextureRegion  mushRegion;

     public  GameStage()  {
           this. init();
     }

     public  void  init()  {

           texture  new  Texture(Gdx. files. internal( "data/object.png"));
           backdropRegion  new  TextureRegion(texture,  512 256 512 128);
           backdrop  new  Image(backdropRegion);
           mushRegion  new  TextureRegion(texture,  204 0 102 85);
           mushroom  new  Image(mushRegion);

           mario  new  Mario( 100 190);
           backdrop. setPosition( 0 170);
           mushroom. setPosition( 290 60);

           this. addActor(backdrop);
           this. addActor(mario);
           this. addActor(mario. buttonL);
           this. addActor(mario. buttonR);
           this. addActor(mushroom);
           mushroom. addListener( new  InputListener(){

                 @Override
                 public  boolean  touchDown(InputEvent  event,  float  x,  float  y,
                             int  pointer,  int  button)  {
                      
                       Constants. Stageflag  Constants. StoreStageOn;
                       return  true;
                 }
                
                
           });

     }
}

(5)StoreStage类中代码。
package  com. potato;

import  com.badlogic.gdx.Gdx;
import  com.badlogic.gdx.graphics.Texture;
import  com.badlogic.gdx.graphics.g2d.TextureRegion;
import  com.badlogic.gdx.scenes.scene2d.InputEvent;
import  com.badlogic.gdx.scenes.scene2d.InputListener;
import  com.badlogic.gdx.scenes.scene2d.Stage;
import  com.badlogic.gdx.scenes.scene2d.ui.Image;

public  class  StoreStage  extends  Stage  {
    
     Texture  texture;
     TextureRegion  goodsShelfRegion;
     TextureRegion  goldRegion;
     TextureRegion  heartRegion;
     Image  goodsShelf;
     Image  gold;
     Image  heart;
    
     public  StoreStage()  {
           super();
           init();
     }
    

     public  void  init()  {
           // 商店道具纹理图片
           texture  new  Texture(Gdx. files. internal( "data/object.png"));
           // 金币纹理
           goodsShelfRegion  new  TextureRegion(texture,  0 85 510 350);
           goodsShelf  new  Image(goodsShelfRegion);
           goodsShelf. setSize( 480 320);;
           // 心形纹理
           heartRegion  new  TextureRegion(texture,  0 0 102 85);
           heart  new  Image(heartRegion);
          
           goldRegion  new  TextureRegion(texture,  102 0 102 85);
           gold  new  Image(goldRegion);
          
           gold. setPosition( 50 50);
           heart. setPosition( 190 50);
          
           this. addActor(goodsShelf);
           this. addActor(gold);
           this. addActor(heart);
          
           heart. addListener( new  InputListener(){

                 @Override
                 public  boolean  touchDown(InputEvent  event,  float  x,  float  y,
                             int  pointer,  int  button)  {
                       Gdx. app. exit();
                       return  true;
                 }
                
           });
          
           gold. addListener( new  InputListener(){

                 @Override
                 public  boolean  touchDown(InputEvent  event,  float  x,  float  y,
                             int  pointer,  int  button)  {
                      
                       Gdx. app. exit();
                       return  true;
                 }
                
           });

     }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值