(译)移动的键盘控制

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的方法,个人讲,我倾向于第二种,因为它更灵活,并且占的内存没那么大!
  

//做一个movieclip叫sqr的,链接属性设为第一倒出,标释符号是sqr
onClipEvent(load) { step=5; movieWidth=300; movieHeight=300; this.stop();//开始是空的,这样可以节省内存的资源 } onClipEvent(enterFrame) { if (Key.isDown (Key.RIGHT) && this._x < movieWidth) { this.attachMovie("sqr", "sqr1", 10 ); this._x+=step; } 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; }
//假如我们要按左键也有影片附加上来的话
this.attachiMovie(“run_left“,“run“,10)
this.attachiMovie(“run_Right“,“run“,10)
this.attachiMovie(“run_UP“,“run“,10)
this.attachiMovie(“run_DOWN“,“run“,10)
//注意要同一个深度,这样的话,以前的那个影片会自动谢载掉...
//也可以this.run.removeMovieClip()
this.attachMovie(“run_left“,11);











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值