通过调用名字变色 [英] change color by calling name

查看:115
本文介绍了通过调用名字变色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:不知道这code会的工作,但无法得到精灵,其中 c.getChildByName(spName)是空的名称, spName 是精灵的字符串名称。

 私人变种S:雪碧;
                私人VAR S2:雪碧;
    私人变种C:UIComponent;
                私人VAR C2:UIComponent;

                私有函数drawQuarterNotes(XX:INT,TY:字符串):无效{
                    如果(TY ==向上){
                        S =新的Sprite();
                        S2 =新的Sprite();

                        C =新UIComponent();
                        C2 =新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(多个);
                        c.ro​​tation = -20;
                        c.x = XX;
                        c.y = XX;
                    }
                    如果(TY ==下){
                        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(多个);
                        c.ro​​tation = -20;
                        c.x = XX;


c.y = XX;
                }
                的addElement(C);
                的addElement(C2);
            }

            私有函数drawQuarterNotes2(XX:INT,TY:字符串):无效{
                如果(TY ==向上){
                    S =新的Sprite();
                    S2 =新的Sprite();

                    C =新UIComponent();
                    C2 =新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(多个);
                    c.ro​​tation = -20;
                    c.x = XX;
                    c.y = XX;
                }
                如果(TY ==下){
                    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(多个);
                    c.ro​​tation = -20;
                    c.x = XX;
                    c.y = XX;
                }
                的addElement(C);
                的addElement(C2);
            }
 

解决方案

 私人变种C:UIComponent =新UIComponent();
    私人VAR S1:雪碧=新的Sprite();
    私人VAR S2:雪碧=新的Sprite();

    私有函数的init():无效
    {
       s1.name =shape1;
       s2.name =shape2;

       //确保S2不会出现在S1之上
       s2.x = 100;

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

       c.addChild(S1);
       c.addChild(S2);
       的addElement(C);

       //改变shape1红色
       changeSpriteColor('shape1,0x990000);

       //改变shape2为灰色
       changeSpriteColor('shape2',值0x777777);
    }

    //通过调用其名称更改子画面的颜色
    私有函数changeSpriteColor(spName:字符串,颜色:UINT):无效
    {
        VAR孩子:雪碧= c.getChildByName(spName)作为精灵;

        //确保儿童已被添加到容器
        如果(孩子!= NULL)
           shapeGraphics(孩子,颜色);
    }

    //改变精灵的颜色
    私有函数shapeGraphics(SP:雪碧,颜色:UINT):无效
    {
       //确保清除图形,改变颜色之前
       //还有其他的方法来改变颜色
       //但我按照你的code!
       sp.graphics.clear();
       sp.graphics.beginFill(0x333333);
       sp.graphics.drawEllipse(7,35,18,12);
       sp.graphics.endFill();
    }

    私有函数chgColorBlue(E:MouseEvent)方法:无效
    {
       shapeGraphics(e.currentTarget,0x000099);
    }

    私有函数chgColorReset(E:MouseEvent)方法:无效
    {
       shapeGraphics(e.currentTarget,0x333333);
    }
 

Update: Not sure if this code would work, but couldn't get the name of the sprite where c.getChildByName(spName) is null, spName is the string name of the sprite.

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

解决方案

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

这篇关于通过调用名字变色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