RokonDemo《Rokon Donate》
懶骨頭(http://blog.csdn.com/iamlazybone)
Rokon:http://rokonandroid.com/
SVN:http://rokon.googlecode.com/svn/examples/donate_app
————————————————
程式運行:
程式很簡單,三個類,Donate是捐贈的意思。
第一個類是RokonActivity.java ,入口類,負責建立引擎載入圖片資源:
import com.stickycoding.rokon.DrawPriority;<br />import com.stickycoding.rokon.RokonActivity;<br />public class Donate extends RokonActivity {<br />public static final float GAME_WIDTH = 8f;<br />public static final float GAME_HEIGHT = 4.8f;<br />private DonateScene scene;<br />public void onCreate() {<br />// 偵錯模式<br />debugMode();<br />// 強制全屏<br />forceFullscreen();<br />// 強制橫屏<br />forceLandscape();<br />// 設定遊戲尺寸<br />setGameSize(GAME_WIDTH, GAME_HEIGHT);<br />// 使用VBO‘S渲染方式<br />setDrawPriority(DrawPriority.PRIORITY_VBO);<br />// 設定圖片路徑<br />setGraphicsPath("textures/");<br />// 建立引擎<br />createEngine();<br />}<br />// 引擎建立完成時調用<br />public void onLoadComplete() {<br />// 載入貼圖資源<br />Textures.load();<br />// 設定情境<br />setScene(scene = new DonateScene());<br />}<br />}
然後是Textures.java,載入資源的具體實作類別。
import com.stickycoding.rokon.Texture;<br />import com.stickycoding.rokon.TextureAtlas;<br />public class Textures {<br />// 靜態資源管理類<br />public static TextureAtlas atlas;<br />// 資源類<br />public static Texture background, box;<br />public static void load() {<br />atlas = new TextureAtlas();<br />atlas.insert(background = new Texture("background.png"));<br />atlas.insert(box = new Texture("box.png"));<br />// 資源管理類調用complete()方法之後無法再添加其他資源類。<br />atlas.complete();<br />}<br />}<br />
第三個類DonateScene.java,情境類,主要是往世界裡添加精靈。
package com.stickycoding.rokondonatefree;<br />import android.util.Log;<br />import android.view.MotionEvent;<br />import com.badlogic.gdx.math.Vector2;<br />import com.badlogic.gdx.physics.box2d.World;<br />import com.stickycoding.rokon.PhysicalSprite;<br />import com.stickycoding.rokon.Scene;<br />import com.stickycoding.rokon.background.FixedBackground;<br />public class DonateScene extends Scene {<br />// 小方塊的個數,數量加大,卡的明顯(骨頭測試手機G2)<br />private static final int BOX_COUNT = 6;<br />// Box2D的世界類,<br />private World world;<br />// 靜態背景<br />private FixedBackground background;<br />// 向量類<br />private Vector2 gravity;<br />// 精靈類<br />private PhysicalSprite[] wall, box;<br />public DonateScene() {<br />// 建立情境(層的個數,精靈餓個數)<br />super(3, new int[] { 1, BOX_COUNT, 1 });<br />setBackground(background = new FixedBackground(Textures.background));<br />// gravity參數,經測試,是設定原點座標,重力方向。<br />setWorld(world = new World(gravity = new Vector2(0, 0), false));<br />createWalls();<br />createBoxes();<br />}<br />// 由於要Box2d效果,所以需要建四面牆。<br />private void createWalls() {<br />wall = new PhysicalSprite[4];<br />wall[0] = new PhysicalSprite(0, -1, Donate.GAME_WIDTH, 1);<br />wall[0].createStaticBox();<br />add(0, wall[0]);<br />wall[1] = new PhysicalSprite(Donate.GAME_WIDTH, 0, 1,<br />Donate.GAME_HEIGHT);<br />wall[1].createStaticBox();<br />add(0, wall[1]);<br />wall[2] = new PhysicalSprite(0, Donate.GAME_HEIGHT, Donate.GAME_WIDTH,<br />1);<br />wall[2].createStaticBox();<br />add(0, wall[2]);<br />wall[3] = new PhysicalSprite(-1, 0, 1, Donate.GAME_HEIGHT);<br />wall[3].createStaticBox();<br />add(0, wall[3]);<br />}<br />// 建立BOX_COUNT個精靈。<br />private void createBoxes() {<br />box = new PhysicalSprite[BOX_COUNT];<br />for (int i = 0; i < BOX_COUNT; i++) {<br />box[i] = new PhysicalSprite((float) Math.random()<br />* Donate.GAME_WIDTH, (float) Math.random()<br />* Donate.GAME_HEIGHT, 0.2f + (float) Math.random() * 0.5f,<br />0.2f + (float) Math.random() * 0.5f);<br />box[i].setTexture(Textures.box);<br />box[i].setRGBA((float) Math.random(), (float) Math.random(),<br />(float) Math.random(), 1);<br />box[i].createDynamicBox();<br />// 給沒有貼圖的精靈加邊框<br />box[i].setBorder(0, 0, 0, 1);<br />// 添加到第2層<br />add(1, box[i]);<br />}<br />}<br />int x = 0, y = 0;<br />@Override<br />public void onGameLoop() {<br />}<br />@Override<br />public void onPause() {<br />}<br />@Override<br />public void onResume() {<br />}<br />@Override<br />public void onReady() {<br />}<br />@Override<br />public void onTouchDown(float x, float y, MotionEvent event,<br />int pointerCount, int pointerId) {<br />// 測試gravity參數方法:作用為設定重力方向。<br />gravity.set(8 - (float) Math.random() * 16,<br />8 - (float) Math.random() * 16);<br />world.setGravity(gravity);<br />Log.i("", "xx=" + world.getGravity().x + " yy=" + world.getGravity().y);<br />}<br />}<br />