您的位置:首页 > 其它

(译)移动的键盘控制

2004-11-30 00:50 567 查看
Mad Sci 著
在这篇教程中我主要讲用键盘事件触发不同的移动类型,我们将从简单的开始,然后再完成一些先进的编码技术,我假设你熟悉了flash5.0(2k4也受用哦)的脚本语言,让我们从简单的开始....
场景大小为300*300
准备一个mc放到时间轴
onClipEvent(enterFrame)
{
if (Key.isDown (Key.RIGHT)) this._x+=5;
if (Key.isDown (Key.LEFT)) this._x-=5;
if (Key.isDown (Key.UP)) this._y-=5;
if (Key.isDown (Key.DOWN)) this._y+=5;
}
//这样你就可以用 上,下,左,右键控制影片了mc了...
//注意左上角的坐标是(0,0)哦!

下面我们做点改变:
onClipEvent(enterFrame)
{
if (Key.isDown (Key.RIGHT)) this._x+=5;
else if (Key.isDown (Key.LEFT)) this._x-=5;
else if (Key.isDown (Key.UP)) this._y-=5;
else if (Key.isDown (Key.DOWN)) this._y+=5;
}
//这样的话,按键就唯一了,不会出现两键一起按都有用的....
我们注意到这样的影片的速度是5象素/帧,所以帧频设高点平滑和快些!

我们再改变一点点...让程序更容易扩展,更容易改变速度!
onClipEvent(Load) step+=5;
//加载时候初始化数据step...

onClipEvent(enterFrame)
{
if (Key.isDown (Key.RIGHT)) this._x+=step;
else if (Key.isDown (Key.LEFT) ) this._x-=step;
else if (Key.isDown (Key.UP) ) this._y-=step;
else if (Key.isDown (Key.DOWN) ) this._y+=step;
}
//到目前为止,这个已经够好了...但是问题还是存在的,因为,影片可以跑道场景的外面去,这显然我们无法忍受,那怎么改呢?其实,只要做一点点的改变就行了,加个边界判断的语句就行了...
onClipEvent(load)
{
 step=5;
 }


onClipEvent(enterFrame)
{
 if(Key.isDown(Key.RIGHT)&&this._x<300) this._x+=step;
 if(Key.isDown(Key.LEFT)&&this._x>0)  this._x-=step;
 if(Key.isDown(Key.UP)&&this._y>0) this._y-=step;
 if(Key.isDown(Key.DOWN)&&this._y<300) this._y+=step;
 }
//你看不懂的话,可以试着运行这些代码!

2.让你的影片动起来
  有两种方法,一是,Keyframes 二是,attachmovie的方法,个人讲,我倾向于第二种,因为它更灵活,并且占的内存没那么大!
  

onClipEvent(load)
{
step=5;
movieWidth=300;
movieHeight=300;
this.stop();
}

onClipEvent(enterFrame)
{
if (Key.isDown (Key.RIGHT) && this._x < movieWidth)
{

}
else if (Key.isDown (Key.LEFT) && this._x > 0) this._x-=step;
else if (Key.isDown (Key.UP) && this._y > 0) this._y-=step;
else if (Key.isDown (Key.DOWN) && this._y < movieHeight) this._y+=step;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  flash 脚本 扩展 语言 up