flex游戏引擎(PushButton)--组件运动-自动走的小球

Lesson3Base.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.core.*;
 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 Lesson3Base extends Sprite
 {
  public function Lesson3Base()
  {
   PBE.startup(this);                                                // Start up PBE
   
   createScene();                                                       // Set up a simple scene entity
   
   createHero();                                                        // Create a simple avatar entity
  }
  
  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
   
   var spatial:SimpleSpatialComponent = new SimpleSpatialComponent();   // Create our spatial component
   
   spatial.position = new Point(-375,-275);                             // Set our hero's spatial position as -375,-275 -- the upper left corner
   spatial.size = new Point(50,50);                                     // Set our hero's size as 50,50
   spatial.spatialManager = PBE.spatialManager;
   
   hero.addComponent( spatial, "Spatial" );                             // Add our spatial component to the Hero entity with the name "Spatial"
   
   var render:SimpleShapeRenderer = new SimpleShapeRenderer();          // Create a renderer to display our object
   render.fillColor = 0x0000FF0;
   render.isCircle = true;
   render.radius = 25;
   render.lineSize = 2;
   render.lineColor = 0x000000;
   render.scene = PBE.scene;                                            // Set which scene this is a part of
   
   // 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 rotation information
   render.rotationProperty = new PropertyReference("@Spatial.rotation");
   
   hero.addComponent( render, "Render" );                               // Add our render component to the Hero entity with the name "Render"
   var controller:DemoControllerComponent = new DemoControllerComponent();
   // 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" );
   
   hero.initialize("Hero");                                             // Register the entity with PBE under the name "Hero"        
  }
 }
}

DemoControllerComponent.as

package
{
 
 import com.pblabs.engine.components.TickedComponent;

 import com.pblabs.engine.entity.PropertyReference;
 
 import flash.geom.Point;

 // Make a ticked component so that it can update itself every frame with onTick() 

 public class DemoControllerComponent extends TickedComponent

 {
    
  // Keep a property reference to our entity's position.
    
  public var positionReference:PropertyReference;
   
  // Store the direction that our entity is traveling: 1 is to the right, -1 is to the left.
     
  private var direction:int = 1;
   
     
  // onTick() is called every frame
 
  public override function onTick(tickRate:Number):void
    
  {
     
   // Copy the owner entity's position into a local Point structure
    
   var position:Point = owner.getProperty(positionReference);
   
   // If we are over the left edge...
     
   if (position.x < -375) {
       
    // ...then push ourselves to the right for the time being.
         
     direction = 1;
         
    // Move our entity down a notch
           
     if (position.y<280)  position.y += 20;
     else position.y=-260;
       
   }
        
   // If we are over the right edge...
      
   else if (position.x > 375) {
          
    // ...then push ourselves to the left for the time being.
          
     direction = -1;
          
    // Move our entity down a notch
       
     
     if (position.y<280)  position.y += 20;
     else position.y=-260;
        
   }
      
   // Move 5 units in the direction that we're headed
       
    position.x += direction * 10;
    
   // Set the spatial component's position based on our new value
      
    owner.setProperty(positionReference, position);
 
  }
 
 }




 
 rar附件为fb的工程
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值