Flash CS5 多次添加同一个显示对象例子
来源:互联网 发布:用spss主成分分析数据 编辑:程序博客网 时间:2024/06/02 03:15
同一个显示对象不论被代码加入显示列表多少次,在屏幕上只会有一个显示对象。
例:文档类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();}}}
效果
- Flash CS5 多次添加同一个显示对象例子
- 殿堂之路P361>>多次添加同一个显示对象
- 同一个view多次添加到同一个控制器,只相当于一次
- 为什么多次调用同一个对象的HashCode方法返回不一样
- Flash cs5 初试
- Hello,Flash CS5
- Adobe Flash CS5 快捷键
- Flash CS5 快捷键
- 多次映射同一个类
- Flash Professional CS5的代码提示不显示的终极解决办法
- 对flash cs5的展望
- Flash cs5 新增的功能
- Flash CS5 新特征预览
- Flash CS5暗藏物理引擎
- flash cs5,获取本地视频
- flash cs5 as3.0 基础
- Flash CS5 XFL文件格式解读
- dreamweaver CS5找不到flash插件,
- 推荐] C语言 内存管理详解
- objective-C 中两种实现动画的方法
- global index & local index的区别
- 80x86存储器组织结构
- iphone xcode项目运行流程
- Flash CS5 多次添加同一个显示对象例子
- 基于java的网络抓包技术研究与实现(转)
- android (1)
- 基于角色的权限设计 - 类图
- UDP广播与多播
- 操作Cookie
- java 用可获取的字体、样式、字号修饰文字
- c3p0配置详解
- 声明式函数与函数例子