开发者

Adapting AS2 code to Flex 4.5

I have a flash source of the pendulum. I want to use it in Flex. I replace movie clips (ball0_mc and ball2_mc) on the image (hand.png and pend.png) in the source code. But I get in the final application a different kind of physics, the movement slowed down, the rope breaks away from the images.

Can anyone advise what I did wrong in this particular example?

AS2 source:

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

Result in 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>


The answer to this problem is that you need to understand 1) what is the end goal output of each AS2 function and 2) how often is that function being invoked.

For example, I'm not sure why you don't have have the problem AS2 equivalent of onEnterFrame in your Flex version. Or that a Flex application runs at 24fps and your AS2 project .fla will probably run at a different frame rate.

In your flex version, you are adding this dragging business when in the AS2 version there is none.

There is so many things you are doing wrong here that it all comes down to you needing to understand each function's purpose.

If it's any consolation, having experience in both AS2 and Flex, a port is possible...but with the exception it can't be done at higher than Flex's default frame rate of 24fps. You can try to amp up the frames higher but you run the risk the Flex Framework might behave out of it's originally designed specs and parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