HeroControllerComponent .as
//组件控制,包括键盘,移动
/*******************************************************************************
* PushButton Engine
* Copyright (C) 2009 PushButton Labs, LLC
* For more information see http://www.pushbuttonengine.com
*
* This file is licensed under the terms of the MIT license, which is included
* in the License.html file at the root directory of this SDK.
******************************************************************************/
package
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.components.TickedComponent;
import com.pblabs.engine.entity.PropertyReference;
import com.pblabs.engine.core.InputKey;
import flash.geom.Point;
// Make a ticked component so that it can update itself every frame with onTick()
public class HeroControllerComponent extends TickedComponent
{
// Keep a property reference to our entity's position.
public var positionReference:PropertyReference;
// onTick() is called every frame
public override function onTick(tickRate:Number):void
{
var position:Point = owner.getProperty(positionReference);
// Look at our input keys to see which direction we should move. Left is -x, right is +x.
if (PBE.isKeyDown(InputKey.RIGHT))
{
// Move our hero to the right
position.x += 15;
}
if (PBE.isKeyDown(InputKey.LEFT))
{
// Move our hero to the left
position.x -= 15;
}
if (PBE.isKeyDown(InputKey.UP))
{
// Move our hero to the right
position.y -= 15;
}
if (PBE.isKeyDown(InputKey.DOWN))
{
// Move our hero to the left
position.y += 15;
}
// Finally, add some boundary limits so that we don't go off the edge of the screen.
30.
if (position.x > 375)
{
// Set our position at the wall edge
position.x = 375;
}
else if (position.x < -375)
{
// Set our position at the wall edge
position.x = -375;
}
if (position.y > 260)
{
// Set our position at the wall edge
position.y = 260;
}
else if (position.y < -260)
{
// Set our position at the wall edge
position.y = -260;
}
// Send our manipulated spatial variables back to the spatial manager
owner.setProperty(positionReference, position);
}
}
}
Lesson5Base.as
/*******************************************************************************
* PushButton Engine
* Copyright (C) 2009 PushButton Labs, LLC
* For more information see http://www.pushbuttonengine.com
*
* This file is licensed under the terms of the MIT license, which is included
* in the License.html file at the root directory of this SDK.
******************************************************************************/
package
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.entity.*;
import com.pblabs.rendering2D.*;
import com.pblabs.rendering2D.ui.*;
import flash.display.Sprite;
import flash.geom.Point;
[SWF(width="800", height="600", frameRate="60")]
public class Lesson5Base extends Sprite
{
public function Lesson5Base()
{
// Start up PBE
PBE.startup(this);
new MyRes();
// Set up a simple scene entity
createScene();
// Create a simple avatar entity
createHero();
createBackground();
}
private function createScene():void
{
var sceneView:SceneView = new SceneView(); // Make the SceneView
sceneView.width = 800;
sceneView.height = 600;
PBE.initializeScene(sceneView); // This is just a helper function that will set up a basic scene for us
}
private function createHero():void
{
var hero:IEntity = allocateEntity(); // Allocate an entity for our hero avatar
//创造位置及尺寸信息的组件
createSpatial( hero,
// with location of 0,0...
new Point(0, 0),
new Point(50,50)
);
// Create an instance of our hero controller component
var controller:HeroControllerComponent = new HeroControllerComponent();
// Point the controller component to this entity's Spatial component for position information
controller.positionReference = new PropertyReference("@Spatial.position");
// Add the demo controller component to the Hero entity with the name "Controller"
hero.addComponent( controller, "Controller" );
// Create a simple render component to display our object
var render:SpriteRenderer = new SpriteRenderer();
// Tell the Render component to use one of the images embedded by our ResourceBundle
render.fileName = "../assets/fanship.png";
// Add the renderer to the scene.
render.scene = PBE.scene;
// Set our hero to render above the background.
render.layerIndex = 10;
//从空间组件中提取坐标信息
// Point the render component to this entity's Spatial component for position information
render.positionProperty = new PropertyReference("@Spatial.position");
// Point the render component to this entity's Spatial component for size information
render.sizeProperty = new PropertyReference("@Spatial.size");
// Add our render component to the Hero entity with the name "Render"
hero.addComponent( render, "Render" );
hero.initialize("Hero"); // Register the entity with PBE under the name "Hero"
}
private function createBackground():void
{
// Allocate an entity for our background sprite
var bg:IEntity = PBE.allocateEntity();
// Add our spatial component to the background entity ...
createSpatial( bg,
// with location of 0,0...
new Point(0, 0)
);
// Create a simple render component to display our object
// Just like the hero, this also uses a SpriteRenderComponent
var render:SpriteRenderer = new SpriteRenderer();
// Tell the Render component to use one of the images embedded by our ResourceLinker
render.fileName = "../assets/bg.jpg";
// Set our background to render below the hero.
render.layerIndex = 1;
// Add the renderer to the scene.
render.scene = PBE.scene;
// Point the render component to this entity's Spatial component for position information
render.positionProperty = new PropertyReference("@Spatial.position");
// Add our render component to the BG entity with the name "Render"
bg.addComponent( render, "Render" );
// Register the entity with PBE under the name "BG"
bg.initialize("BG");
}
private function createSpatial( ent:IEntity, pos:Point, size:Point = null ):void
{
//创建位置和尺寸信息组件,并加入到实体
// Create our spatial component
var spatial:SimpleSpatialComponent = new SimpleSpatialComponent();
// Do a named lookup to register our background with the scene spatial database
spatial.spatialManager = PBE.spatialManager;
// Set our background position in space
spatial.position = pos;
if (size != null)
spatial.size = size;
ent.addComponent(spatial, "Spatial");
}
}
}
//引用的资源定义,能把资源包括图片放入发布后的swf文件
// ActionScript file
/*******************************************************************************
* PushButton Engine
* Copyright (C) 2009 PushButton Labs, LLC
* For more information see http://www.pushbuttonengine.com
*
* This file is licensed under the terms of the MIT license, which is included
* in the License.html file at the root directory of this SDK.
******************************************************************************/
//这个类非常重要,将完成图片资源打包进发布后的swf文件
package
{
import com.pblabs.engine.resource.*;
public class MyRes extends ResourceBundle
{
[Embed(source="../assets/bg.jpg")]
//resBg这个名称不重要,但必须保证唯一,必须要申明
public var resBg:Class;
[Embed(source="../assets/fanship.png")]
//resShip这个名称不重要,但必须保证唯一,必须要申明
public var resShip:Class;
}
}
演示:http://www.deepfuturesoft.info/flexstudy/Lesson5Base.swf