[Flex] Actionscript Project Migration

  • Actionscript Project Migration
  • Download: MotionUIComponent.zip

    平常在Flash底下辛苦寫的actionscript project如果要移植到Flex Project上可以透過UIComponent來承接Sprite,只是有一個限制就是如果你有使用 fl.* package在Flex裡是不支援的。這裡我直接以[Flash] Motion為範例,在這一篇裡我們寫了不小的程式碼來達到多種Motion的特效,如果要拿到Flex Project上當然是不希望要重新撰寫,以下就是一個移植的範例程式碼:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="initApp()" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#7E9ED7, #CFE6EF]" width="400" height="300" viewSourceURL="srcview/index.html">
    <mx:Script>
    <![CDATA[
    import flash.display.Sprite;

    import xinyu.geom.Ball;
    import xinyu.science.Science;
    import xinyu.motions.SpaceMotion2D;
    import xinyu.motions.CircleMotion2D;

    private var ball:Ball;

    private var ballMask:Sprite;

    private function initApp(): void {
    ball = new Ball(0xCCCCCC, 0x000055, 10, -45);
    rootStage.addChild(ball);
    ball.setMotion(new SpaceMotion2D(new Rectangle(0,0,rootStage.width, rootStage.height), 5, 30));
    ball.x = Science.randInt(0, rootStage.width);
    ball.y = Science.randInt(0, rootStage.height);
    motionPanel.title = "2D Space Motion";

    initMask();
    }

    private function initMask():void{
    ballMask = new Sprite();
    ballMask.graphics.beginFill(0x000000);
    ballMask.graphics.drawRect(0,0,rootStage.width, rootStage.height);
    ballMask.graphics.endFill();
    rootStage.addChild(ballMask);
    rootStage.mask = ballMask;
    }

    private function run():void{
    ball.motion.run();
    }

    private function stop():void{
    ball.motion.stop();
    }

    private function onMotionClicked(event:MouseEvent):void{
    var running:Boolean = ball.motion.running;

    switch(event.target.label){
    case "2D Space Motion":
    ball.setMotion(new SpaceMotion2D(new Rectangle(0,0,rootStage.width, rootStage.height), 5, 30));
    break;
    case "2D Circle Motion":
    ball.setMotion(new CircleMotion2D(ball.x, ball.y, 50, 0.1, 30, CircleMotion2D.CW));
    break;
    }

    motionPanel.title = event.target.label;

    if(running){
    run();
    }
    }
    ]]>
    </mx:Script>

    <mx:VBox verticalGap="4" left="10" top="10" right="10" bottom="10">
    <mx:Panel id="motionPanel" width="100%" height="240" layout="absolute" title="2D Space Motion" horizontalAlign="center" verticalAlign="middle" cornerRadius="5">
    <mx:UIComponent id="rootStage" width="100%" height="100%" x="0"/>
    </mx:Panel>
    <mx:ApplicationControlBar width="100%" height="100%">
    <mx:Button label="Run" click="run()" width="100%" height="100%"/>
    <mx:Button label="Stop" click="stop()" width="100%" height="100%"/>
    <mx:Button label="2D Space Motion" click="onMotionClicked(event)" width="100%" height="100%"/>
    <mx:Button label="2D Circle Motion" click="onMotionClicked(event)" width="100%" height="100%"/>
    </mx:ApplicationControlBar>
    </mx:VBox>
    </mx:Application>

    從Flex Reference裡可以得知UIComponent是FlexSprite的子類別,而FlexSprite繼承於Sprite,所以當我們要加入當初在Flash底下設計的DisplayObject時,要在Flex主場景裡新增一個UIComponent來包住Ball物件。

    Flex Reference - UIComponent

    從這樣的特性可以得知,以後如果要直接在Flex撰寫ActionScript可以直接繼承於UIComponent即可。

No comments:

Post a Comment

Orange - data analysis tool

Installation pip install orange3 Run orange python -m Orange.canvas