(译)移动的键盘控制
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;
}
在这篇教程中我主要讲用键盘事件触发不同的移动类型,我们将从简单的开始,然后再完成一些先进的编码技术,我假设你熟悉了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;
}
相关文章推荐
- 我的应聘总结!
- duwamish7的感悟!
- 帮帮我好不好!!!!
- 備份服務器端SQL SERVER數據庫至本地目錄
- Struts tags (2) ----Bean(1)
- Struts tags (4) ----Html(2)
- 4年程序员感悟
- 开发工具浅谈
- 一种基于R-Tree的改进多维索引[待正稿]
- 解决打过震荡波补丁之后oracle无法正常启动的问题
- thinking in c++卷2
- Struts国际化问题
- GSFL:一种网格服务的工作流架构(一)
- win2003搜索字词问题
- SymbianOS Series 60学习笔记二:ListBox控件
- java调用exe或sh的代码
- 潘爱民的经历
- UBB代码的实现(ASP)
- 如何写好论文
- Intermidiate EDI--CPFR