flex游戏引擎(pushbutton)-飞机发子弹
声明:本文为刘兴(http://deepfuture.iteye.com/)原创,如转载请注明来源http://deepfuture.iteye.com/blog/722156
gamemain.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 mymain { import flash.display.Sprite; import com.pblabs.engine.PBE; import tools.mygameapi; [SWF(width="800", height="600", frameRate="60")] public class gamemain extends Sprite { public function gamemain() { // Start up PBE PBE.startup(this); new MyRes(); // Set up a simple scene entity mygameapi.createScene(); // Create a simple avatar entity mygameapi.createPlane(); mygameapi.createBackground(); // createDd(); } } } DdControllerComponent.as package mymain { import com.pblabs.engine.PBE; import com.pblabs.engine.components.TickedComponent; import com.pblabs.engine.entity.PropertyReference; import flash.geom.Point; public class DdControllerComponent extends TickedComponent { public var positionReference:PropertyReference; public var dname:String; public function DdControllerComponent(name:String){ dname=name; } // 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. position.y-=5; // Finally, add some boundary limits so that we don't go off the edge of the screen. if (position.y > 260 || position.y < -260) { // Set our position at the wall edge position.y=-400; } // Send our manipulated spatial variables back to the spatial manager owner.setProperty(positionReference, position); if (position.y==-400){ owner.removeComponent(owner.lookupComponentByName(dname+"Render")); owner.removeComponent(owner.lookupComponentByName(dname+"Controller")); } } } } PlaneControllerComponent.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 mymain { import com.pblabs.engine.PBE; import com.pblabs.engine.components.TickedComponent; import com.pblabs.engine.core.InputKey; import com.pblabs.engine.entity.PropertyReference; import com.pblabs.rendering2D.*; import flash.display.Sprite; import flash.geom.Point; import tools.mygameapi; // Make a ticked component so that it can update itself every frame with onTick() public class PlaneControllerComponent extends TickedComponent { // Keep a property reference to our entity's position. public var positionReference:PropertyReference; public var ddno:uint; public function PlaneControllerComponent() { ddno=0; } // 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; } if (PBE.isKeyDown(InputKey.F)) { // Move our hero to the left createDd(new Point(position.x,position.y-80)); } // Finally, add some boundary limits so that we don't go off the edge of the screen. 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); } private function createDd(firstpos:Point):void { // Allocate an entity for our hero avatar var ddname:String=ddno.toString(); mygameapi.createSpatial( owner, // with location of 0,0... firstpos, ddname+"Spatial", new Point(10,50) ); if (ddno<uint.MAX_VALUE){ ddno++; }else{ ddno=0; } // Create an instance of our hero controller component var ddcontroller:DdControllerComponent = new DdControllerComponent(ddname); // Point the controller component to this entity's Spatial component for position information ddcontroller.positionReference = new PropertyReference("@"+ddname+"Spatial.position"); // Add the demo controller component to the Hero entity with the name "Controller" owner.addComponent( ddcontroller, ddname+"Controller" ); // Create a simple render component to display our object var ddrender:SpriteRenderer = new SpriteRenderer(); // Tell the Render component to use one of the images embedded by our ResourceBundle ddrender.fileName = "../assets/dd.gif"; // Add the renderer to the scene. ddrender.scene = PBE.scene; // Set our hero to render above the background. ddrender.layerIndex = 3; // Point the render component to this entity's Spatial component for position information ddrender.positionProperty = new PropertyReference("@"+ddname+"Spatial.position"); // Point the render component to this entity's Spatial component for size information ddrender.sizeProperty = new PropertyReference("@"+ddname+"Spatial.size"); // Add our render component to the Hero entity with the name "Render" owner.addComponent( ddrender, ddname+"Render" ); // Register the entity with PBE under the name "Hero" } } } mygameapi.as package tools { 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; import mymain.PlaneControllerComponent; import mymain.DdControllerComponent; public class mygameapi { public function mygameapi() { } public static function createSpatial( ent:IEntity, pos:Point, name:String,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, name); } public static 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 } public static function createPlane():void { var plane:IEntity = allocateEntity(); // Allocate an entity for our hero avatar //飞机 createSpatial(plane, // with location of 0,0... new Point(0, 0), "PlSpatial", new Point(50,50) ); // Create an instance of our hero controller component var plcontroller:PlaneControllerComponent = new PlaneControllerComponent(); // Point the controller component to this entity's Spatial component for position information plcontroller.positionReference = new PropertyReference("@PlSpatial.position"); // Add the demo controller component to the Hero entity with the name "Controller" plane.addComponent( plcontroller, "PlController" ); // Create a simple render component to display our object var plrender:SpriteRenderer = new SpriteRenderer(); // Tell the Render component to use one of the images embedded by our ResourceBundle plrender.fileName = "../assets/fanship.png"; // Add the renderer to the scene. plrender.scene = PBE.scene; // Set our hero to render above the background. plrender.layerIndex = 2; // Point the render component to this entity's Spatial component for position information plrender.positionProperty = new PropertyReference("@PlSpatial.position"); // Point the render component to this entity's Spatial component for size information plrender.sizeProperty = new PropertyReference("@PlSpatial.size"); // Add our render component to the Hero entity with the name "Render" plane.addComponent( plrender, "PlRender" ); plane.initialize("plane"); } public static 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), "BgSpatial" ); // Create a simple render component to display our object // Just like the hero, this also uses a SpriteRenderComponent var bgrender:SpriteRenderer = new SpriteRenderer(); // Tell the Render component to use one of the images embedded by our ResourceLinker bgrender.fileName = "../assets/bg.jpg"; // Set our background to render below the hero. bgrender.layerIndex = 1; // Add the renderer to the scene. bgrender.scene = PBE.scene; // Point the render component to this entity's Spatial component for position information bgrender.positionProperty = new PropertyReference("@BgSpatial.position"); // Add our render component to the BG entity with the name "Render" bg.addComponent( bgrender, "BgRender" ); // Register the entity with PBE under the name "BG" bg.initialize("BG"); } } }
?
?
?
?
?
?
?
注意按F键发导弹
?