更新:このコードが機能するかどうかわかりませんが、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);
}
クールでより簡単!しかし、私は複数のインスタンスを実装している場合、私は唯一の最新のスプライトを認識することができますが、古いものは失われます、彼らの名前を呼び出すことはできませんでした、私は個別に変更することができます – Proyb2
あなたは何も失うことはありません! Spriteの新しいインスタンスを作成し、名前を付けてコンテナに追加した瞬間から、changeSpriteColorメソッドを使用して色を変更することができます。コンテナから削除され、作成した関数のスコープ外の変数に割り当てられていない場合にのみ、Sprite参照が失われます。 – PatrickS
あなたのコードに従って、UIComponentとSpriteはグローバルとして宣言されます。「shape1」と「shape2」を作成する2つの関数を実行すると、「shape1」はステージでは消えますが、shape2は残ります。 – Proyb2