【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一个可以显示在屏幕上一例子!!
[Actionscript3] 纯文本查看 复制代码 //向上转换;
//文档类;
package
{
import flash.display.Sprite;
import flash.display.Stage;
import son.Shaperect;
import son.Shapecircle;
import son.Shapeellipse;
import son.Shapedraw;
public class doc extends Sprite
{
public function doc()
{//若在构造器中先实例化各个图形,生成的各个图形的位置在同一处;
/* var _shaperect:Shaperect = new Shaperect(this);
var _shapecircle:Shapecircle = new Shapecircle(this);
var _shapeellipse:Shapeellipse = new Shapeellipse(this);
var _shapedraw:Shapedraw = new Shapedraw(this);
*/
//用for循环来生成50个位置不同大小不同的图形;
for (var i:int=0; i<50; i++)
{
var randi:Number = Math.random();
trace(i);
if (randi<0.3)
{
randdraw( new Shaperect(this));
}
else if (randi<0.7)
{
randdraw(new Shapecircle(this));
}
else
{
randdraw(new Shapeellipse(this));
}
}
}
private function randdraw(sh:Shapedraw)
{
sh.shapefill(Math.random()*0xFFFFFF);//填充随机的颜色;
sh.shapemove(stage.stageWidth*Math.random(),stage.stageHeight*Math.random());//摆放随机的位置;
}
}
}
//向上转换
//定义一个父类:Shapedraw;
package son
{
import flash.display.Shape;//导入画图工具:
import flash.display.DisplayObjectContainer;//导入容器类:
public class Shapedraw
{
protected var _shape:Shape;
public function Shapedraw(ds:DisplayObjectContainer)
{
_shape=new Shape();
ds.addChild(_shape);//把画的图放入容器中;
}
protected function shapedraw()
{//画图方法不写,方便后面的子类重写方法
}
//填充方法;
public function shapefill(e:uint)
{
_shape.graphics.beginFill(e,Math.random());
shapedraw();
_shape.graphics.endFill();
}
//摆放位置方法;
public function shapemove(ex:Number,ey:Number)
{
_shape.x = ex;
_shape.y = ey;
}
}
}
//子类:Shaperect;
package son
{
import flash.display.DisplayObjectContainer;
public class Shaperect extends Shapedraw
{
public function Shaperect(ds:DisplayObjectContainer)
{
super(ds);//用super来调用父类的构造函数;
}
//重写画图方法,画矩形
override protected function shapedraw()
{
_shape.graphics.drawRect(0,0,_shape.stage.width*0.25*Math.random(),_shape.stage.height*0.25*Math.random());
}
}
}
//子类:Shapecircle;
package son
{
import flash.display.DisplayObjectContainer;
public class Shapecircle extends Shapedraw
{
public function Shapecircle(ds:DisplayObjectContainer)
{
super(ds);//用super来调用父类的构造函数;
}
//重写画图方法,画圆
override protected function shapedraw()
{
_shape.graphics.drawCircle(0,0,_shape.stage.width*0.2*Math.random());
}
}
}
//子类:Shapeellipse;
package son
{
import flash.display.DisplayObjectContainer;
public class Shapeellipse extends Shapedraw
{
public function Shapeellipse(ds:DisplayObjectContainer)
{
super(ds);//用super来调用父类的构造函数;
}
//重写画图方法,画椭圆
override protected function shapedraw()
{
_shape.graphics.drawEllipse(0,0,_shape.stage.width*0.25*Math.random(),_shape.stage.height*0.25*Math.random());
}
}
}
|