要試玩 starling framework 最簡單的方式就是去安裝
新版的 FlashDevelop(目前是 4.0.2RTM),
會自動把 Flex SDK 4.6 / AIR SDK 3.2 / flash debug player 11.2 通通裝好,
接著安裝 starling
download starling 1.1
http://github.com/PrimaryFeather/Starling-Framework/zipball/v1.1
安裝 Stats 方便觀察 FPS
download Mrdoom's Stats for starling
http://www.kouma.fr/files/stats.zip
安裝 nape
download nape 9.1(as3 debug build swc)
http://deltaluca.me.uk/docnew/
start starling(code 都有註解應該很容易瞭解)
in Main class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import starling.core.Starling; | |
[SWF( width="800", height="600", frameRate="60" , backgroundColor="0xCCCCCC")] | |
public class Main extends Sprite | |
{ | |
private var starling:Starling; | |
public function Main():void | |
{ | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.align = StageAlign.TOP_LEFT; | |
// pass Game class | |
starling = new Starling(Game, stage); | |
starling.start(); | |
starling.stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreated); | |
} | |
private function onContextCreated(e:Event):void | |
{ | |
// set framerate to 30 in software mode | |
trace("info:",Starling.context.driverInfo); | |
if (Starling.context.driverInfo.toLowerCase().indexOf("software") != -1) | |
Starling.current.nativeStage.frameRate = 30; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import fr.kouma.starling.utils.Stats; | |
import nape.geom.Vec2; | |
import nape.phys.Body; | |
import nape.phys.BodyType; | |
import nape.phys.Material; | |
import nape.shape.Circle; | |
import nape.shape.Polygon; | |
import nape.space.Space; | |
import nape.util.BitmapDebug; | |
import nape.util.ShapeDebug; | |
import starling.core.Starling; | |
import starling.display.DisplayObject; | |
import starling.display.Image; | |
import starling.display.Sprite; | |
import starling.events.Event; | |
public class Game extends Sprite | |
{ | |
[Embed(source = "assets/back.png")] | |
private var bg:Class; | |
private var space:Space; | |
private const DEBUG_ENABLE:Boolean = false; | |
//private var debug:BitmapDebug; | |
private var debug:ShapeDebug; | |
public function Game() | |
{ | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
// background image | |
addChild(Image.fromBitmap(new bg())); | |
// 設定場景 重力(影響掉落的速度) | |
space = new Space(new Vec2(0, 3000)); | |
// 設置地板 | |
var floor:Body = new Body(BodyType.STATIC); | |
floor.shapes.add(new Polygon(Polygon.rect(0, 600, 800, 20))); | |
floor.space = space; | |
// add starling stats | |
addChild(new Stats()); | |
// debug | |
if (DEBUG_ENABLE) { | |
debug = new ShapeDebug(800, 600, 0x333333); | |
// faster render(but....need fucking adobe license) | |
//debug = new BitmapDebug(800, 600, 0x333333); | |
debug.draw(space); | |
// flash sprite | |
var _container_debug:flash.display.Sprite = new flash.display.Sprite(); | |
// debug.display is a flash DisplayObject, it's not a starling Sprite. | |
_container_debug.addChild(debug.display); | |
Starling.current.nativeOverlay.addChild(_container_debug); | |
} | |
addEventListener(Event.ENTER_FRAME, loop); | |
} | |
private function loop(e:Event):void | |
{ | |
if (DEBUG_ENABLE) { | |
debug.clear(); | |
space.step(1 / 60); | |
debug.draw(space); | |
debug.flush() | |
} | |
else space.step(1 / 60); | |
if (Math.random() < 0.03) | |
addBall(); | |
} | |
private function addBall():void | |
{ | |
// 設定種類及座標 | |
var ball:Body = new Body(BodyType.DYNAMIC, new Vec2(Math.random() * 750, 100)); | |
// 設定球的規格及材質 | |
ball.shapes.add(new Circle(51.5, null, new Material(20))); | |
// 預設提供幾種材質wood/steel/ice/rubber/glass/sand | |
//ball.shapes.add(new Circle(51.5, null, Material.rubber())); | |
ball.space = space; | |
// 貼上球的圖檔 | |
if (!DEBUG_ENABLE) { | |
ball.graphic = new Basketball(); | |
ball.graphicUpdate = updateGraphic; | |
addChild(ball.graphic); | |
} | |
} | |
private function updateGraphic(b:Body):void | |
{ | |
// fixed memory leak | |
var gr:DisplayObject = b.graphic; | |
gr.x = b.position.x; | |
gr.y = b.position.y; | |
gr.rotation = b.rotation; | |
} | |
} | |
} |
http://blog.codestage.ru/2012/01/15/nape-and-starling/
沒有留言:
張貼留言