Enemy following me actionscript(二楼附译文)[本文已收录教程库]
Okay I yesterday I was doing some actionscripting to make a game, (cant reveal it yet though because its not my own, I was just hired to make some parts of it). But I would like to share some of the code with you, like the source code I share today.This flash actionscript source code is to make an enemy follow your mouse, and some of the great things to notice is that it not only follows that mouse path, it also rotates so its front view is always facing the mouse, thats pretty cool.
[swf]http://tutorial.jcwcn.com/d/file/2D-Graphics/Flash/Actionscripting/2008-10-28/52611a5c2f9d4f1e4d312e6b6bd73ef7.swf[/swf]
So first we need to do some small preparations like creating our enemy, Im going to let this up to you, do whatever you want. When you are done, right click and convert it to a movieclip and give it an instance name, I named mine "enemy_mc"
[img=338,146]http://tutorial.jcwcn.com/d/file/2D-Graphics/Flash/Actionscripting/2008-10-28/648deec2950ff77e7171d15a34c59335.jpg[/img]
Now we are ready to do some actionscripting, but remember this is for actionscript 3.0, for it to work with previous versions you will need to do some changes and Im not doing it for you. :-)
You can just copy and past the code into your flash actionscript panel, or you can try reading the comments.
The actionscript source code:
First an eventlistener to call the function to make the enemy move, this is an enterframe event.
[list=1][*]enemy_mc.addEventListener(Event.ENTER_FRAME, do_stuff); [/list]
enemy_mc.addEventListener(Event.ENTER_FRAME, do_stuff);
The function we call
[list=1]function do_stuff(event:Event):void { [*]
// here we do some calculations to make the ememy rotate based on the mouse angle, centered to the ememy.
var myRadians:Number = Math.atan2(mouseY-enemy_mc.y, mouseX-enemy_mc.x); [*]
var myDegrees:Number = Math.round((myRadians*180/Math.PI));
[*]
// this is the stuff making the enmy move towards the mouse.
var yChange:Number = Math.round(mouseY-enemy_mc.y); [*]
var xChange:Number = Math.round(mouseX-enemy_mc.x);
var yMove:Number = Math.round(yChange/20); [*]
var xMove:Number = Math.round(xChange/20);
[*]
// Nothing will work without using all this previous calculations, so we set the x and y axis and the rotation.
enemy_mc.y += yMove; [*] enemy_mc.x += xMove; enemy_mc.rotation = myDegrees+90; [*]} [/list]
function do_stuff(event:Event):void { // here we do some calculations to make the ememy rotate based on the mouse angle, centered to the ememy. var myRadians:Number = Math.atan2(mouseY-enemy_mc.y, mouseX-enemy_mc.x); var myDegrees:Number = Math.round((myRadians*180/Math.PI)); // this is the stuff making the enmy move towards the mouse. var yChange:Number = Math.round(mouseY-enemy_mc.y); var xChange:Number = Math.round(mouseX-enemy_mc.x); var yMove:Number = Math.round(yChange/20); var xMove:Number = Math.round(xChange/20); // Nothing will work without using all this previous calculations, so we set the x and y axis and the rotation. enemy_mc.y += yMove; enemy_mc.x += xMove; enemy_mc.rotation = myDegrees+90;}
[[i] 本帖最后由 xyl7422 于 2008-11-19 10:02 编辑 [/i]] 敌人跟随我的AS代码
很高兴我昨天为一个游戏写了一些AS。(虽然不是我自已的而不能展示它,我只是被邀请写了其中的一部分),但是我很乐意与你一起共享其中的一些代码,比如我今天与你共享的一些源代码。
这个flash Actionspript源代码创一个跟随你的鼠标的敌人,特别要注意的是它不只是跟随鼠标的路径,而它的脸部总是随着鼠标旋转。这很酷吧。
首先我们要做一些诸如创建我们的敌人的准备工作,你总会做这些事,无论你是否乐意。当你完成时,右击它将它转换为MC,并为它取一个实例名称,我取的名称是:“"enemy_mc"。
现在我们准备写一些AS代码,但请记住这些AS代码是AS3.0的,为了使它工作你或许应做一些改变版本的事情,我没法为你做这些。:-)
你可以复制并粘贴这些代码到你的flash动面板或者你可以输入这些代码。
AS源代码:
首先是一个使敌有移动的事件监听,这是一个enterframe事件。
enemy_mc.addEventListener(Event.ENTER_FRAME, do_stuff);
要调用的函数
function do_stuff(event:Event):void {
// 这儿计算出敌人中心点随着鼠标旋转的角度.
var myRadians:Number = Math.atan2(mouseY-enemy_mc.y, mouseX-enemy_mc.x);
var myDegrees:Number = Math.round((myRadians*180/Math.PI));
// 这是敌人移向鼠标的参数.
var yChange:Number = Math.round(mouseY-enemy_mc.y);
var xChange:Number = Math.round(mouseX-enemy_mc.x);
var yMove:Number = Math.round(yChange/20);
var xMove:Number = Math.round(xChange/20);
// 不使用上面的计算就没有什么可以人做的事,这样我就设置x和y及旋转.
enemy_mc.y += yMove;
enemy_mc.x += xMove;
enemy_mc.rotation = myDegrees+90;
}
[[i] 本帖最后由 sanbos 于 2008-10-29 17:29 编辑 [/i]] 作业
[swf]http://bbs.jcwcn.com/attachments/month_0810/20081029_6c8f8f476330ff54de7b5pZCg6Bxhw7N.swf[/swf]
[[i] 本帖最后由 xyl7422 于 2008-11-19 09:27 编辑 [/i]] 很好玩的哈,照搬AS做了个玩玩,也算交作业吧。
为什么在我的电脑中蝴蝶会跟着鼠标动的(在FLASH中测试和导出影片都会动的),传上来变不动了呀?
[swf]http://bbs.jcwcn.com/attachments/month_0810/20081029_147ddb305553d8023736w9zpUz5MN9wE.swf[/swf]
好奇怪,再这里下载后,回到自己的电脑中又会跟着鼠标动了哈!
[[i] 本帖最后由 xyl7422 于 2008-11-19 09:26 编辑 [/i]] 我不明白这个教程是AS3的那么为什么我做不来,用AS2也做不出来,我导了楼上一个人的代码确不是用原文张的代码做的,即然那个做不出来,为什么不改改,这样的教程是好教程吗,不是误人时间和误人子弟吗,
这是我做的答案TypeError: Error #1034: 强制转换类型失败:无法将 "[object MovieClip]-8" 转换为 flash.display.MovieClip。
at _fla::MainTimeline/yd()
我刚学做代码不会改,希望楼主能帮忙看看 [quote]我不明白这个教程是AS3的那么为什么我做不来,用AS2也做不出来,我导了楼上一个人的代码确不是用原文张的代码做的,即然那个做不出来,为什么不改改,这样的教程是好教程吗,不是误人时间和误人子弟吗,
这是我做的 ...
[size=2][color=#999999]冰天使LOVE 发表于 2009-7-14 14:18[/color] [url=http://bbs.jcwcn.com/redirect.php?goto=findpost&pid=5664956&ptid=212230][img]http://bbs.jcwcn.com/images/common/back.gif[/img][/url][/size][/quote]
这个教程没有任何问题。不知道你是怎样做的。 [i=s] 本帖最后由 从天而降 于 2009-10-7 00:07 编辑 [/i]
做好了,交作业! 7楼朋友的作业
[swf]http://bbs.jcwcn.com/attachments/month_0910/09100700077240f6e628c78780.swf[/swf] [i=s] 本帖最后由 zctmh0336 于 2009-10-7 15:54 编辑 [/i]
交作业了。
[swf]http://bbs.jcwcn.com/attachments/month_0910/0910071459de5024737901d6f3.swf[/swf] 交作业.
做个简化版的
[swf]http://bbs.jcwcn.com/attachments/month_0910/091009161180e599696388a096.swf[/swf] [i=s] 本帖最后由 linjieatmxd 于 2009-10-30 20:35 编辑 [/i]
哈哈,我林杰来交作业啦~希望大家看到我的作业不要有太大的反应啊{:1_174:} 11楼的作业:
[swf]http://bbs.jcwcn.com/attachments/month_0910/091030201866fbebd13c8af0b2.swf[/swf] 请问var myDegrees:Number = Math.round((myRadians*180/Math.PI))这一句是什么意思?我不太理解啊,还有结尾那句为什么还要加90度,麻烦傻博士给我讲解一下好吗? [b] [url=http://bbs.jcwcn.com/redirect.php?goto=findpost&pid=6126131&ptid=212230]13#[/url] [i]linjieatmxd[/i] [/b]
var myDegrees:Number = Math.round((myRadians*180/Math.PI))
这句是将算出的鼠标与对象的角的弧度转换为角度值,以便用来设置对象的旋转度。
加90度应该是确定对象的旋转方向。 谢谢傻博士,我懂了,原来atan2返回值是以弧度制表示的呀,开始我以为返回值是角度制呢,呵呵。 请教,我做了个enemy_mc 接着照抄了代码 出现下面的错误是什么原因?新手求教
[attach]1254223[/attach] [i=s] 本帖最后由 zctmh0336 于 2009-11-5 20:25 编辑 [/i]
谢谢博士老师,辛苦
[swf]http://bbs.jcwcn.com/attachments/month_0911/0911051641b93113ec2502cb8e.swf[/swf] [b] [url=http://bbs.jcwcn.com/redirect.php?goto=findpost&pid=6136838&ptid=212230]16#[/url] [i]nono123456[/i] [/b]
是AS3文档? 回博士,我用的是cs4 as3的文档,我才刚学的几天实在有点迷糊啊.
做这个我的步骤是1.随便做个元件给它as个类enemy_mc 2.在场景中第一帧中写入你说的那段代码.
之后测试影片就出现16楼那图. 是不是我漏了什么步骤了呢,我菜鸟啊真不好意思... [b] [url=http://bbs.jcwcn.com/redirect.php?goto=findpost&pid=6140509&ptid=212230]19#[/url] [i]nono123456[/i] [/b]
将你做的元件放到舞台上,然后给它取个实例名称:enemy_mc [i=s] 本帖最后由 nono123456 于 2009-11-7 11:20 编辑 [/i]
感谢博士指点. 教师典范~ 21楼朋友的作业
[swf]http://bbs.jcwcn.com/attachments/month_0911/0911071116c5e3111030efebb0.swf[/swf]
页:
[1]