Actionscript 3 AS3架构:如何在一个对象的单个实例与多个调用之间进行交互?

Actionscript 3 AS3架构:如何在一个对象的单个实例与多个调用之间进行交互?,actionscript-3,flash,flashdevelop,removechild,Actionscript 3,Flash,Flashdevelop,Removechild,我是actionscript新手,在使用FlashDevelop显示单个对象实例时遇到问题 我有一个main.as,其中我显示了一个图像作为背景。然后我显示一个矩形,其中包含一些文本,当鼠标悬停在目标上时,这些文本会变为两个文本(在舞台上出现/消失)。矩形位于类TextBox.as中 我知道我的代码非常混乱,因为每次我到达目标(调用tween)时,它都会创建一个新的矩形实例。但是如果我试着改变它,它会给我错误。此外,我似乎无法删除矩形(使用removeChild()),一旦创建矩形,它就找不到子

我是actionscript新手,在使用FlashDevelop显示单个对象实例时遇到问题

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

我知道我的代码非常混乱,因为每次我到达目标(调用tween)时,它都会创建一个新的矩形实例。但是如果我试着改变它,它会给我错误。此外,我似乎无法删除矩形(使用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} );
        }
    }

提前感谢。

我不知道您想要实现什么目标,但您应该重用您的textbox实例,例如:

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

对我只需在main中声明TextBox,它就可以工作了!我忘了。所以我可以使用:
addChild(textbox)而不是:
addChild(新文本框)谢谢!不客气。请把这个答案标记为正确:)谢谢。