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(Mous开发者_JAVA技巧eEvent.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 );
}
I assume you're writing this function in the actions
of the flash file, and not in a separate .as
file. This function will rely on the stage
object being set (if you were to port this to a class, you may or may not have to wait for the ADDED_TO_STAGE
event.)
function chgColorBlue():void
{
var element = stage.getChildByName('shape1');
element.graphics.beginFill(0x000099);
element.graphics.drawEllipse(7,35,18,12);
element.graphics.endFill();
}
Edit to show example: You can call this function after creating the object. It defeats the purpose of trying to access the element by the name attribute.
function chgColorBlue(element:Sprite):void
{
element.graphics.beginFill(0x000099);
element.graphics.drawEllipse(7,35,18,12);
element.graphics.endFill();
}
精彩评论