Flash游戏开发常用函数代

上传人:hao****021 文档编号:166979517 上传时间:2022-11-02 格式:DOC 页数:22 大小:39.50KB
返回 下载 相关 举报
Flash游戏开发常用函数代_第1页
第1页 / 共22页
Flash游戏开发常用函数代_第2页
第2页 / 共22页
Flash游戏开发常用函数代_第3页
第3页 / 共22页
点击查看更多>>
资源描述
Flash游戏开发常用函数代 作者:游牧人2类型:闪吧BBS来源:闪吧对象数组 比如要构建一个有很多属性的数组,简单的可以这样做: 2004代码: vara:Array=newArray(); for(vari=0;i10;i+) ai=newObject(); ai.x=10; ai.y=10*i; ai.name=-; ai.id=i; /随便你给什么属性啊。 /先用局部变量存储对象会更好一点。 vara=newArray() for(vari=0;i10;i+) varta=newObject(); ta.x=10; ta.y=10; ta.name=n+i; a.push(ta); /或者更简便一点的方法: vara=newArray() for(vari=0;i10;i+) ai=x:10,y:10,name:n+i 程序控制角色运动 下面这段代码控制MovieClipmc从(50,50)横向运动到(100,50)停止,速度为5pixel: mc._x=mc._y=50; mc.onEnterFrame=function() _x+=5; trace(_x); if(_x=100) delete(mc.onEnterFrame); ; 精确计时 我们设定了时间为60秒,然后通过setInterval来定期(每1000milliseconds)触发一个函数runTimer。 functionrunTimer用来计时,当时间到了以后,转去执行outThere。 functionoutThere用来处理时间到了以后的动作,不要忘记clearInterval(intervalID),停止计时。 vartime:Number=60; varintervalID:Number; intervalID=setInterval(runTimer,1000); functionrunTimer() time-; trace(time); if(time=0) outThere(); functionoutThere() /blahblahblah clearInterval(intervalID); 找到目前最小的一个可用的深度Depth(来自国外) 针对FlashPlayer6的: availTopDepth=function() varallDepths=; for(varzin_root) if(_rootzinstanceofButton|_rootzinstanceofMovieClip|_rootzinstanceofTextField) allDepths.push(_rootz.getDepth(); if(allDepths.length!=0) sortAll=function(a,b)if(ab)return1;elsereturn0; allDepths.sort(sortAll); return(parseInt(allDepthsallDepths.length-1)+1); ; trace(_root.availTopDepth(); 放置一个MovieClip在Stage,再执行这段Action就会得到最近的一个可用深度。我们也可以用这个来创建绝对不 会出问题的MovieClip,比如: _root.createEmptyMovieClip(mc_name,availTopDepth(); getNextHighestDepth()以及getInstanceAtDepth(depth)是forplayer7的。 鼠标消隐 鼠标消隐之后,右健弹出菜单,鼠标就又出现了,并且一直保留着 完整的解决方法是:在左健抬起、右健第二次抬起、回车键抬起的时候再次消隐鼠标。 完整的方法比较复杂,下面是一个简单的解决方法,可以解决绝大多数的问题: onEnterFrame=function() if(Key.isDown(1)|Key.isDown(Key.ENTER) Mouse.hide(); 我常用的一个加速度移动的代码 /以下代码放到主场景第一帧 stop(); _global.a=5; /移动加速度,越大越慢 _global.click_x=0; /用来记录点击鼠标的位置 _global.click_y=0; _root.onMouseDown=function() _global.click_x=_root._xmouse; _global.click_y=_root._ymouse; /me是受控mc的instancename me.onEnterFrame=function() if(Math.abs(_global.click_x-me._x)2&Math.abs(_global.click_y-me._y)1000|Number(a_v.text)0) /defaultvalue a_v.text=5; else _global.a=Number(a_v.text)?Number(a_v.text):5; /trace(_global.a); ; 随机输出1到100而不从复的语句 seq=newArray(100); pArray=newArray(100); functionmakeRandom() for(i=1;i=100;i+) seqi=A; functionmRandom() while(true) n=int(random(100)+1; if(seqn=A) seqn=0; break; return(n); functionrArray() for(i=1;idis) deleteonEnterFrame; ; 计算两个对象之间/两点之间的距离(注册点) functiongetDistanceOf(target1,target2,x2,y2) if(arguments.length=4) dx=x2-target1; dy=y2-target2; elseif(arguments.length=2) dx=target2._x-target1._x; dy=target2._y-target1._y; returnMath.sqrt(dx*dx+dy*dy); /Arguments对象是一个数组,其中包含作为参数传递给任何函数的值。每次在动作脚本中调用函数时,都会为该函 数自动创建Arguments对象。同时还会创建一个局部变量arguments,使您可引用arguments对象。让播放的MC暂停一段时间 functionpausePlay(sec) pfunc=function() this.play(); clearInterval(this.pint); stop(); this.pint=setInterval(this,pfunc,sec*1000); /这样调用.sec是暂停的时间,单位是秒. pausePlay(2); onHitTest(target),自己写的一个MC事件,当该MC与指定的MChitTest的时候触发事件.其实也没什么特别的地方,一样也是用setInterval来实现 stop(); MovieClip.prototype.listen=function(target) if(this.isHiting=undefined) this.isHiting=this.hitTest(target); if(this.hitTest(target) if(this.isHiting=false) this.broadcastMessage(onHitTest,this,target);/广播事件,给事件传递this和target两个参数 this.isHiting=true; else this.isHiting=false; ;/为MovieClip添加域成员listen成员,用于监视当前对象与目标是否碰撞 MovieClip.prototype.watch=function(target) this.timer=setInterval(this,listen,50,target); ;/以每50毫秒检测一次的速度来检测是否碰撞 MovieClip.prototype.unWatch=function() clearInterval(this.timer); ;/停止对对象的监视 ASBroadcaster.initialize(MovieClip.prototype);/初始化MovieClip原型为事件源 /下面是调用的示例 /假设有两个MovieClip,左边ball,右边wall,让ball不断往wall移动,同时监视wall,一旦击中触发事件onHitTest ball.onEnterFrame=function() this._x+=5; ;/让ball不断往右方移动工 myListener=newObject(); myListener.onHitTest=function(source,target) trace(The+source._name+hit+target._name+.); ; ball.addListener(myListener);/创建监听员并注册给ball ball.watch(wall);/让ball监视wall MD532位码的FLASH算法 /- functionmd5(s) returnbinl2hex(core_md5(str2binl(s),s.length*strsize); functioncore_md5(x,len) xlen5=(xlen5)|(1289)16)+(y16)+(lsw16); return(msw16)|(lsw&65535); functionbit_rol(num,cnt) return(num(32-cnt); functionstr2binl(str) varbin=Array(); varmask=(1strsize)-1; vari=0; while(i5=(bini5)|(str.charCodeAt(i/strsize)&mask)i%32); i=i+strsize; returnbin; functionbinl2hex(binarray) if(hexcase) else varhex_tab=0123456789abcdef; varstr=; vari=0; while(i2)(i%4*8)+4)&15)+hex_tab.charAt(binarrayi 2)(i%4*8)&15); i+; returnstr; varhexcase=0; varstrsize=8; /-以上不用修改, b=md5(xx); /xx可设为任意字符 s=b.toUpperCase(); /转换成大写 trace(b); trace(s); 发点缓动函数 Math.linearTween=function(t,b,c,d) returnc*t/d+b; ; /线性运动函数 Math.easeInQuad=function(t,b,c,d) returnc*(t/=d)*t+b; ; /二次缓入函数 Math.easeOutQuad=function(t,b,c,d) return-c*(t/=d)*(t-2)+b; ; /二次缓出函数 Math.easeINOutQuad=function(t,b,c,d) if(t/=d/2)1) returnc/2*t*t+b; return-c/2*(-t)*(t-2)-1)+b; ; /二次缓入缓出函数 Math.easeInCubic=function(t,b,c,d) returnc*Math.pow(t/d,3)+b; ; /三次缓入函数 Math.easeOutCubic=function(t,b,c,d) returnc*(Math.pow(t/d-1,3)+1)+b; ; /三次缓出函数 Math.easeINOutCubic=function(t,b,c,d) if(t/=d/2)1) returnc/2*Math.pow(t,3)+b; returnc/2*(Math.pow(t-2,3)+2)+b; ; /三次缓入缓出函数 Math.easeInQuart=function(t,b,c,d) returnc*Math.pow(t/d,4)+b; ; /四次缓入函数 Math.easeOutQuart=function(t,b,c,d) return-c*(Math.pow(t/d-1,4)-1)+b; ; /四次缓出函数 Math.easeINOutQuart=function(t,b,c,d) if(t/=d/2)1) returnc/2*Math.pow(t,4)+b; return-c/2*(Math.pow(t-2,4)-2)+b; ; /四次缓入缓出函数 Math.easeInQuint=function(t,b,c,d) returnc*Math.pow(t/d,5)+b; ; /五次缓入函数 Math.easeOutQuint=function(t,b,c,d) returnc*(Math.pow(t/d-1,5)+1)+b; ; /五次缓出函数 Math.easeINOutQuint=function(t,b,c,d) if(t/=d/2)1) returnc/2*Math.pow(t,5)+b; returnc/2*(Math.pow(t-2,5)+2)+b; ; /五次缓入缓出函数 Math.easeInSine=function(t,b,c,d) returnc*(1-Math.cos(t/d*(Math.PI/2)+b; ; /正弦缓出函数 Math.easeOutSine=function(t,b,c,d) returnc*Math.sin(t/d*(Math.PI/2)+b; ; /正弦缓出函数 Math.easeINOutSine=function(t,b,c,d) returnc/2*(1-Math.cos(Math.PI*t/d)+b; ; /正弦缓入缓出函数 Math.easeInExpo=function(t,b,c,d) returnc*Math.pow(2,10*(t/d-1)+b; ; /指数缓入函数 Math.easeOutExpo=function(t,b,c,d) returnc*(-Math.pow(2,-10*t/d)+1)+b; ; /指数缓出函数 Math.easeINOutExpo=function(t,b,c,d) if(t/=d/2)1) returnc/2*Math.pow(2,10*(t-1)+b; returnc/2*(-Math.pow(2,-10*-t)+2)+b; ; /指数缓入缓出函数 Math.easeInCirc=function(t,b,c,d) returnc*(1-Math.sqrt(1-(t/=d)*t)+b; ; /圆形缓入函数 Math.easeOutCirc=function(t,b,c,d) returnc*Math.sqrt(1-(t=t/d-1)*t)+b; ; /圆形缓出函数 Math.easeINOutCirc=function(t,b,c,d) if(t/=d/2)1) returnc/2*(1-Math.sqrt(1-t*t)+b; returnc/2*(Math.sqrt(1-(t-=2)*t)+1)+b; ; /圆形缓入缓出函数
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 中学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!