0

我是 actionscript 的新手,使用 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} );
        }
    }

提前致谢。

4

1 回答 1

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);
    }
}
于 2013-09-11T12:45:43.823 回答