2010-12-27 13 views
0

shape1、shape2、shape3などと異なる名前の同じスプライトのインスタンスを複数作成するにはどうすればいいですか?その場合、関数はgetChildByNameを通してスプライトの色を変更しますか?新しいインスタンスを作成するかどうかにかかわらず、古いスプライトは新しいスプライトによって上書きされ、古い「shape1」を呼び出すとnullになります。どのようにスプライトをすべて保持するのですか?複数スプライト異なる名前

var c:UIComponent = new UIComponent(); 
    var s:Sprite = new Sprite(); 

private function shapeCreate() { 
     s.graphics.beginFill(0x333333); 
     s.graphics.drawEllipse(7,35,18,12); 
     s.graphics.endFill(); 
     s.name = "shape1"; 
      s.addEventListener(MouseEvent.MOUSE_DOWN,chgColorBlue); 
      s.addEventListener(MouseEvent.MOUSE_UP,chgColorReset); 
      c.addChild(s); 
    addElement(c); 
} 

    private function chgColorBlue(e:MouseEvent):void { 
     e.currentTarget.graphics.beginFill(0x000099); 
     e.currentTarget.graphics.drawEllipse(7,35,18,12); 
     e.currentTarget.graphics.endFill(); 
     } 
+0

私は、以前の記事に私のコメントを追加するには、あなたの前の質問... – PatrickS

+0

感謝する答えを追加しました。 – Proyb2

答えて

0

同じスプライトインスタンスを使用するので、shapeCreate()メソッドを呼び出すと、それがオーバーライドされます。

必要な数のスプライトを定義し、シェイプの作成を個別に適用してオーバーライドしないようにする必要があります。

var c:UIComponent = new UIComponent(); 

//Creating the Sprite instances for the shapes 
var s1:Sprite = new Sprite(); 
s1.name = "s1_sp"; 
var s2:Sprite = new Sprite(); 
s2.name = "s2_sp"; 
var s3:Sprite = new Sprite(); 
s3.name = "s3_sp"; 

private function shapeCreate(s:Sprite) 
{ 
    s.graphics.beginFill(0x333333); 
    s.graphics.drawEllipse(7,35,18,12); 
    s.graphics.endFill(); 
    s.addEventListener(MouseEvent.MOUSE_DOWN,chgColorBlue); 
    s.addEventListener(MouseEvent.MOUSE_UP,chgColorReset); 
    c.addChild(s); 
    addElement(c); 
} 

private function chgColorBlue(e:MouseEvent):void { 
    //Just added this line to clear the old graphics 
    e.currentTarget.graphics.clear(); 

    e.currentTarget.graphics.beginFill(0x000099); 
    e.currentTarget.graphics.drawEllipse(7,35,18,12); 
    e.currentTarget.graphics.endFill(); 
} 


//Creating the shapes 
shapeCreate(s1); 
shapeCreate(s2); 
shapeCreate(s3); 

私はそれがあなたのやりたいことだと思います。

幸運、 ロブ

+0

こんにちは、問題を解決できましたか? – robertp

関連する問題