|
Published for: Flash Player 11
// フレームアクション: メインタイムライン
import starling.core.Starling;
var myStarling:Starling = new Starling(MySprite, stage);
myStarling.start();
|
// ActionScript 3.0クラス定義ファイル: MySprite.as
- package {
- import flash.display.BitmapData;
- import flash.utils.Timer;
- import flash.events.TimerEvent;
- import starling.display.Sprite;
- import starling.display.Image;
- import starling.textures.Texture;
- import starling.events.Event;
- import nape.phys.Body;
- import nape.phys.BodyType;
- import nape.phys.Material;
- import nape.geom.Vec2;
- import nape.shape.Circle;
- import nape.shape.Polygon;
- import nape.space.Space;
- public class MySprite extends Sprite {
- private var nWidth:int;
- private var nHeight:int;
- private var nRadius:Number;
- private var myBitmapData:BitmapData;
- private var myTexture:Texture;
- private var bodies:Vector.<Body> = new Vector.<Body>();
- private var bodyStatic:Body;
- private var mySpace:Space;
- private var myTimer:Timer = new Timer(200, 100);
- public function MySprite() {
- addEventListener(Event.ADDED_TO_STAGE, initialize);
- }
- private function initialize(eventObject:Event):void {
- nWidth = stage.stageWidth;
- nHeight = stage.stageHeight;
- myBitmapData = new Pen();
- myTexture = Texture.fromBitmapData(myBitmapData);
- nRadius = myBitmapData.width / 2;
- mySpace = new Space(new Vec2(0, 2000));
- setFloor(mySpace);
- addEventListener(Event.ENTER_FRAME, refreshScreen);
- myTimer.addEventListener(TimerEvent.TIMER, addBall);
- myTimer.start();
- }
- function addBall(eventObject:TimerEvent):void {
- setBall(mySpace);
- }
- private function refreshScreen(eventObject:Event):void {
- mySpace.step(1/24);
- }
- private function setBall(mySpace:Space):void {
- var myImage:Image = createBall();
- addChild(myImage);
- addPhysics(myImage, mySpace, nWidth * Math.random());
- }
- private function createBall():Image {
- var myImage:Image = new Image(myTexture);
- myImage.pivotX = nRadius;
- myImage.pivotY = nRadius;
- return myImage;
- }
- private function addPhysics(myImage:Image, mySpace:Space, nX:Number = 0, nY:Number = 0) {
- var myBody:Body = new Body(BodyType.DYNAMIC, new Vec2(nX, nY));
- myBody.shapes.add(new Circle(nRadius, null, new Material(20)));
- myBody.graphic = myImage;
- myBody.space = mySpace;
- myBody.graphicUpdate = updateGraphic;
- bodies.push(myBody);
- }
- private function updateGraphic(myBody:Body):void {
- var myImage:Image = myBody.graphic as Image;
- var myPosition:Vec2 = myBody.position;
- myImage.x = myPosition.x;
- myImage.y = myPosition.y;
- myImage.rotation = myBody.rotation;
- }
- private function setFloor(mySpace:Space):void {
- var myPolygon:Polygon = new Polygon(Polygon.rect(0, nHeight, nWidth, 100));
- bodyStatic = new Body(BodyType.STATIC);
- bodyStatic.shapes.add(myPolygon);
- bodyStatic.space = mySpace;
- }
- }
- }
|
|