Apache flex 通过调用名称来改变颜色

Apache flex 通过调用名称来改变颜色,apache-flex,flash,actionscript,Apache Flex,Flash,Actionscript,更新:不确定此代码是否有效,但无法获取精灵的名称,其中c.getChildByName(spName)为空,spName是精灵的字符串名称 private var s:Sprite; private var s2:Sprite; private var c:UIComponent; private var c2:UIComponent; private function drawQuart

更新:不确定此代码是否有效,但无法获取精灵的名称,其中
c.getChildByName(spName)
为空,
spName
是精灵的字符串名称

 private var s:Sprite;
                private var s2:Sprite;
    private var c:UIComponent;
                private var c2:UIComponent;

                private function drawQuarterNotes(xx:int,ty:String):void {
                    if(ty=="up") {
                        s = new Sprite();
                        s2 = new Sprite();

                        c = new UIComponent();
                        c2 = new UIComponent();

                        s2.graphics.lineStyle(3,0x333333);
                        s2.graphics.moveTo(20,0);
                        s2.graphics.lineTo(20,50);
                        c2.addChild(s2);
                        c2.x = xx+16;
                        c2.y = xx-18;

                        s.graphics.beginFill(0x333333);
                        s.graphics.drawEllipse(7,35,18,12);
                        s.graphics.endFill();
                        s.name = "asd2";
                        s.addEventListener(MouseEvent.MOUSE_DOWN,chgColor);
                        s.addEventListener(MouseEvent.MOUSE_UP,chgColorReset);
                        c.addChild(s);
                        c.rotation = -20;
                        c.x = xx;
                        c.y = xx;
                    }
                    if(ty=="down") {
                        s2.graphics.lineStyle(3,0x333333);
                        s2.graphics.moveTo(20,0);
                        s2.graphics.lineTo(20,50);
                        c2.addChild(s2);
                        c2.x = xx+1;
                        c2.y = xx+35;

                        s.graphics.beginFill(0x333333);
                        s.graphics.drawEllipse(7,35,18,12);
                        s.graphics.endFill();
                        c.addChild(s);
                        c.rotation = -20;
                        c.x = xx;


c.y = xx;                   
                }
                addElement(c);
                addElement(c2);
            }

            private function drawQuarterNotes2(xx:int,ty:String):void {
                if(ty=="up") {
                    s = new Sprite();
                    s2 = new Sprite();

                    c = new UIComponent();
                    c2 = new UIComponent();

                    s2.graphics.lineStyle(3,0x333333);
                    s2.graphics.moveTo(20,0);
                    s2.graphics.lineTo(20,50);
                    c2.addChild(s2);
                    c2.x = xx+16;
                    c2.y = xx-18;

                    s.graphics.beginFill(0x333333);
                    s.graphics.drawEllipse(7,35,18,12);
                    s.graphics.endFill();
                    s.name = "asd1";
                    s.addEventListener(MouseEvent.MOUSE_DOWN,chgColor);
                    s.addEventListener(MouseEvent.MOUSE_UP,chgColorReset);
                    c.addChild(s);
                    c.rotation = -20;
                    c.x = xx;
                    c.y = xx;
                }
                if(ty=="down") {
                    s2.graphics.lineStyle(3,0x333333);
                    s2.graphics.moveTo(20,0);
                    s2.graphics.lineTo(20,50);
                    c2.addChild(s2);
                    c2.x = xx+1;
                    c2.y = xx+35;

                    s.graphics.beginFill(0x333333);
                    s.graphics.drawEllipse(7,35,18,12);
                    s.graphics.endFill();
                    c.addChild(s);
                    c.rotation = -20;
                    c.x = xx;
                    c.y = xx;                   
                }
                addElement(c);
                addElement(c2);
            }

我假设您是在flash文件的
操作
中编写此函数,而不是在一个单独的
.as
文件中。此函数将依赖于设置的
stage
对象(如果要将其移植到类,可能需要等待
添加到\u stage
事件)

