/*
This example shows how a class can inherit from the built-in type MovieClip.
Any class that inherits from this type can be attached to a movie clip object
in the library. When an instance of such a movie clip is placed on the stage,
either programmatically or at runtime, an instance of the class is
automatically created and acts as the ActionScript interface to the
MovieScript object. This example causes any movie clip in the library to
bounce around on the stage at a random speed.
The inheritance approach to controlling stage objects with classes is the most
common, mainly because it is simple to implement. Just extend MovieClip with
new functionality and link it to a library object. However, this approach
limits a library object to a single class of functionality that is fixed at design time.
*/
class Bouncer extends MovieClip {
//实例变量
private var y_vel:Number;
private var x_vel:Number;
private var x_min:Number;
private var x_max:Number;
private var y_min:Number;
private var y_max:Number;
private var dragging:Boolean = false;
//构造器
public function Bouncer() {
//为注册点确定一个范围
var halfWidth = this._width/2;
var halfHeight = this._height/2;
var clipBounds = _root.getBounds();
this.x_min = clipBounds.xMin + halfWidth;
this.y_min = clipBounds.yMin + halfHeight;
this.x_max = clipBounds.xMax - halfWidth;
this.y_max = clipBounds.yMax - halfHeight;
//选择一个随机方向开始移动这个MC.
this.x_vel = Math.floor(Math.random()*21)-10;
this.y_vel = Math.floor(Math.random()*21)-10;
}
//事件处理, 每帧相应一次
function onEnterFrame(){
//如果我们正在拖拽,不要移动目标
if (this.dragging) return;
//基于目标的速度计算下一个位置
var next_x = this._x + this.x_vel;
var next_y = this._y + this.y_vel;
//确认我们没有超出左边界
if (next_x < this.x_min){
this.x_vel *= -1;
next_x = this.x_min + (this.x_min - next_x);
}
//确认我们没有超出右边界
else if (next_x > this.x_max){
this.x_vel *= -1;
next_x = this.x_max - (next_x - this.x_max);
}
//确认我们没有超出上边界
if (next_y < this.y_min){
this.y_vel *= -1;
next_y = this.y_min + (this.y_min - next_y);
}
//确认我们没有超出下边界
else if (next_y > this.y_max){
this.y_vel *= -1;
next_y = this.y_max - (next_y - this.y_max);
}
//把已经计算好的下一个位置分配给图形元件的X,Y值
this._x = next_x;
this._y = next_y;
}
//事件处理,当目标被触发(鼠标被按下),开始相应
function onPress() {
//在MC上开始拖拽运动 (通过FLASH自动进行)
this.startDrag();
//开始拖拽时做一个标志
this.dragging = true;
}
//事件相应,当目标被释放(鼠标左键松开),开始相应
function onRelease() {
//停止拖拽
this.stopDrag();
this.dragging = false;
}
//事件相应,当目标被释放(鼠标松开在相应范围之外),开始相应
function onReleaseOutside() {
//停止拖拽,如果我们正确的拖拽
if(this.dragging) {
this.stopDrag();
this.dragging = false;
}
}
}
//dynamically add mouse chasers to the movie
for(var i:Number=1; i < 10; i++) {
//create the chaser graphic
c = this.attachMovie('chaser_mc', 'chaser'+i, i);
//create the chaser class that will control the graphics
obj = new Chaser(0.05*i, c);
}
(注意最后一句bj = new Chaser(0.05*i, c); 第二个参数是我们创建的对影片剪辑对象)
//代码注释略
interface Scalable {
//scale the width and height of an object by a factor
public function Scale(w:Number, h:Number);
}
//(注意第一行用interface声明,而不是Class,最后一行只有方法声明而没有内容)
现在我们在Chaser类中具体化该方法:
1 、 修改Chaser类中的代码和Demo.as第一帧里的代码,被修改部分已经标蓝.
class Chaser implements Scalable {
private var speed:Number;
private var graphic:MovieClip;
public function Scale(w:Number, h:Number) {
//scale the width and height of the graphic by a factor
this.graphic._width *= w;
this.graphic._height *= h;
}
...
//dynamically add mouse chasers to the movie
for(var i:Number=1; i < 10; i++) {
//create the chaser graphic
c = this.attachMovie('chaser_mc', 'chaser'+i, i);
//create the chaser class that will control the graphics
obj = new Chaser(0.05*i, c);
obj.Scale(1.0/i, 1.0/i)
}