2010-12-26 12 views
0

更新:このコードが機能するかどうかわかりませんが、c.getChildByName(spName)がnullの場合はスプライトの名前を取得できませんでした。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); 
      } 

答えて

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

クールでより簡単!しかし、私は複数のインスタンスを実装している場合、私は唯一の最新のスプライトを認識することができますが、古いものは失われます、彼らの名前を呼び出すことはできませんでした、私は個別に変更することができます – Proyb2

+0

あなたは何も失うことはありません! Spriteの新しいインスタンスを作成し、名前を付けてコンテナに追加した瞬間から、changeSpriteColorメソッドを使用して色を変更することができます。コンテナから削除され、作成した関数のスコープ外の変数に割り当てられていない場合にのみ、Sprite参照が失われます。 – PatrickS

+0

あなたのコードに従って、UIComponentとSpriteはグローバルとして宣言されます。「shape1」と「shape2」を作成する2つの関数を実行すると、「shape1」はステージでは消えますが、shape2は残ります。 – Proyb2

1

私はあなたが別の.asファイルにフラッシュファイルのactionsでこの機能を書き込み、していないと仮定します。この関数は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(); 
} 
+0

のErm、私はまだあなたのコードしようとした後になって、フラッシュビルダーを使用しています「TypeError例外を:エラー#1009:nullのオブジェクト参照のプロパティまたはメソッドにアクセスすることはできません。」 – Proyb2

+0

トレースからnullを取得しますが、 "shape1"の後に.nameを追加することもできません。 – Proyb2

+0

修正点は、要素をパラメータとして関数に渡し、 'var element = stage.getChildByName( 'shape1');'行を削除することです。関数の宣言は 'function chgColorBlue(element:Sprite):void'になります。要素が作成された後に**関数を呼び出さないか、要素をステージに直接追加せず、別の要素のコンテキストで 'getChildByName'を呼び出す必要があります。 – zzzzBov

関連する問題