2011-08-31 68 views
0

我有一個擺錘的閃光源。我想在Flex中使用它。我將源代碼中的圖像(hand.png和pend.png)上的影片剪輯(ball0_mc和ball2_mc)替換掉。但我在最後的應用程序中獲得了另一種物理學,運動速度減慢,繩子從圖像中脫離出來。將AS2代碼調整爲Flex 4.5

任何人都可以建議我在這個特定的例子中做錯了什麼?

AS2來源:

function lines(Void) 
{ 
    _root.createEmptyMovieClip("lines", _root.getNextHighestDepth()); 
    with (lines) 
    { 
     clear(); 
     lineStyle(1, 0); 
     _root.moveTo(ball0_mc._x, ball0_mc._y); 
     _root.lineTo(ball2_mc._x, ball2_mc._y); 
    } 
    return undefined; 
} 
function verlet(ball) 
{ 
    var __reg3 = ball.mc._x; 
    var __reg2 = ball.mc._y; 
    ball.mc._x = ball.mc._x + (ball.mc._x - ball.oldx); 
    ball.mc._y = ball.mc._y + (ball.mc._y - ball.oldy); 
    ball.mc._y = ball.mc._y + grav * dt * dt; 
    ball.oldx = __reg3 
    ball.oldy = __reg2 
} 
function constraints(ball0, ball1) 
{ 
    var __reg7 = new flash.geom.Point(ball0.mc._x, ball0.mc._y); 
    var __reg6 = new flash.geom.Point(ball1.mc._x, ball1.mc._y); 
    var __reg1 = __reg6.subtract(__reg7); 
    var __reg5 =Math.sqrt(__reg1.x * __reg1.x) + Math.sqrt(__reg1.y * __reg1.y); 
    var __reg4 = (__reg5 - restlength)/__reg5 * (ball0.mass * 1 + ball1.mass * 1); 
    ball0.mc._x = ball0.mc._x + ball0.mass * 1 * __reg1.x * __reg4; 
    ball0.mc._y = ball0.mc._y + ball0.mass * 1 * __reg1.y * __reg4; 
    ball1.mc._x = ball1.mc._x - ball1.mass * 1 * __reg1.x * __reg4; 
    ball1.mc._y = ball1.mc._y - ball1.mass * 1 * __reg1.y * __reg4; 
    return undefined; 
} 
var t = 0; 
var ct = 0; 
var dt = 0; 
var particles = new Array(); 
var ball = new Object(); 
ball.oldx = ball0_mc._x; 
ball.oldy = ball0_mc._y; 
ball.mc = ball0_mc; 
ball.mass = 0.2; 
particles.push(ball); 

var ball = new Object(); 
ball.oldx = ball2_mc._x; 
ball.oldy = ball2_mc._y; 
ball.mc = ball2_mc; 
ball.mass =0.2; 
particles.push(ball); 
var grav = 0.5; 
var restlength = 120; 
_root.onEnterFrame = function (Void) 
{ 
    ++t; 
    dt = t - ct; 
    ct = t; 
    var __reg1 = 1; 
    while (__reg1 < particles.length) 
    { 
     var __reg2 = particles[__reg1]; 
     verlet(__reg2); 
     ++__reg1; 
    } 
    constraints(particles[0], particles[1]); 
    particles[0].mc._x = _xmouse; 
    particles[0].mc._y = _ymouse; 
    lines(); 
    return undefined; 
} 
; 

結果在軟硬度:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    width="760" height="760" minWidth="760" minHeight="760" 
     creationComplete="application1_creationCompleteHandler(event)"> 

