当前位置:七道奇文章资讯设计学堂Flash设计
日期:2009-04-04 00:59:00  来源:本站整理

<b>Flash游戏开辟常用函数代</b>[Flash设计]

赞助商链接



  本文“<b>Flash游戏开辟常用函数代</b>[Flash设计]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
■■■对象数组
比方要构建一个有很多属性的数组,简单的可以这样做:
2004 代码:
var a:Array = new Array();
for (var i = 0; i<10; i++) {
a[i] = new Object();
a[i].x = 10;
a[i].y = 10*i;
a[i].name = "---";
a[i].id = i;
// 随便你给什么属性啊.
}
////先用部分变量存储对象会更好一点.
var a=new Array()
for(var i=0;i<10;i++){
var ta=new Object();
ta.x=10;
ta.y=10;
ta.name="n"+i;
a.push(ta);
}
////大概更简便一点的办法:
var a=new Array()
for(var i=0;i<10;i++){
a[i]={x:10,y:10,name:"n"+i}
}


■■■程序掌握角色运动
下面这段代码掌握 MovieClip mc 从 (50,50) 横向运动到(100,50) 终止,速度为 5 pixel:
mc._x = mc._y=50;
mc.onEnterFrame = function() {
_x += 5;
trace(_x);
if (_x == 100) {
delete (mc.onEnterFrame);
}
};


■■■切确计时
我们设定了时间为 60 秒,然后通过 setInterval 来按期(每1000milliseconds)触发一个函数 runTimer.
function runTimer 用来计时,当时间到了今后,转去履行 outThere.
function outThere 用来处理时间到了今后的行动,不要忘掉 clearInterval(intervalID),终止计时.
var time:Number = 60;
var intervalID:Number;
intervalID = setInterval(runTimer, 1000);
function runTimer() {
time--;
trace(time);
if (time == 0) {
outThere();
}
}
function outThere() {
// blah blah blah
clearInterval(intervalID);
}


■■■找到目前最小的一个可用的深度 Depth(来自国外)
针对 Flash Player 6 的:
availTopDepth = function () {
var allDepths = [];
for (var z in _root) {
if (_root[z] instanceof Button || _root[z] instanceof MovieClip || _root[z] instanceof TextField) {
allDepths.push(_root[z].getDepth());
}
}
if (allDepths.length != 0) {
sortAll = function (a, b) { if (ab) {return 1;} else {return 0;}};
allDepths.sort(sortAll);
return (parseInt(allDepths[allDepths.length-1])+1);
}
};
trace(_root.availTopDepth());
安排一个 MovieClip 在 Stage,再履行这段 Action 就会得到近来的一个可用深度.我们也可以用这个来成立绝对不

会出问题的 MovieClip,比方:
_root.createEmptyMovieClip("mc_name", availTopDepth());
getNextHighestDepth() 以及 getInstanceAtDepth(depth)是 for player 7 的.


■■■鼠标消隐
鼠标消隐之后,右健弹出菜单,鼠标就又呈现了,并且一向保存着~
完好的办理办法是:在 左健抬起、右健第二次抬起、回车键抬起的时刻再次消隐鼠标.
完好的办法对比复杂,下面是一个简单的办理办法,可以办理绝大大都的问题:
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的instance name
me.onEnterFrame = function() {
if (Math.abs(_global.click_x-me._x)<2 && Math.abs(_global.click_y-me._y)<2) {
//假如已经基本移到了鼠标点过的位置就撤消这个事件句柄的呼应程序
delete (this.onEnterFrame);
return;
} else {
//掌握移动
me._x += (_global.click_x-me._x)/_global.a;
me._y += (_global.click_y-me._y)/_global.a;
}
};
};
//////以下部份可选
//a_v是个可以输入的文本框,用来输入加快度,便可以自定义速度,便利调试
a_v.onChanged = function() {
if (a_v.text == "" || Number(a_v.text)>1000 || Number(a_v.text)<0) {
//default value
a_v.text = 5;
} else {
_global.a = Number(a_v.text) ? Number(a_v.text) : 5;
//trace(_global.a);
}
};


