大家好,我是奶綠茶
在 Flash 裡要製作 Physics 效果, 大家該馬上連想到 Box2D
其實 Flash CS5 就有內建這樣的類別, 只是目前 Adobe 還沒公開
你可以在這個路徑下找到 Physics 類別
X:\Program Files\adobe\Adobe Flash CS5\Common\Configuration\ActionScript 3.0\libs
小弟我玩了一下,還滿好上手的, 不過沒有任何的文件, K 起來有點辛苦, 反正先完看看
參考網址:http://heftybyte.com/?p=36
線上Demo請點我
轉載請註明出處
在 Flash 裡要製作 Physics 效果, 大家該馬上連想到 Box2D
其實 Flash CS5 就有內建這樣的類別, 只是目前 Adobe 還沒公開
你可以在這個路徑下找到 Physics 類別
X:\Program Files\adobe\Adobe Flash CS5\Common\Configuration\ActionScript 3.0\libs
小弟我玩了一下,還滿好上手的, 不過沒有任何的文件, K 起來有點辛苦, 反正先完看看
參考網址:http://heftybyte.com/?p=36
線上Demo請點我
轉載請註明出處
/** * @author milkmidi * @see http://milkmidi.blogspot.com * @version 1.0.1 * @date created 2010/12/14/ */ package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.ColorTransform; import flash.geom.Point; import PffLib.*; import swc.Builder_mc; import swc.Circle_mc; public class CrashDemo extends Sprite { private var _world:World; public function CrashDemo() { var _physm:PhysicsManager = new PhysicsManager(this); _world = _physm.createWorld(); _world.enableCollisions(true); _world.enableFriction(true); _world.setGravity(new Point(0, 500)); //_world.setDamping(2); createPhysicsElements(); createConstraint(); createBuilder(); trace(PhysObj.kCollisionTypeRectangle) trace(PhysObj.kCollisionTypeCircle) addEventListener(Event.ENTER_FRAME , onTickHandler); } private function createConstraint():void { // top createBox(0, 0, stage.stageWidth , 20); // down createBox(0, stage.stageHeight - 20, stage.stageWidth , 20); //left createBox(0, 0, 20 , stage.stageHeight); //right createBox(stage.stageWidth - 20, 0, 20 , stage.stageHeight); } private function createBox(pX:int , pY:int ,pWidth:int , pHeight:int):void { var _box:Sprite = ElementsUtil.create(ElementsUtil.BOX, pX , pY , pWidth , pHeight ); addChild(_box); var _obj:PhysObj = _world.addPhysObj( _box, new Point(0, 0), PhysObj.kCollisionTypeRectangle, false ); _obj.setNonMoving(true); //_obj.setCoefficientOfFriction(1); } private function createPhysicsElements():void{ var _physObj:PhysObj; var _c:Sprite = new Circle_mc; _c.x = 100; _c.addEventListener(MouseEvent.CLICK , function (e:MouseEvent):void { //var _vx:int = _c.mouseX * -1; var _vx:Number = Math.random() * 500 - 200; var _vy:Number = Math.random() * 500 + 200; //var _vy:int = _c.mouseY ; //trace(_c.mouseY , _c.height, _vy); _physObj.setLinearVelocity(new Point(_vx * 2, -_vy*2)); }); _c.buttonMode = true; addChild(_c); _physObj = _world.addPhysObj( _c , new Point( 1, 1 ) , PhysObj.kCollisionTypeCircle, false ); _physObj.setAngularVelocity(Math.PI / 2); //_physObj.setCoefficientOfFriction(1); //_c.physObj = _physObj; var _mc1:Sprite = ElementsUtil.create(ElementsUtil.BOX, 10, 10, 50, 50); addChild(_mc1); var _obj1:PhysObj = _world.addPhysObj( _mc1, new Point(0, 0), PhysObj.kCollisionTypeRectangle, false ); _world.addJoint( 0 ,_physObj, new Point(0, 30) , _obj1, new Point(50, 50)); } private function createBuilder():void{ var _c:Builder_mc = new Builder_mc(); addChild( _c ); for (var i:int = 0; i < _c.numChildren; i++) { var _mc:DisplayObject = _c.getChildAt(i); _world.addPhysObj( _mc, new Point(0, 0), PhysObj.kCollisionTypeRectangle, false ); } } private function onTickHandler(e:Event):void { _world.advanceFrames(); _world.updateAllObjects(); } }//__________________________________________________________________________________ End Class }//__________________________________________________________________________________ End Package import flash.display.Sprite; class ElementsUtil { public static const BOX :String = "box"; public static const CIRCLE :String = "circle"; public static function create(pType:String , pX:int , pY:int , pWidth:int,pHeight:int):Sprite { var sprite:Sprite = new Sprite(); sprite.graphics.beginFill(0x0); switch (pType) { case BOX: sprite.graphics.drawRect(0, 0, pWidth , pHeight); break; case CIRCLE: sprite.graphics.drawCircle(0, 0, pWidth); break; } sprite.graphics.endFill(); sprite.x = pX; sprite.y = pY; return sprite; } }
留言
據我自己實作後的感想 , 效能和模擬能力還是蠻遜的 , 但這樣也才有大進步的空間不是嗎 , aodbe加油!! :D