将AS2代码调整为Flex 4.5

时间:2011-08-31 12:35:31

标签: flex actionscript-2 flex4.5

我有一个钟摆的闪光源。我想在Flex中使用它。我在源代码中的图像(hand.png和pend.png)上替换了影片剪辑(ball0_mc和ball2_mc)。但是我在最后的应用程序中得到了一种不同的物理学,运动减慢了,绳索从图像中脱离出来。

有人可以告诉我在这个特定的例子中做错了吗?

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;
}
;

Flex中的结果:

<?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 个答案:

答案 0 :(得分:1)

这个问题的答案是你需要了解1)每个AS2函数的最终目标输出是什么,以及2)调用该函数的频率。

例如,我不确定为什么你的Flex版本中没有AS2相当于onEnterFrame的问题。或者Flex应用程序以24fps运行,而AS2项目.fla可能会以不同的帧速率运行。

在您的Flex版本中,您在AS2版本中添加此拖动业务时没有。

这里有很多你做错的事情,这一切都归结为你需要了解每个功能的目的。

如果有任何安慰,在AS2和Flex都有经验,可以使用端口......但除了Flex的默认帧速率24fps之外,它无法完成。您可以尝试将帧放大一些,但是您可能会遇到Flex Framework可能表现出最初设计的规格和参数的风险。