MX2004 as2.0制作一个控制移动的类
新建一个as文件,存为moveclass.as
class moveclass extends MovieClip //定义这个类为MovieClip扩展类: { var speed:Number; var w:Number; var h:Number; //定义三个变量分别为速度,移动边界宽,移动边界长 function move_ctrl(w:Number,h:Number,speed:Number) //写一个有三个变量的move_ctrl方法,用于控制MovieClip; {
if(Key.isDown(Key.RIGHT)) //得到方向键的确定 {
this._x +=speed; //MovieClip向X轴移动speed位置 this._rotation = 90;//转向 if(this._x > w) //判断是否超过边界宽 { this._x = w; } }
//下面同上进行方向键判断 if(Key.isDown(Key.LEFT )) { this._x -=speed; this._rotation = 270; if(this._x < 0) { this._x = 0; } } if(Key.isDown(Key.UP)) { this._y -=speed; this._rotation = 0; if(this._y < 0) { this._y = 0; } } if(Key.isDown(Key.DOWN)) { this._y +=speed; this._rotation = 180; if(this._y > h) { this._y = h; } }
//微调方向,小转弯. if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.DOWN)) { this._rotation = 315; } if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) { this._rotation = 45; } if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.UP)) { this._rotation = 225; } if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.UP)) { this._rotation = 135; }
} }
然后建立一个fla,在里面建立一个MovieClip,比如汽车等要移动的物体. Ctrl+L.调出库,点这个MC,按右键,选择Linkage,用于把这个MC扩展 成CLASS类.
如图所示进行设置:


然后.返回Layer1,放入这个MC.并命名为Linkage里所取名一样.

现在我们在Layer1的第一帧里写上:
|