Example of how to add the same Display object multiple times in Flash cs5

Source: Internet
Author: User
Tags addchild

No matter how many times the same Display object is added to the display list by code, only one display object is displayed on the screen.

For example, a star is generated in sampleadd of the document class. The two containers ConA AND Conb are used. The ConA coordinate has no position. The default value is (0, 0). In the upper-left corner of the screen, the Conb coordinate is set to 200, that is, 200 pixels are moved to the right, and in (, 0 ). The vertical coordinate of star is set to 100, indicating that it is 100 pixels down relative to the coordinate origin of the parent container.

Three sprite buttons are also generated, marked as A, B, and x respectively. Click a to add stars to the container. Click B to add stars to container B. However, no matter how many times we click, only one star is shielded, you can add stars as much as you do. Click X to remove the object from the display list.

Sampleadd. CS (document class)

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. 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 ("container A has added Stars");} else {Conb. addchild (STAR); trace ("container B has added Stars") ;}} private function removestar (EVT: mouseevent): void {If (ConA. contains (STAR) {ConA. removechild (STAR); trace ("Remove star from container a");} else if (Conb. contains (STAR) {Conb. removechild (STAR); trace ("Remove star from container B");} else {trace ("the star does not exist, no need to remove ");}}}}

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();}}}

Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.