Flash CS5 多次添加同一个显示对象例子

同一个显示对象不论被代码加入显示列表多少次,在屏幕上只会有一个显示对象。

例:文档类SampleAdd中生成一个星星显示对象star,两个容器conA和conB。ConA坐标没有位置,默认是(0,0),在屏幕左上方;ConB横坐标设为200,即向右移动了200像素,在(200,0)。star的纵坐标设为100,表示它相对于父容器的坐标原点往下移100像素。

   同时还生成了3个Sprite当按钮用,分别标记为A,B,X。单击A,则在容器中添加入星星;单击B,则在容器B 中加入星星,但是,不论我们点多少次,屏蔽上只会有一个星星,不会点多少就加多少星星。单击X,则把对象从显示列表中移除。

SampleAdd .cs(文档类)

package 
{
 import flash.display.Sprite;
 import flash.events.MouseEvent;

 public class SampleAdd extends Sprite
 {

  private var conA:Sprite;
  private var conB:Sprite;
  private var buttonA:RectSprite;
  private var buttonB:RectSprite;
  private var buttonX:RectSprite;
  private var star:StartShape;

  public function SampleAdd()
  {
   star=new StartShape();
   star.y = 100;
   conA=new Sprite();
   conB=new Sprite();
   conB.x = 200;
   addChild(conA);
   addChild(conB);

   buttonA = new RectSprite("A",0xff9900);
   buttonB = new RectSprite("B",0x669900);
   buttonX = new RectSprite("X",0x669900);

   buttonA.y = 100;
   buttonB.x = 150;
   buttonB.y = 100;
   buttonX.y = 160;
   buttonX.x = 50;
   addChild(buttonA);
   addChild(buttonB);
   addChild(buttonX);
   buttonA.addEventListener(MouseEvent.CLICK,addStarInContainer);
   buttonB.addEventListener(MouseEvent.CLICK,addStarInContainer);
   buttonX.addEventListener(MouseEvent.CLICK,removeStar);

   trace(this.contains(star));
  }
  private function addStarInContainer(evt:MouseEvent):void
  {
   if (evt.currentTarget == buttonA)
   {
    conA.addChild(star);
    trace("容器A加入了星星");
   }
   else
   {
    conB.addChild(star);
    trace("容器B加入了星星");
   }

  }
  private function removeStar(evt:MouseEvent):void
  {
   if (conA.contains(star))
   {
    conA.removeChild(star);
    trace("移除容器A的星星");
   }
   else if (conB.contains(star))
   {
    conB.removeChild(star);
    trace("移除容器B的星星");
   }
   else
   {
    trace("星星已经不存在,不需要再移除");
   }
  }
 }

}

RectSprite.cs

package  {
 import flash.text.TextField;
 import flash.display.Sprite;
 
 public class RectSprite extends Sprite{
  private var _lable:TextField
  
  public function RectSprite(lableName:String,color:uint) {
   this.graphics.lineStyle(2,0x85DB18);
   this.graphics.beginFill(color);
   this.graphics.drawRoundRect(0,0,100,50,10,10);
   this.graphics.endFill();
   
   _lable=new TextField();
   _lable.htmlText="<font size='24'><b>"+ lableName +"</b></font>";
   _lable.autoSize="left";
   addChild(_lable);
  }

 }
 
}


StartShape .cs

package  {
	//import flash.display.Sprite;
	import flash.display.GradientType;
	import flash.display.Shape;
	
	public class StartShape extends Shape{
		
		public function StartShape(x:Number=0,y:Number=0,
					points:int=5,innerRadius:Number=20,
					outerRadius:Number=50,angle:Number=0,
					color:uint=0xff0000) {
			var count=Math.abs(points);
			this.graphics.lineStyle(2,0x85DB18);
			this.graphics.beginFill(color);
			if(count > 2){
				var step,halfStep,start,n,dx,dy;
				step=(Math.PI*2)/points;
				halfStep=step/2;
				
				start=(angle/180)*Math.PI;
				this.graphics.moveTo(x+(Math.cos(start)*outerRadius),
						  y-(Math.sin(start)*outerRadius));
				
				for(n=1; n <= count; n++){
					dx=x+Math.cos(start+(step*n)-halfStep)*innerRadius;
					dy=y-Math.sin(start+(step*n)-halfStep)*innerRadius;
					this.graphics.lineTo(dx,dy);
					
					dx=x+Math.cos(start+(step*n))*outerRadius;
					dy=y-Math.sin(start+(step*n))*outerRadius;
					this.graphics.lineTo(dx,dy);
				}
			}
			this.graphics.endFill();
			
		}

	}
	
}


 效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值