ActionScrip3从多个嵌套容器中获取特定子项

时间:2016-08-26 12:49:27

标签: actionscript-3

我想请求你帮助ActionScript 3.如何从两个孩子(具体的titleBannerPicture)中访问一个来自函数TitleBannerEffects()的调用(parent是titleBannerContainer sprite对象)。像titleBannerContainer.titleBannerPicture或this.titleBannerContainer.titleBannerPicture或this.titleBannerPicture这样的经典方法返回错误或未定义状态。此语法仅适用于构造函数TitleBanner()函数。请有人帮我解决这个平庸的问题。

谢谢。

代码示例:

package myscript {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.BitmapData
import flash.display.Bitmap;
import flash.filters.BitmapFilterQuality;
import flash.filters.GlowFilter;
import flash.filters.DropShadowFilter;
import Math;

public dynamic
class TitleBanner extends Sprite {
    // Premenne nastavania objektu
        public var pictureName_str: String = "Title_Banner_Picture";
        public var maskName_str: String = "Title_Banner_Mask";
    // Premenne tlacitok
        public var buttonActivation: Boolean = true;
    // Premenne efektu zmeny obrazku
        private var actualPicturePosition: String = "LeftUp";
        private var nextPicturePosition: String = "RightUp";
        private var titleBannerMaskWidth: Number;
        private var titleBannerMaskHeight: Number;

    // Konstruktor
    public function TitleBanner(): void {
        // Nacitanie Container-u
            var titleBannerContainer: Sprite = new Sprite();
            titleBannerContainer.name = "titleBannerContainer";
            this.addChild(titleBannerContainer);
        // Nacitanie masky do Container-u: titleBannerContainer
            var titleBannerMask_mc: Title_Banner_Mask = new Title_Banner_Mask();
            titleBannerMask_mc.name = "titleBannerMask_mc";
            titleBannerContainer.addChild(titleBannerMask_mc);
        // Nacitanie obrazku do Container-u: titleBannerContainer
            var titleBannerPicture: Bitmap = new Bitmap(new Title_Banner_Picture());
            titleBannerPicture.name = "titleBannerPicture";
            titleBannerContainer.addChild(titleBannerPicture);
        // Priradenie masky
            titleBannerPicture.mask = titleBannerMask_mc;
        // Nastavenie filtrov Container-u
            titleBannerContainer.filters = TitleBannerFilters();
        // Nastavenie efektu posuvania obrazkov
            titleBannerMaskWidth = Math.ceil(titleBannerMask_mc.width);
            titleBannerMaskHeight = Math.ceil(titleBannerMask_mc.height);   
            TitleBannerEffects();  
    }
    private function TitleBannerFilters(): Array {
        // Nastavenie Glow efektu
            var titleBannerGlowEffect: GlowFilter = new GlowFilter();
        //titleBannerGlowEffect.alpha = 1;          
            titleBannerGlowEffect.color = 0xffffff;
            titleBannerGlowEffect.blurX = 20;
            titleBannerGlowEffect.blurY = 20;
            titleBannerGlowEffect.inner = true;
            titleBannerGlowEffect.strength = 2;
            titleBannerGlowEffect.quality = BitmapFilterQuality.HIGH;
        // Nastavenie Shadow efektu
            var titleBannerShadowEffect: DropShadowFilter = new DropShadowFilter();
            titleBannerShadowEffect.alpha = 1;
            titleBannerShadowEffect.color = 0x666666;
            titleBannerShadowEffect.blurX = 4;
            titleBannerShadowEffect.blurY = 4;
            titleBannerShadowEffect.inner = true;
            titleBannerShadowEffect.angle = 45;
            titleBannerShadowEffect.distance = 4;
            titleBannerShadowEffect.strength = 1;
            titleBannerShadowEffect.quality = BitmapFilterQuality.HIGH;
        // Navratova premenna
            return [titleBannerGlowEffect, titleBannerShadowEffect];
    }
    private function TitleBannerEffects(): void {
        switch (actualPicturePosition) {
            // Prechod prveho obrazku
                case "LeftUp":
                    switch (nextPicturePosition) {
                        case "RightUp":
                            trace("LeftUp -> RightUp");
                            //trace(this.getChildAt(0).name].numChildren);                  
                        break;
                        case "LeftDown":
                            trace("LeftUp -> LeftDown");
                            break;
                        case "RightDown":
                            trace("LeftUp -> RightDown");
                            break;
                        default:
                            trace("No transition");
                            break;
                    }
                    break;
            // Prechod druheho obrazku
                case "RightUp":
                    switch (nextPicturePosition) {
                        case "LeftDown":
                            trace("RightUp -> LeftDown");
                            break;
                        case "RightDown":
                            trace("RightUp -> RightDown");
                            break;
                        case "LeftUp":
                            trace("RightUp -> LeftUp");
                            break;
                        default:
                            trace("No transition");
                            break;
                    }
                    break;
            // Prechod tretieho obrazku
                case "LeftDown":
                    switch (nextPicturePosition) {
                        case "RightDown":
                            trace("LeftDown -> RightDown");
                            break;
                        case "LeftUp":
                            trace("LeftDown -> LeftUp");
                        break;
                        case "RightUp":
                            trace("LeftDown -> RightUp");
                            break;
                        default:
                            trace("No transition");
                            break;
                    }
                    break;
            // Prechod stvrteho obrazku
                case "RightDown":
                    switch (nextPicturePosition) {
                        case "LeftUp":
                            trace("RightDown -> LeftUp");
                            break;
                        case "RightUp":
                            trace("RightDown -> RightUp");
                            break;
                        case "LeftDown":
                            trace("RightDown -> LeftDown");
                            break;
                        default:
                            trace("No transition");
                            break;
                    }
                    break;
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

我相信如果你想从构造函数外部访问titleBannerContainer或它的子元素,那么变量需要成为文档类的属性。您在构造函数中声明变量,因此它只存在于构造函数中。要使该变量成为文档类的属性,请将变量声明放在构造函数之外,如下所示:

class TitleBanner extends Sprite {
     // variables declared here are called properties of the document class.
     // you have some. 
     // add your titleBannerContainer here
     var titleBannerContainer:Sprite = new Sprite();

现在您可以从其他功能访问titleBannerContainer了。

答案 1 :(得分:0)

感谢您的回复,我很感激。然而,经过一天半的尝试和搜索,我找到了一些形式的解决方案:

Object(this.getChildAt(0)).getChildAt(1))

从函数TitleBannerEffects()返回我想要的对象titleBannerPicture调用。

再次感谢您