■■■随机输出1到100而不从复的语句~~~
seq = new Array(100);
pArray = new Array(100);
function makeRandom() {
for (i=1; i<=100; i++) {
seq[i] = "A";
}
}
function mRandom() {
while (true) {
n = int(random(100))+1;
if (seq[n] == "A") {
seq[n] = "0";
break;
}
}
return (n);
}
function rArray() {
for (i=1; i<=100; i++) {
pArray[i] = mRandom();
trace(pArray[i]);
}
}
stop();


■■■偷梁换柱~~~
我们可以用这样一个本领,在某些函数外部加一些东西而不需求理解函数的代码~~
比方,假定当前有很多mc 覆盖了onEnterFrame函数,我们可以这样理解那些在_root下的mc 的onEnterFrame函

数是以什么次序履行的:
代码:
for(var i in _root){
if(_root[i].onEnterFrame != null){//假如这个Object实现了onEnterFrame接口的话
_root[i].oldFunc = _root[i].onEnterFrame;//首先保存本来的onEnterFrame的句柄
_root[i].onEnterFrame = function(){//然后开始替换了~~加上一层壳~~
trace(this+ ": onEnterFrame Called.");
this.oldFunc();//我们用保存的句柄调用本来的函数
trace(this+ ": onEnterFrame Returned.");
}
}
}
假如要撤消这些附加的操作:
代码:
for(var i in _root){
if(_root[i].oldFunc!= null){//假如发现改过得痕迹
_root[i].onEnterFrame = _root[i].oldFunc;
delete(oldFunc);
}
}
呵呵这些操作的关键在于 flash里面的引用机制:
全部函数名都是引用,而函数本身是浮在数据的海洋里面的.假如没有任何变量引用他,他就会被回收.假若有,

他就不会被回收.所以有着样一个风趣的事情:delete函数不会删掉任何东西,除了要求删掉的那个名字:(和java

机制类似,而和C差别)

a= function(){trace("Function a called");}
b=a;
delete(a);//除了a这个名字被删掉以外,没有任何事情发生
b();

函数通过this得到其调用者的实例.


■■■MovieClipPlus.as
定义了一些简单的MC操作
//画点:由于flash没有画点,那我们就画短线
MovieClip.prototype.point = OxMovieClipPoint;
function OxMovieClipPoint(x, y) {
this.moveTo(x, y);
this.lineTo(x+1, y);
}
//画矩形,左上角x1,y1 右下角x2,y2, 颜色rgb, 不透明度alpha
MovieClip.prototype.rect = OxMovieClipRect;
function OxMovieClipRect(x1, y1, x2, y2, rgb, alpha) {
this.beginFill(rgb, alpha);
this.moveTo(x1, y1);
this.lineTo(x1, y2);
this.lineTo(x2, y2);
this.lineTo(x2, y1);
this.lineTo(x1, y1);
this.endFill();
}
//画圆:这么bt的冬冬当然不是我写的,作者素Jody Keating
//圆心x,y ,半径r
MovieClip.prototype.circle = OxMovieClipCircle;
function OxMovieClipCircle(x, y, r) {
var a = r*0.414213562;
var b = r*0.707106781;
this.moveTo(x+r, y);
this.curveTo(x+r, y-a, x+b, y-b);
this.curveTo(x+a, y-r, x, y-r);
this.curveTo(x-a, y-r, x-b, y-b);
this.curveTo(x-r, y-a, x-r, y);
this.curveTo(x-r, y+a, x-b, y+b);
this.curveTo(x-a, y+r, x, y+r);
this.curveTo(x+a, y+r, x+b, y+b);
this.curveTo(x+r, y+a, x+r, y);
}


■■■两点间的直线移动
var s = 15;
_root.onMouseDown = function() {
var oldM_x = _root._xmouse;
var oldM_y = _root._ymouse;
ax = aa._x;
ay = aa._y;
dis = Math.sqrt((oldM_x-ax)*(oldM_x-ax)+(oldM_y-ay)*(oldM_y-ay));
xa = (oldM_x-ax)/dis;
ya = (oldM_y-ay)/dis;
amove();
};
function amove() {
onEnterFrame = function () {
aa._x += s*xa;
aa._y += s*ya;
if (Math.sqrt((aa._x-ax)*(aa._x-ax)+(aa._y-ay)*(aa._y-ay))>dis) {
delete onEnterFrame;
}
};
}


