当前位置: 首页 > 图文教程 > Flash动画 > Flash动画制作 > 为AS2.0添砖加瓦-编写类扩展(2)
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;
}
}
}



car.onEnterFrame = function() {
car.move_ctrl(300,200,10);
}
speed =5;
w = Stage.width;
h = Stage.height;
car.onEnterFrame = function() {
car.move_ctrl(w,h,speed);
}
评论 (0) All