AS3控制儿童的动画片段

时间:2012-04-11 01:27:03

标签: actionscript-3 flash

我很少有来自库的movieclip会添加到舞台上,在movieclip里面有一些我要控制它的mc,这是我的脚本。

if (selectDiffText.text == "Collection 1 Easy")
    {
        var c1_easy:cartoonEasy = new cartoonEasy();
        addChild(c1_easy);
        c1_easy.x = 412;
        c1_easy.y = 400;
        TweenMax.from(c1_easy, 0.5, {alpha:0, ease:Expo.easeOut});
    }
    else if (selectDiffText.text == "Collection 1 Medium")
    {
        var c1_medium:cartoonMedium = new cartoonMedium();
        addChild(c1_medium);
        c1_medium.x = 412;
        c1_medium.y = 400;
        TweenMax.from(c1_medium, 0.5, {alpha:0, ease:Expo.easeOut});
    }
else
    {
        trace("ERROR!");
    }

如果 selectDiffText 更改,上面的代码将从库中加载特定的动画片段。

var movieList:Array = [cartoonMedium1,cartoonMedium2,cartoonMedium3,cartoonMedium4,cartoonMedium5,cartoonMedium6];

function getRandomMovie():MovieClip
{
    var index:int = Math.floor(Math.random() * movieList.length);
    var mcClass:Class = movieList.splice(index,1)[0];
    return new mcClass();
}
playGame = getRandomMovie();
addChild(playGame);

假设如果将c1_medium添加到舞台上,c1_medium还会从库中随机添加1个动画片段。

playGame mc内部,我已经 mouseTarget.alpha = 0; ...如何从根级别控制它? c1_easy也有mouseTarget.alpha = 0。

1 个答案:

答案 0 :(得分:0)

要轻松完成此操作,c1_easyc1_medium都应该扩展同一个类。例如:

class c1_easy extends CartoonGenericClass{[...]}

class c1_medium extends CartoonGenericClass{[...]}

CartoonGenericClass中,您将拥有变量mouseTarget。你使用getter / setter公开这个:

protected var _mouseTarget:Number;

public function get mouseTarget():Number{
    return _mouseTarget;
}

public function set mouseTarget(value:Number):void{
    // (should probably do some verification on "value" here)
    _mouseTarget.alpha = value;
}

这样就可以从任何可以访问mouseTargetc1_easy的对象访问变量c1_medium(请注意,没有下划线):

c1_easy.mouseTarget = 0;
c1_medium.mouseTarget = .3;

等。这是标准的OOP,所以可能是时候破解文档了。 HTH!