Nape是除Box2D之外的又一个2D物理模拟引擎,有Luca Deltodesco用Haxe语言编写的。Nape引擎还提供了AS3版的SWC库,供Flash开发使用。
Nape包下载:点我下载Nape
在这里,是做了一个篮球掉落效果 如下:
starling的创建,还是老套路,这里就不述说了。Nape的用法,有额外教程,可以渐渐学习。天地会Nape教程:Nape教程
下边直接贴出code:
文档类:StNape.as
package { import com.vini123.Game; import flash.display.Sprite; import starling.core.Starling; [SWF(width = "680" , height = "510" ,frameRate = '30')] public class StNape extends Sprite { private var _starling:Starling; public function StNape() { Starling.handleLostContext = true; _starling = new Starling(Game , stage); _starling.start(); } } }
Game.as
package com.vini123 { import com.vini123.view.Basketball; 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 starling.display.Image; import starling.display.Sprite; import starling.events.Event; import starling.textures.Texture; public class Game extends Sprite { [Embed(source="../../../asset/back.png")] public var Back:Class; private var _space:Space; public function Game() { addEventListener(Event.ADDED_TO_STAGE , addStageHandler); } private function addStageHandler(e:Event):void { addChild(new Image(Texture.fromBitmap(new Back))); var gravity:Vec2 = new Vec2(0,3000); _space = new Space(gravity); var body:Body = new Body(BodyType.STATIC); body.shapes.add(new Polygon(Polygon.rect(0,510,680,20))); body.space = _space; addEventListener(Event.ENTER_FRAME ,enterHandler) } private function enterHandler(e:Event):void { _space.step(1/60); _space.liveBodies.foreach(updateGraphics); 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))); ball.space = _space; ball.userData.graphic = new Basketball(); addChild(ball.userData.graphic); } private function updateGraphics(b:Body):void { b.userData.graphic.x = b.position.x; b.userData.graphic.y = b.position.y; b.userData.graphic.rotation = b.rotation; } } }