AS3架构:如何在多个调用中补间对象的单个实例?

时间:2013-09-11 11:32:37

标签: actionscript-3 flash flashdevelop removechild

我是动手脚本的新手,在使用FlashDevelop显示对象的单个实例时遇到了麻烦。

我有一个main.as,其中我将图像显示为背景。然后我显示一个矩形,其中包含一些文本,当鼠标悬停目标(在舞台上出现/消失)时补间。矩形在TextBox.as类中。

我知道我的代码非常混乱,因为它每次到达目标时都会创建一个新的矩形实例(调用补间)。但如果我试图切换它,它会给我带来错误。此外,我似乎无法删除我的矩形(使用removeChild())一旦创建它,​​它找不到孩子。

有人能指出我应该使用的架构是什么,只创建一个矩形实例?

这是我的一些代码:

//IMPORT LIBRARIES
import Classes.TextBox;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import com.greensock.TweenLite;

// Setup SWF Render Settings
[SWF(width = "620", height = "650")]

public class Main extends Sprite 
{
    //DEFINING VARIABLES
    [Embed(source="../lib/myimage.jpg")]
    private var picture:Class;
    private var myTween:TweenLite;

    //CONSTRUCTOR 
    public function Main():void 
    {   
        addChild(new TextBox);
        addChild(new picture);
        addEventListener(MouseEvent.MOUSE_OVER, appear);
    }

    //ROLLDOWN FUNCTION
    public function appear(e:MouseEvent):void 
    {
        trace("Appear");
        var text:TextBox = new TextBox();
        addChild(text);
        addChild(new picture);

        if (picture) {
            removeEventListener(MouseEvent.MOUSE_OVER, appear);
            //addEventListener(Event.COMPLETE, appearComplete);
            myTween = new TweenLite(text, 1, { y:340 , onComplete:appearComplete, onReverseComplete:disappearComplete} );
        }
    }

提前致谢。

1 个答案:

答案 0 :(得分:1)

我不知道你想要实现什么补间,但是你应该重用你的文本框实例,例如:

import Classes.TextBox;
import com.greensock.TweenLite;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

[SWF(width = "620", height = "650")]
public class Main extends Sprite {

    [Embed(source="../lib/myimage.jpg")]
    private var pictureClass:Class;

    private var picture:Bitmap;
    private var textbox:TextBox;

    public function Main():void {
        if (stage)
            init();
        else
            addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        picture = new pictureClass();
        textbox = new TextBox();

        addChild(picture);
        addChild(textbox);

        addEventListener(MouseEvent.MOUSE_OVER, tween);
    }

    public function tween(e:MouseEvent):void {
        removeEventListener(MouseEvent.MOUSE_OVER, tween);
        TweenLite.to(textbox, 1, { y:340, onComplete:reverse } );
    }

    private function reverse():void {
        TweenLite.to(textbox, 1, { y:0, onComplete:tweenComplete } );
    }

    private function tweenComplete():void {
        addEventListener(MouseEvent.MOUSE_OVER, tween);
    }
}