以下是一个完整的以xml组装游戏的实例:整个项目文件结构如下:
1、assets/Level/common.xml 文件为pushButton引擎当中的组件组装文件,以下为该文件代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<!DOCTYPE things PUBLIC "HJKHKJ" "PBEDTD.dtd">
<things>
<entity name="SpatialDB">
<component name="Manager" type="com.pblabs.box2D.Box2DManagerComponent">
</component>
</entity>
<entity name="Scene">
<component name="Scene" type="com.pblabs.rendering2D.DisplayObjectScene">
<trackObject componentReference="FeiJi"/>
<sceneViewName>MainApp</sceneViewName>
</component>
</entity>
<entity name="BG">
<component name="Spatial" type="com.pblabs.rendering2D.SimpleSpatialComponent">
<spatialManager componentReference="SpatialDB"/>
<position>
<x>0</x>
<y>0</y>
</position>
<size type="">
<x>896</x>
<y>640</y>
</size>
</component>
<component name="Render" type="com.pblabs.rendering2D.SpriteRenderer">
<fileName>
assets/image/bgs.jpg
</fileName>
<positionProperty>@Spatial.position</positionProperty>
<sizeProperty>@Spatial.size</sizeProperty>
<scene componentReference="Scene"/>
</component>
</entity>
<entity name="FeiJi">
<component name="Spatial" type="com.pblabs.rendering2D.SimpleSpatialComponent">
<position>
<x>0</x>
<y>0</y>
</position>
<spatialManager componentReference="SpatialDB"/>
</component>
<component name="Render" type="com.pblabs.rendering2D.SpriteRenderer">
<fileName>assets/image/fanship.png</fileName>
<positionProperty>@Spatial.position</positionProperty>
<sizeProperty>@Spatial.size</sizeProperty>
<scene componentReference="Scene"/>
</component>
<component name="Controller" type="controller.FeijiController">
<positionReference>@Spatial.position</positionReference>
</component>
</entity>
<entity name="HuaHua">
<component name="Spatial" type="com.pblabs.rendering2D.SimpleSpatialComponent">
<position>
<x>-400</x>
<y>0</y>
</position>
<size type="">
<x>60</x>
<y>60</y>
</size>
<spatialManager componentReference="SpatialDB"/>
</component>
<component name="Render" type="com.pblabs.rendering2D.SpriteRenderer">
<fileName>assets/image/HH_baihe.png</fileName>
<positionProperty>@Spatial.position</positionProperty>
<sizeProperty>@Spatial.size</sizeProperty>
<scene componentReference="Scene"/>
</component>
<component name="Controller" type="controller.HuaHuaController">
<input childType="com.pblabs.engine.core.InputMap">
<GoUp>UP</GoUp>
<GoDown>DOWN</GoDown>
</input>
</component>
</entity>
<group name="Common">
<objectReference name="SpatialDB"/>
<objectReference name="Scene"/>
<objectReference name="BG"/>
<objectReference name="FeiJi"/>
<objectReference name="HuaHua"/>
</group>
</things>
<things>
<entity name="SpatialDB">
<component name="Manager" type="com.pblabs.box2D.Box2DManagerComponent">
</component>
</entity>
<entity name="Scene">
<component name="Scene" type="com.pblabs.rendering2D.DisplayObjectScene">
<trackObject componentReference="FeiJi"/>
<sceneViewName>MainApp</sceneViewName>
</component>
</entity>
<entity name="BG">
<component name="Spatial" type="com.pblabs.rendering2D.SimpleSpatialComponent">
<spatialManager componentReference="SpatialDB"/>
<position>
<x>0</x>
<y>0</y>
</position>
<size type="">
<x>896</x>
<y>640</y>
</size>
</component>
<component name="Render" type="com.pblabs.rendering2D.SpriteRenderer">
<fileName>
assets/image/bgs.jpg
</fileName>
<positionProperty>@Spatial.position</positionProperty>
<sizeProperty>@Spatial.size</sizeProperty>
<scene componentReference="Scene"/>
</component>
</entity>
<entity name="FeiJi">
<component name="Spatial" type="com.pblabs.rendering2D.SimpleSpatialComponent">
<position>
<x>0</x>
<y>0</y>
</position>
<spatialManager componentReference="SpatialDB"/>
</component>
<component name="Render" type="com.pblabs.rendering2D.SpriteRenderer">
<fileName>assets/image/fanship.png</fileName>
<positionProperty>@Spatial.position</positionProperty>
<sizeProperty>@Spatial.size</sizeProperty>
<scene componentReference="Scene"/>
</component>
<component name="Controller" type="controller.FeijiController">
<positionReference>@Spatial.position</positionReference>
</component>
</entity>
<entity name="HuaHua">
<component name="Spatial" type="com.pblabs.rendering2D.SimpleSpatialComponent">
<position>
<x>-400</x>
<y>0</y>
</position>
<size type="">
<x>60</x>
<y>60</y>
</size>
<spatialManager componentReference="SpatialDB"/>
</component>
<component name="Render" type="com.pblabs.rendering2D.SpriteRenderer">
<fileName>assets/image/HH_baihe.png</fileName>
<positionProperty>@Spatial.position</positionProperty>
<sizeProperty>@Spatial.size</sizeProperty>
<scene componentReference="Scene"/>
</component>
<component name="Controller" type="controller.HuaHuaController">
<input childType="com.pblabs.engine.core.InputMap">
<GoUp>UP</GoUp>
<GoDown>DOWN</GoDown>
</input>
</component>
</entity>
<group name="Common">
<objectReference name="SpatialDB"/>
<objectReference name="Scene"/>
<objectReference name="BG"/>
<objectReference name="FeiJi"/>
<objectReference name="HuaHua"/>
</group>
</things>
2、MainApp 游戏运行的主类,也就是文档类
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package {
import com.pblabs.box2D.Box2DManagerComponent;
import com.pblabs.engine.PBE;
import com.pblabs.rendering2D.DisplayObjectScene;
import com.pblabs.rendering2D.SimpleSpatialComponent;
import com.pblabs.rendering2D.SpriteRenderer;
import com.pblabs.rendering2D.ui.SceneView;
import controller.FeijiController;
import controller.HuaHuaController;
import flash.display.Sprite;
public class MainApp extends Sprite
{
public function MainApp()
{
trace("hello");
PBE.startup(this);
createScene();
loadLevel();
}
private function createScene():void
{
var scene:SceneView=new SceneView();
scene.width=1024;
scene.height=600;
PBE.initializeScene(scene);
scene.name="MainApp";
}
private function loadLevel():void
{
PBE.levelManager.addFileReference(0,"assets/Level/common.xml");
PBE.levelManager.addGroupReference(0,"Common");
PBE.levelManager.loadLevel(0);
PBE.levelManager.start();
}
private function register():void
{
PBE.registerType(com.pblabs.rendering2D.DisplayObjectScene);
//PBE.registerType();
PBE.registerType(Box2DManagerComponent);
PBE.registerType(SimpleSpatialComponent);
PBE.registerType(SpriteRenderer);
PBE.registerType(FeijiController)
PBE.registerType(HuaHuaController);
}
}
}
import com.pblabs.box2D.Box2DManagerComponent;
import com.pblabs.engine.PBE;
import com.pblabs.rendering2D.DisplayObjectScene;
import com.pblabs.rendering2D.SimpleSpatialComponent;
import com.pblabs.rendering2D.SpriteRenderer;
import com.pblabs.rendering2D.ui.SceneView;
import controller.FeijiController;
import controller.HuaHuaController;
import flash.display.Sprite;
public class MainApp extends Sprite
{
public function MainApp()
{
trace("hello");
PBE.startup(this);
createScene();
loadLevel();
}
private function createScene():void
{
var scene:SceneView=new SceneView();
scene.width=1024;
scene.height=600;
PBE.initializeScene(scene);
scene.name="MainApp";
}
private function loadLevel():void
{
PBE.levelManager.addFileReference(0,"assets/Level/common.xml");
PBE.levelManager.addGroupReference(0,"Common");
PBE.levelManager.loadLevel(0);
PBE.levelManager.start();
}
private function register():void
{
PBE.registerType(com.pblabs.rendering2D.DisplayObjectScene);
//PBE.registerType();
PBE.registerType(Box2DManagerComponent);
PBE.registerType(SimpleSpatialComponent);
PBE.registerType(SpriteRenderer);
PBE.registerType(FeijiController)
PBE.registerType(HuaHuaController);
}
}
}
3、FeijiController是消费机的控制器组件
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package controller
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.components.TickedComponent;
import com.pblabs.engine.core.InputKey;
import com.pblabs.engine.entity.PropertyReference;
import flash.geom.Point;
public class FeijiController extends TickedComponent
{
public var positionReference:PropertyReference;
public function FeijiController()
{
super();
}
public override function onTick(deltaTime:Number):void
{
var position:Point=owner.getProperty(positionReference);
if(PBE.isKeyDown(InputKey.RIGHT))
{
position.x+=15;
}
if(PBE.isKeyDown(InputKey.LEFT))
{
position.x-=15;
}
if(PBE.isKeyDown(InputKey.UP))
{
position.y-=15;
}
if(PBE.isKeyDown(InputKey.DOWN))
{
position.y+=15;
}
if(position.x>375)
{
position.x=375;
}else if(position.x<-375)
{
position.x=-375;
}
owner.setProperty(positionReference,position);
}
}
}
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.components.TickedComponent;
import com.pblabs.engine.core.InputKey;
import com.pblabs.engine.entity.PropertyReference;
import flash.geom.Point;
public class FeijiController extends TickedComponent
{
public var positionReference:PropertyReference;
public function FeijiController()
{
super();
}
public override function onTick(deltaTime:Number):void
{
var position:Point=owner.getProperty(positionReference);
if(PBE.isKeyDown(InputKey.RIGHT))
{
position.x+=15;
}
if(PBE.isKeyDown(InputKey.LEFT))
{
position.x-=15;
}
if(PBE.isKeyDown(InputKey.UP))
{
position.y-=15;
}
if(PBE.isKeyDown(InputKey.DOWN))
{
position.y+=15;
}
if(position.x>375)
{
position.x=375;
}else if(position.x<-375)
{
position.x=-375;
}
owner.setProperty(positionReference,position);
}
}
}
4、HuaHuaController是小花的控制器组件
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package controller
{
import com.pblabs.engine.components.TickedComponent;
import com.pblabs.engine.core.InputMap;
import com.pblabs.engine.entity.PropertyReference;
import flash.geom.Point;
public class HuaHuaController extends TickedComponent
{
private var _input:InputMap;
public function get input():InputMap
{
return _input;
}
public function set input(value:InputMap):void
{
_input=value;
if(_input!=null)
{
_input.mapActionToHandler("GoUp",onGoUpHandler);
_input.mapActionToHandler("GoDown",onGoDownHandler);
}
}
private function onGoUpHandler(value:Number):void
{
trace("向上");
trace(value);
}
private function onGoDownHandler(value:Number):void
{
trace("乡下");
trace(value);
}
public function HuaHuaController()
{
super();
}
public override function onTick(deltaTime:Number):void
{
}
}
}
{
import com.pblabs.engine.components.TickedComponent;
import com.pblabs.engine.core.InputMap;
import com.pblabs.engine.entity.PropertyReference;
import flash.geom.Point;
public class HuaHuaController extends TickedComponent
{
private var _input:InputMap;
public function get input():InputMap
{
return _input;
}
public function set input(value:InputMap):void
{
_input=value;
if(_input!=null)
{
_input.mapActionToHandler("GoUp",onGoUpHandler);
_input.mapActionToHandler("GoDown",onGoDownHandler);
}
}
private function onGoUpHandler(value:Number):void
{
trace("向上");
trace(value);
}
private function onGoDownHandler(value:Number):void
{
trace("乡下");
trace(value);
}
public function HuaHuaController()
{
super();
}
public override function onTick(deltaTime:Number):void
{
}
}
}