サイトトップ

Director Flash 書籍 業務内容 プロフィール

Nape in Starling Framework
Starlingフレームワークで物理演算エンジンNapeを使う
  Adobe Flash Player を取得

解説:「Starlingフレームワークで物理演算エンジンNapeを使う

Published for: Flash Player 11

// フレームアクション: メインタイムライン
import starling.core.Starling;
var myStarling:Starling = new Starling(MySprite, stage);
myStarling.start();

    // ActionScript 3.0クラス定義ファイル: MySprite.as
  1. package {
  2.   import flash.display.BitmapData;
  3.   import flash.utils.Timer;
  4.   import flash.events.TimerEvent;
  5.   import starling.display.Sprite;
  6.   import starling.display.Image;
  7.   import starling.textures.Texture;
  8.   import starling.events.Event;
  9.   import nape.phys.Body;
  10.   import nape.phys.BodyType;
  11.   import nape.phys.Material;
  12.   import nape.geom.Vec2;
  13.   import nape.shape.Circle;
  14.   import nape.shape.Polygon;
  15.   import nape.space.Space;
  16.   public class MySprite extends Sprite {
  17.     private var nWidth:int;
  18.     private var nHeight:int;
  19.     private var nRadius:Number;
  20.     private var myBitmapData:BitmapData;
  21.     private var myTexture:Texture;
  22.     private var bodies:Vector.<Body> = new Vector.<Body>();
  23.     private var bodyStatic:Body;
  24.     private var mySpace:Space;
  25.     private var myTimer:Timer = new Timer(200, 100);
  26.     public function MySprite() {
  27.       addEventListener(Event.ADDED_TO_STAGE, initialize);
  28.     }
  29.     private function initialize(eventObject:Event):void {
  30.       nWidth = stage.stageWidth;
  31.       nHeight = stage.stageHeight;
  32.       myBitmapData = new Pen();
  33.       myTexture = Texture.fromBitmapData(myBitmapData);
  34.       nRadius = myBitmapData.width / 2;
  35.       mySpace = new Space(new Vec2(0, 2000));
  36.       setFloor(mySpace);
  37.       addEventListener(Event.ENTER_FRAME, refreshScreen);
  38.       myTimer.addEventListener(TimerEvent.TIMER, addBall);
  39.       myTimer.start();
  40.     }
  41.     function addBall(eventObject:TimerEvent):void {
  42.       setBall(mySpace);
  43.     }
  44.     private function refreshScreen(eventObject:Event):void {
  45.       mySpace.step(1/24);
  46.     }
  47.     private function setBall(mySpace:Space):void {
  48.       var myImage:Image = createBall();
  49.       addChild(myImage);
  50.       addPhysics(myImage, mySpace, nWidth * Math.random());
  51.     }
  52.     private function createBall():Image {
  53.       var myImage:Image = new Image(myTexture);
  54.       myImage.pivotX = nRadius;
  55.       myImage.pivotY = nRadius;
  56.       return myImage;
  57.     }
  58.     private function addPhysics(myImage:Image, mySpace:Space, nX:Number = 0, nY:Number = 0) {
  59.       var myBody:Body = new Body(BodyType.DYNAMIC, new Vec2(nX, nY));
  60.       myBody.shapes.add(new Circle(nRadius, null, new Material(20)));
  61.       myBody.graphic = myImage;
  62.       myBody.space = mySpace;
  63.       myBody.graphicUpdate = updateGraphic;
  64.       bodies.push(myBody);
  65.     }
  66.     private function updateGraphic(myBody:Body):void {
  67.       var myImage:Image = myBody.graphic as Image;
  68.       var myPosition:Vec2 = myBody.position;
  69.       myImage.x = myPosition.x;
  70.       myImage.y = myPosition.y;
  71.       myImage.rotation = myBody.rotation;
  72.     }
  73.     private function setFloor(mySpace:Space):void {
  74.       var myPolygon:Polygon = new Polygon(Polygon.rect(0, nHeight, nWidth, 100));
  75.       bodyStatic = new Body(BodyType.STATIC);
  76.       bodyStatic.shapes.add(myPolygon);
  77.       bodyStatic.space = mySpace;
  78.     }    
  79.   }
  80. }

Copyright © 2001-2012 Fumio Nonaka.  All rights reserved.