網站上的代碼示例是Haxe,而不是ActionScript,它解釋了添加'包'時得到的錯誤。
下面是類的修改版本的ActionScript:
package{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.StageQuality;
import nape.space.Space;
import nape.space.UniformSleepSpace;
import nape.util.Tools;
import nape.geom.Vec2;
import nape.geom.GeomPoly;
import nape.phys.PhysObj;
import nape.phys.Material;
import nape.geom.AABB;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class Main extends Sprite
{
private var space:Space;
private var g:Graphics;
private var vl:Array;
private var px:Number; private var py:Number;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private function mouseDown(ev:MouseEvent):void {
var x = stage.mouseX;
var y = stage.mouseY;
g.lineStyle(1, 0, 0.5);
vl.push(new Vec2(x, y));
g.moveTo(x, y);
px = x; py = y;
}
private function mouseUp(ev:MouseEvent):void {
if (vl.length >= 3) {
var poly = new GeomPoly(vl);
poly.simplify(50, 4);
if (!poly.selfIntersecting()) {
if (!poly.cw()) poly.points.reverse();
var p = Tools.createConcave(poly, 0, 0, 0, false, Material.Wood);
space.addObject(p);
addChild(p.graphic);
}
}
g.clear();
vl = new Array();
px = -100;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public function init(ev:Event = null)
{
var s = new Sprite();
g = s.graphics;
addChild(s);
s.cacheAsBitmap = true;
vl = new Array();
px = -100;
space = new UniformSleepSpace(new AABB(0,0, 800,600), 30, new Vec2(0, 250));
var p:PhysObj;
space.addObject(p = Tools.createBox(-20, 300, 50, 600, 0, 0, 0, true, Material.Wood));
addChild(p.graphic);
space.addObject(p = Tools.createBox(820, 300, 50, 600, 0, 0, 0, true, Material.Wood));
addChild(p.graphic);
space.addObject(p = Tools.createBox(400, -20, 800, 50, 0, 0, 0, true, Material.Wood));
addChild(p.graphic);
space.addObject(p = Tools.createBox(400, 620, 800, 50, 0, 0, 0, true, Material.Wood));
addChild(p.graphic);
stage.quality = StageQuality.MEDIUM;
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private function enterFrame(ev:Event) {
graphics.clear();
graphics.lineStyle(1, 0, 1);
if (px != -100) {
var x = stage.mouseX;
var y = stage.mouseY;
var dx = x - px; var dy = y - py;
if (dx * dx + dy * dy > 5) {
vl.push(new Vec2(x, y));
px = x; py = y;
g.lineTo(x, y);
}else {
graphics.moveTo(px, py);
graphics.lineTo(x, y);
}
}
space.step(1/60, 6, 6);
}
}
}
而這裏的時間軸代碼:
import flash.Boot;
new Boot();
var m:Main = new Main();
m.addEventListener(Event.ADDED_TO_STAGE, m.init);
addChild(m)
你FLA應該是800×600與60,對於這種設置一個幀率。 另外,請注意我已經將構造函數更改爲init方法。這是因爲 階段在那裏被訪問,並且直到Main的實例被添加到階段 階段的引用將爲空。
混合AS3和Haxe對於新編程的人來說並不容易。如果你對Haxe感興趣,那就去做吧,否則,爲了更簡單的2D物理學,我建議給予Box2D World Construction Kit一試。它允許您使用組件定義輕鬆地在IDE中直接創建世界,並且它應該比直接AS3端口快一點,因爲它使用Alchemy。
此外,對gotoAndLearn一個免費的入門視頻教程,讓你開始。
Goodluck!
因爲Nape是使用Haxe構建的,所以您可以試試haxe的郵件列表... – goliatone 2011-01-09 13:23:13