通过呼叫名称来改变颜色

时间:2010-12-26 17:46:23

标签: 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 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);
            }

2 个答案:

答案 0 :(得分:1)

我假设您在Flash文件的actions中编写此函数,而不是在单独的.as文件中。此函数将依赖于正在设置的stage对象(如果您要将其移植到某个类,则可能需要或可能不必等待ADDED_TO_STAGE事件。)

function chgColorBlue():void
{
  var element = stage.getChildByName('shape1');
  element.graphics.beginFill(0x000099);
  element.graphics.drawEllipse(7,35,18,12);
  element.graphics.endFill();
}

编辑以显示示例: 您可以在创建对象后调用此函数。它失败了尝试通过name属性访问元素的目的。

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

答案 1 :(得分:1)

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