■■■计算两个对象之间/两点之间的距离(注册点)
function getDistanceOf(target1, target2, x2, y2) {
if (arguments.length == 4) {
dx = x2-target1;
dy = y2-target2;
} else if (arguments.length == 2) {
dx = target2._x-target1._x;
dy = target2._y-target1._y;
}
return Math.sqrt(dx*dx+dy*dy);
}
//Arguments 对象是一个数组,此中包含作为参数传送给任何函数的值.每次在行动脚本中调用函数时,城市为该函

数自动成立 Arguments 对象.同时还会成立一个部分变量 arguments,使您可引用 arguments 对象.

■■■让播放的MC暂停一段时间
function pausePlay(sec) {
pfunc = function () {
this.play();
clearInterval(this.pint);
}
stop();
this.pint = setInterval(this, "pfunc", sec*1000);
}
//这样调用.sec是暂停的时间,单位是秒.
pausePlay(2);


■■■onHitTest(target),自己写的一个MC事件,当该MC与指定的MC hitTest的时刻触发事件.其实也没什么分外的地方,一样也是用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 = new Object();
myListener.onHitTest = function(source,target){
trace("The " + source._name + " hit " + target._name + ".");
};
ball.addListener(myListener);//成立监听员并注册给ball
ball.watch(wall);//让ball监督wall
■■■MD5 32位码的FLASH算法
//-----------
function md5(s) {
return binl2hex(core_md5(str2binl(s), s.length*strsize));
}
function core_md5(x, len) {
x[len >> 5] = (x[len >> 5]) | (128 << len%32);
x[(((len+64) >>> 9) << 4)+14] = len;
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var i = 0;
while (i var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
a = md5_ff(a, b, c, d, x[i+0], 7, -680876936);
d = md5_ff(d, a, b, c, x[i+1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i+2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i+3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i+4], 7, -176418897);
d = md5_ff(d, a, b, c, x[i+5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i+6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i+7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i+8], 7, 1770035416);
d = md5_ff(d, a, b, c, x[i+9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i+12], 7, 1804603682);
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
a = md5_gg(a, b, c, d, x[i+1], 5, -165796510);
d = md5_gg(d, a, b, c, x[i+6], 9, -1069501632);
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i+0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i+5], 5, -701558691);
d = md5_gg(d, a, b, c, x[i+10], 9, 38016083);
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i+4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i+9], 5, 568446438);
d = md5_gg(d, a, b, c, x[i+14], 9, -1019803690);
c = md5_gg(c, d, a, b, x[i+3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i+8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i+13], 5, -1444681467);
d = md5_gg(d, a, b, c, x[i+2], 9, -51403784);
c = md5_gg(c, d, a, b, x[i+7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
a = md5_hh(a, b, c, d, x[i+5], 4, -378558);
d = md5_hh(d, a, b, c, x[i+8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i+1], 4, -1530992060);
d = md5_hh(d, a, b, c, x[i+4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i+7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i+13], 4, 681279174);
d = md5_hh(d, a, b, c, x[i+0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i+3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i+6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i+9], 4, -640364487);
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i+2], 23, -995338651);
a = md5_ii(a, b, c, d, x[i+0], 6, -198630844);
d = md5_ii(d, a, b, c, x[i+7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i+5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i+12], 6, 1700485571);
d = md5_ii(d, a, b, c, x[i+3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i+1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i+8], 6, 1873313359);
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i+6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i+4], 6, -145523070);
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i+2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i+9], 21, -343485551);
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
i = i+16;
}
return Array(a, b, c, d);
}
function md5_cmn(q, a, b, x, s, t) {
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
}
function md5_ff(a, b, c, d, x, s, t) {
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t) {
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t) {
return md5_cmn((b ^ c) ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t) {
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function safe_add(x, y) {
var lsw = (x & 65535)+(y & 65535);
var msw = ((x >> 16)+(y >> 16))+(lsw >> 16);
return (msw << 16) | (lsw & 65535);
}
function bit_rol(num, cnt) {
return (num << cnt) | (num >>> (32-cnt));
}
function str2binl(str) {
var bin = Array();
var mask = (1 << strsize)-1;
var i = 0;
while (i<(str.length*strsize)) {
bin[i >> 5] = (bin[i >> 5]) | ((str.charCodeAt(i/strsize) & mask) << i%32);
i = i+strsize;
}
return bin;
}
function binl2hex(binarray) {
if (hexcase) {
} else {
}
var hex_tab = "0123456789abcdef";
var str = "";
var i = 0;
while (i<(binarray.length*4)) {
str = str+(hex_tab.charAt(((binarray[i >> 2]) >> ((i%4*8)+4)) & 15)+hex_tab.charAt(((binarray[i

>> 2]) >> (i%4*8)) & 15));
i++;
}
return str;
}
var hexcase = 0;
var strsize = 8;
//--------以上不用改正,
b = md5("xx");
//xx可设为肆意字符
s = b.toUpperCase();
//转换成大写
trace(b);
trace(s);


■■■发点缓动函数
Math.linearTween = function(t, b, c, d) {
return c*t/d+b;
};
//线性运动函数
Math.easeInQuad = function(t, b, c, d) {
return c*(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) {
return c/2*t*t+b;
}
return -c/2*((--t)*(t-2)-1)+b;
};
//二次缓入缓出函数
Math.easeInCubic = function(t, b, c, d) {
return c*Math.pow(t/d, 3)+b;
};
//三次缓入函数
Math.easeOutCubic = function(t, b, c, d) {
return c*(Math.pow(t/d-1, 3)+1)+b;
};
//三次缓出函数
Math.easeINOutCubic = function(t, b, c, d) {
if ((t /= d/2)<1) {
return c/2*Math.pow(t, 3)+b;
}
return c/2*(Math.pow(t-2, 3)+2)+b;
};
//三次缓入缓出函数
Math.easeInQuart = function(t, b, c, d) {
return c*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) {
return c/2*Math.pow(t, 4)+b;
}
return -c/2*(Math.pow(t-2, 4)-2)+b;
};
//四次缓入缓出函数
Math.easeInQuint = function(t, b, c, d) {
return c*Math.pow(t/d, 5)+b;
};
//五次缓入函数
Math.easeOutQuint = function(t, b, c, d) {
return c*(Math.pow(t/d-1, 5)+1)+b;
};
//五次缓出函数
Math.easeINOutQuint = function(t, b, c, d) {
if ((t /= d/2)<1) {
return c/2*Math.pow(t, 5)+b;
}
return c/2*(Math.pow(t-2, 5)+2)+b;
};
//五次缓入缓出函数
Math.easeInSine = function(t, b, c, d) {
return c*(1-Math.cos(t/d*(Math.PI/2)))+b;
};
//正弦缓出函数
Math.easeOutSine = function(t, b, c, d) {
return c*Math.sin(t/d*(Math.PI/2))+b;
};
//正弦缓出函数
Math.easeINOutSine = function(t, b, c, d) {
return c/2*(1-Math.cos(Math.PI*t/d))+b;
};
//正弦缓入缓出函数
Math.easeInExpo = function(t, b, c, d) {
return c*Math.pow(2, 10*(t/d-1))+b;
};
//指数缓入函数
Math.easeOutExpo = function(t, b, c, d) {
return c*(-Math.pow(2, -10*t/d)+1)+b;
};
//指数缓出函数
Math.easeINOutExpo = function(t, b, c, d) {
if ((t /= d/2)<1) {
return c/2*Math.pow(2, 10*(t-1))+b;
}
return c/2*(-Math.pow(2, -10*--t)+2)+b;
};
//指数缓入缓出函数
Math.easeInCirc = function(t, b, c, d) {
return c*(1-Math.sqrt(1-(t /= d)*t))+b;
};
//圆形缓入函数
Math.easeOutCirc = function(t, b, c, d) {
return c*Math.sqrt(1-(t=t/d-1)*t)+b;
};
//圆形缓出函数
Math.easeINOutCirc = function(t, b, c, d) {
if ((t /= d/2)<1) {
return c/2*(1-Math.sqrt(1-t*t))+b;
}
return c/2*(Math.sqrt(1-(t -= 2)*t)+1)+b;
};
//圆形缓入缓出函数
  以上是“<b>Flash游戏开辟常用函数代</b>[Flash设计]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .