Android游戏引擎《Rokon》学习笔记六:一个使用Box2D的Rokon小例子:Rokon Donate

RokonDemo《Rokon Donate》

懒骨头(http://blog.csdn.com/iamlazybone)

 

Rokon:http://rokonandroid.com/

SVN:http://rokon.googlecode.com/svn/examples/donate_app

————————————————

 

 

程序运行截图:

 

Android游戏引擎《Rokon》学习笔记六:一个使用Box2D的Rokon小例子:Rokon Donate_第1张图片

 

 

程序很简单,三个类,Donate是捐赠的意思。

 

第一个类是RokonActivity.java ,入口类,负责创建引擎加载图片资源:

 

import com.stickycoding.rokon.DrawPriority; import com.stickycoding.rokon.RokonActivity; public class Donate extends RokonActivity { public static final float GAME_WIDTH = 8f; public static final float GAME_HEIGHT = 4.8f; private DonateScene scene; public void onCreate() { // 调试模式 debugMode(); // 强制全屏 forceFullscreen(); // 强制横屏 forceLandscape(); // 设置游戏尺寸 setGameSize(GAME_WIDTH, GAME_HEIGHT); // 使用VBO‘S渲染方式 setDrawPriority(DrawPriority.PRIORITY_VBO); // 设置图片路径 setGraphicsPath("textures/"); // 创建引擎 createEngine(); } // 引擎创建完成时调用 public void onLoadComplete() { // 加载贴图资源 Textures.load(); // 设置场景 setScene(scene = new DonateScene()); } } 

 

然后是Textures.java,加载资源的具体实现类。

 

import com.stickycoding.rokon.Texture; import com.stickycoding.rokon.TextureAtlas; public class Textures { // 静态的资源管理类 public static TextureAtlas atlas; // 资源类 public static Texture background, box; public static void load() { atlas = new TextureAtlas(); atlas.insert(background = new Texture("background.png")); atlas.insert(box = new Texture("box.png")); // 资源管理类调用complete()方法之后无法再添加其他资源类。 atlas.complete(); } }  

 

第三个类DonateScene.java,场景类,主要是往世界里添加精灵。

 

package com.stickycoding.rokondonatefree; import android.util.Log; import android.view.MotionEvent; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.World; import com.stickycoding.rokon.PhysicalSprite; import com.stickycoding.rokon.Scene; import com.stickycoding.rokon.background.FixedBackground; public class DonateScene extends Scene { // 小方块的个数,数量加大,卡的明显(骨头测试手机G2) private static final int BOX_COUNT = 6; // Box2D的世界类, private World world; // 静态的背景 private FixedBackground background; // 向量类 private Vector2 gravity; // 精灵类 private PhysicalSprite[] wall, box; public DonateScene() { // 创建场景(层的个数,精灵饿个数) super(3, new int[] { 1, BOX_COUNT, 1 }); setBackground(background = new FixedBackground(Textures.background)); // gravity参数,经测试,是设置原点坐标,重力方向。 setWorld(world = new World(gravity = new Vector2(0, 0), false)); createWalls(); createBoxes(); } // 由于要Box2d效果,所以需要建四面墙。 private void createWalls() { wall = new PhysicalSprite[4]; wall[0] = new PhysicalSprite(0, -1, Donate.GAME_WIDTH, 1); wall[0].createStaticBox(); add(0, wall[0]); wall[1] = new PhysicalSprite(Donate.GAME_WIDTH, 0, 1, Donate.GAME_HEIGHT); wall[1].createStaticBox(); add(0, wall[1]); wall[2] = new PhysicalSprite(0, Donate.GAME_HEIGHT, Donate.GAME_WIDTH, 1); wall[2].createStaticBox(); add(0, wall[2]); wall[3] = new PhysicalSprite(-1, 0, 1, Donate.GAME_HEIGHT); wall[3].createStaticBox(); add(0, wall[3]); } // 建立BOX_COUNT个精灵。 private void createBoxes() { box = new PhysicalSprite[BOX_COUNT]; for (int i = 0; i < BOX_COUNT; i++) { box[i] = new PhysicalSprite((float) Math.random() * Donate.GAME_WIDTH, (float) Math.random() * Donate.GAME_HEIGHT, 0.2f + (float) Math.random() * 0.5f, 0.2f + (float) Math.random() * 0.5f); box[i].setTexture(Textures.box); box[i].setRGBA((float) Math.random(), (float) Math.random(), (float) Math.random(), 1); box[i].createDynamicBox(); // 给没有贴图的精灵加边框 box[i].setBorder(0, 0, 0, 1); // 添加到第2层 add(1, box[i]); } } int x = 0, y = 0; @Override public void onGameLoop() { } @Override public void onPause() { } @Override public void onResume() { } @Override public void onReady() { } @Override public void onTouchDown(float x, float y, MotionEvent event, int pointerCount, int pointerId) { // 测试gravity参数方法:作用为设置重力方向。 gravity.set(8 - (float) Math.random() * 16, 8 - (float) Math.random() * 16); world.setGravity(gravity); Log.i("", "xx=" + world.getGravity().x + " yy=" + world.getGravity().y); } }  

 

 

 

你可能感兴趣的:(游戏,android,import,float,引擎,textures)