<fx:Script> 
<![CDATA[ 
    import mx.core.IVisualElement; 
    import mx.events.FlexEvent; 

    private var t = 0; 
    private var ct = 0; 
    private var dt = 0; 
    private var particles:Array = new Array(); 
    private var grav = 0.5; 
    private var restlength = 120; 
    private var mass = 0.2; 

    protected function application1_creationCompleteHandler(event:FlexEvent):void 
    { 
    rope.xFrom = hand.x+30; 
    rope.yFrom = hand.y+50; 
    rope.xTo = pend.x + 10; 
    rope.yTo = pend.y; 

    addEventListener(MouseEvent.MOUSE_MOVE, startDragging); 
    var ball = new Object(); 
    ball.oldx = hand.x; 
    ball.oldy = hand.y; 
    ball.mc = hand; 
    ball.mass = 0.2; 
    particles.push(ball); 

    var ball = new Object(); 
    ball.oldx = pend.x; 
    ball.oldy = pend.y; 
    ball.mc = pend; 
    ball.mass =0.2; 
    particles.push(ball); 

    //constraints(particles[1], particles[2]); 
    } 

    protected function verlet(ball):void 
    { 
    var __reg3 = ball.mc.x; 
    var __reg2 = ball.mc.y; 
    ball.mc.x = ball.mc.x + (ball.mc.x - ball.oldx); 
    ball.mc.y = ball.mc.y + (ball.mc.y - ball.oldy); 
    ball.mc.y = ball.mc.y + grav * dt * dt; 
    ball.oldx = __reg3 
    ball.oldy = __reg2 
    } 
    protected function constraints(ball0, ball1):void 
    { 
    var __reg7 = new flash.geom.Point(ball0.mc.x, ball0.mc.y); 
    var __reg6 = new flash.geom.Point(ball1.mc.x, ball1.mc.y); 
    var __reg1 = __reg6.subtract(__reg7); 
    var __reg5 =Math.sqrt(__reg1.x * __reg1.x) + Math.sqrt(__reg1.y * __reg1.y); 
    var __reg4 = (__reg5 - restlength)/__reg5 * (ball0.mass * 1 + ball1.mass * 1); 
    ball0.mc.x = ball0.mc.x + ball0.mass * 1 * __reg1.x * __reg4; 
    ball0.mc.y = ball0.mc.y + ball0.mass * 1 * __reg1.y * __reg4; 
    ball1.mc.x = ball1.mc.x - ball1.mass * 1 * __reg1.x * __reg4; 
    ball1.mc.y = ball1.mc.y - ball1.mass * 1 * __reg1.y * __reg4; 
    } 
    protected function startDragging(event:MouseEvent):void 
    { 
    hand.x = this.mouseX; 
    hand.y = this.mouseY; 
    rope.xFrom = hand.x+30; 
    rope.yFrom = hand.y+50; 
    rope.xTo = pend.x + 10; 
    rope.yTo = pend.y; 
    ++t; 
    dt = t - ct; 
    ct = t; 
    var __reg1 = 1; 
    while (__reg1 < particles.length) 
    { 
    var __reg2 = particles[__reg1]; 
    verlet(__reg2); 
    ++__reg1; 
    } 
    constraints(particles[0], particles[1]); 
    } 

]]> 
</fx:Script> 
<s:Line id="rope"> 
<s:stroke> 
    <s:SolidColorStroke color="black" weight="1.2"/> 
</s:stroke> 
</s:Line> 
<s:Image id="hand" x="365" y="76" smooth="true" source="@Embed('images/hand.png')"/> 
<s:Image id="pend" x="410" y="392" smooth="true" source="@Embed('images/pend.png')"/> 
</s:Application> 

回答

1

這個問題的答案的問題是,您需要了解1)什麼是每個AS2功能和2的最終目標輸出)該函數被多次調用。

例如,我不確定爲什麼你的Flex版本中沒有AS2相當於onEnterFrame的問題。或者Flex應用程序以24fps運行,並且您的AS2項目.fla可能會以不同的幀速率運行。

在您的flex版本中,您在AS2版本中添加此拖動業務時沒有。

這裏有很多事情你做錯了,這一切都歸結爲你需要了解每個功能的目的。

如果任何安慰,有AS2和Flex的經驗,一個端口是可能的......但是例外情況是它不能以高於Flex的默認幀速率24fps完成。您可以嘗試將框架放大得更高,但是您可能會冒Flex Framework可能超出最初設計的規格和參數的風險。