编辑以显示示例: 您可以在创建对象后调用此函数。它无法通过name属性访问元素

function chgColorBlue(element:Sprite):void
{
  element.graphics.beginFill(0x000099);
  element.graphics.drawEllipse(7,35,18,12);
  element.graphics.endFill();
}

呃,我正在使用Flash builder,在尝试您的代码“TypeError:Error#1009:无法访问空对象引用的属性或方法”后仍然会得到空值。我会从跟踪中得到空值,但在“shape1”后添加.name修复方法可能只是将元素作为参数传递给函数,并删除
var元素=stage.getChildByName('shape1')行。然后,函数声明将是
函数chgColorBlue(元素:Sprite):void
。我猜您可能是在创建元素后没有调用函数,或者您没有直接将元素添加到stage,需要在不同元素的上下文中调用
getChildByName
。它看起来如何?你能给我举个例子吗?现在出去吃午饭。那很酷而且更容易!但我有一个问题,如果我实现了多个实例,唯一最新的精灵可以被识别,旧的会丢失,意味着不能叫他们的名字,我如何保留所有的,这样我就可以单独更改?你不会丢失任何东西!从创建一个新的精灵实例开始,给它一个名称并将其添加到容器中,就可以使用changeSpriteColor方法更改其颜色。如果从容器中删除了一个Sprite引用,并且没有将其分配给创建它的函数范围之外的变量,则只会丢失它。按照您的代码,当我运行两个创建“shape1”和“shape2”的函数“shape1”时,UIComponent和Sprite被声明为全局的在舞台上消失,但形状2保留。我已经更新了我的代码,看看这是否是用2个音符编码的正确方法。假设一个是四分音符名称“asd2”,另一个是asd1,我感到困惑,为什么它不能与许多组合一起工作。哦,那么我们必须为每个图形创建尽可能多的精灵?不能共享吗?如果是,我现在有了更好的理解。左一件事是创建新的sprite作为变量。感谢
function chgColorBlue(element:Sprite):void
{
  element.graphics.beginFill(0x000099);
  element.graphics.drawEllipse(7,35,18,12);
  element.graphics.endFill();
}
    private var c:UIComponent = new UIComponent();
    private var s1:Sprite = new Sprite();
    private var s2:Sprite = new Sprite();

    private function init():void
    {
       s1.name = "shape1";
       s2.name = "shape2";

       //make sure s2 doesn't appear on top of s1
       s2.x = 100;

       s1.addEventListener(MouseEvent.MOUSE_DOWN,  chgColorBlue);
       s1.addEventListener(MouseEvent.MOUSE_UP,    chgColorReset);

       c.addChild(s1);
       c.addChild(s2);
       addElement(c);

       //change shape1 to red
       changeSpriteColor( 'shape1' , 0x990000 );

       //change shape2 to grey
       changeSpriteColor( 'shape2' , 0x777777 );
    }

    //change a sprite color by calling its name
    private function changeSpriteColor( spName:String , color:uint ):void
    {
        var child:Sprite = c.getChildByName(spName ) as Sprite;

        //make sure the child has been added to the container
        if( child != null )
           shapeGraphics( child , color );
    }

    //change a sprite color
    private function shapeGraphics( sp:Sprite  , color:uint ):void
    {
       //make sure to clear the graphics , before changing the color
       //there are other ways to change the color
       //but i'm following your code!
       sp.graphics.clear();
       sp.graphics.beginFill(0x333333);
       sp.graphics.drawEllipse(7,35,18,12);
       sp.graphics.endFill();  
    }

    private function chgColorBlue(e:MouseEvent):void 
    {
       shapeGraphics( e.currentTarget , 0x000099 );
    }

    private function chgColorReset(e:MouseEvent):void 
    {
       shapeGraphics( e.currentTarget , 0x333333 );
    }