XYZBLOG

WELCOME
« »
SuMoTuWeThFrSa
1234
567891011
12131415161718
19202122232425
2627282930
新近评论
新近留言
友情链接
  1. 友情链接 (1)
统计信息
  • 日志: 28
  • 评论: 2
  • 留言: 0
  • 链接: 1
  • 引用: 0
  • 访问: 27242
相册 普通 列表 管理

非线性几率

原创代码 | 2009-05-10 16:13
var a;
var 红几率 = 1/3;
var 黄几率 = 1/6;
var 蓝几率;
for (var i=1; i<=100; i++) {
  var ran=Math.random();
  if (ran<红几率) {
    a="红";
  } else if (ran>=红几率&&ran<黄几率+红几率) {
    a="黄";
  } else {
    a="蓝";
  }
  trace(a);
}
虽然17曾经建议我不要研究这种逆天的东西,但是这个东西的诱惑很大,所以还是研究了一下,在网上似乎有英文版的,但是语焉不详,而且很多人懒得看英文,所以自己写了一下,经ie,遨游,firefox测试有效,其他浏览器还请各位自行测试:
index.fla:
import flash.text.TextField;
var _txt:TextField=new TextField;
_txt.text="请用右键点我"
addChild(_txt);
var i=0;
function displayMessage() {
  i++;
  _txt.text=i+"";
}
ExternalInterface.addCallback("showMessage", displayMessage);

***************************很丑的分隔线*****************************
test.htm:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>index</title>
<script>
//非ie----------------------------------
function mouseEvent_ns(e) {
  //alert(e.button)
  //firefox测得0是左键,1是中键,2是右键
  if (e.button ==2) {
    if (e) {
      //屏蔽弹出
      if (e.stopPropagation) {
        e.stopPropagation();
      }
      if (e.preventDefault) {
        e.preventDefault();
      }
      if (e.preventCapture) {
        e.preventCapture();
      }
      if (e.preventBubble) {
        e.preventBubble();
      }
    }
    //调用as函数
    thisMovie("index").showMessage();
  }
}
function RightMouseUp_ns(e) {
  mouseEvent_ns(e);
  return false;
}
function downRightClick_ns(e) {
  mouseEvent_ns(e);
  return false;
}
//ie----------------------------------
function mouseEvent_ie() {
  //alert(event.button)
  //1是左键,4是中键,2是右键,但不知为什么遨游里按下没反应,弹起时先是得到0再得到2,遗憾...
  if (event.button != 1&&event.button != 4) {  
    //调用as函数
    thisMovie("index").showMessage();    
    //屏蔽弹出
    thisMovie("index").openRightClick();
    parent.frames.location.replace('javascript: parent.falseframe');
  }
}
function RightMouseUp_ie() {
  mouseEvent_ie()
  return false;
}
function RightMouseDown_ie(e) {  
  mouseEvent_ie()
  return false;
}
//得到播放器对象---------------------------------------
function thisMovie(movieName) {
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}
//触发鼠标事件-----------------------------------------
this.ns = (navigator.appName == 'Netscape');
if (this.ns) {
  document.captureEvents(Event.MOUSEDOWN);
  document.addEventListener("mousedown", downRightClick_ns, true);
  document.captureEvents(Event.MOUSEUP);
  document.addEventListener("mouseup", RightMouseUp_ns, true);
} else {
  document.onmousedown = RightMouseDown_ie;
  document.onmouseup = RightMouseUp_ie;
}

</script>
</head>
<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginwidth="0">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="900" height="500" id="index" align="middle">
   <param name="allowScriptAccess" value="sameDomain" />
   <param name="movie" value="index.swf" />
   <param name="menu" value="false" />
   <param name="quality" value="high" />
   <param name="wmode" value="opaque" />
   <param name="bgcolor" value="#ffffff" />
   <embed src="index.swf" menu="false" quality="high" wmode="opaque" bgcolor="#ffffff" width="900" height="500" name="index" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />  
</object>
</body>
</html>
***************************很丑的分隔线*****************************
细心的人还会发现ie里鼠标点快了会有检测丢失的情况,又一遗憾,不过总归可以稍微利用下了吧
测试地址:
090302_test.htm

爬山代码

原创代码 | 2009-02-19 13:50
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
this.createEmptyMovieClip("man_mc", _root.getNextHighestDepth());
man_mc.beginFill(0xFF0000, 100);
man_mc.moveTo(0, 0);
man_mc.lineTo(-20, -80);
man_mc.lineTo(20, -80);
man_mc.endFill();
man_mc._x = 200;
this.createEmptyMovieClip("action_mc", _root.getNextHighestDepth());
action_mc._y = 500;
//山的初始位置
var fillColor = 0x9999FF;
//山的颜色
var totalWidth = 1500;
var x0 = 0;
var y0 = action_mc._y;
var dy = 20;
var y_arr:Array = [];
var rate = 200;
var timeCount = 0;
var timeRate = 20;
//控制多少桢绘画出新的山峰(谷),必须是能被rate整除的数
var scaley = 2;
//鼠标差值和产生的山峰(谷)的比例,值越大波浪越小
for (var i = 0; i<=Math.floor(totalWidth/rate+1); i++) {
  y_arr.push(Math.random()*(Stage.height-action_mc._y)/scaley);
}
//action_mc.lineStyle(5, 0xFF00FF, 100);
drawPic = function () {
  action_mc.clear();
  action_mc.beginFill(fillColor, 100);
  action_mc.moveTo(0, (y_arr[0]+y_arr[1])/2+dy/2);
  for (var i = 1; i<=y_arr.length-1; i += 1) {
    action_mc.curveTo(i*rate-rate/2, y_arr[i]-i*dy+dy, i*rate, (y_arr[i]+y_arr[i+1])/2-i*dy+dy/2);
  }
  action_mc.lineTo((y_arr.length-1)*rate, 400);
  action_mc.lineTo(0, 400);
  action_mc.endFill();
};
drawPic();
_root.onEnterFrame = function() {
  action_mc._x -= rate/timeRate;
  action_mc._y += dy/timeRate;
  timeCount++;
  if (timeCount%timeRate == 0) {
    y_arr.shift();
    y_arr.push(action_mc._ymouse/scaley);
    if (timeCount%(timeRate*2) == 0) {
    }
    action_mc._x = x0;
    action_mc._y = y0;
  }
  drawPic();
  var manSpeed = rate/(timeRate*2);
  var manDx = _root._xmouse-man_mc._x;
  if (Math.abs(manDx)>manSpeed) {
    man_mc._x += (manDx>0) ? manSpeed : (-manSpeed);
  }
  man_mc._y = getY(man_mc._x);
};
var getY = function (x):Number {
  var bb:BitmapData = new BitmapData(action_mc._width, Stage.height, true, 0);
  bb.draw(action_mc, new Matrix(1, 0, 0, 1, action_mc._x, action_mc._y));
  var y = 0;
  while (bb.getPixel(x, y) == !fillColor) {
    y++;
  }
  bb.dispose();
  return (y);
};
不知道有没有更高效的方法,欢迎拍砖,测试:

跟随鼠标转动

原创代码 | 2008-05-06 13:02
l_mc.onEnterFrame = function() {
  dx = _xmouse-l_mc._x;
  dy = _ymouse-l_mc._y;
  if (dy<0) {
    l_mc._rotation = 180*Math.atan(-dx/dy)/Math.PI;
  } else if(dy>0){
    l_mc._rotation = 180*Math.atan(-dx/dy)/Math.PI+180;
  }else{
    l_mc._rotation = dx < 0?-90:90;
  }
};

as3 写法:
function getAngle(dx:Number, dy:Number):Number {
  var _angle:Number
  if (dy<0) {
    _angle = 180*Math.atan(-dx/dy)/Math.PI;
  } else if(dy>0){
    _angle = 180*Math.atan(-dx/dy)/Math.PI+180;
  }else{
    _angle =dx < 0?90:90;
  }
  return _angle;
}
var a:String = (""+(1000+random(999))).substr(1, 3);
trace(a);

汽车代码

原创代码 | 2007-12-14 17:24
今天无聊写的汽车引擎:
新建个实例名叫car_mc的影片剪辑,然后把下面代码贴在桢上就可以了:
var v0 = -5;
var decay = 0.95;
var vx = 0;
var vy = 0;
var angle = 0;
var changeAngle = 0;
_root.onEnterFrame = function() {
  v0 *= decay;
  changeAngle = 0;
  if (Key.isDown(Key.LEFT)) {
    changeAngle = -v0;
  }
  if (Key.isDown(Key.RIGHT)) {
    changeAngle = v0;
  }
  if (Key.isDown(Key.UP)) {
    v0 -= 1;
  }
  angle += changeAngle;
  vx = v0*Math.sin(angle*Math.PI/180);
  vy = v0*Math.cos(angle*Math.PI/180);
  car_mc._x += vx;
  car_mc._y += vy;
  car_mc._rotation = -angle;
};
点击浏览:

hitTest函数优化

原创代码 | 2007-10-17 09:50
isOnRoll = function (m:MovieClip) {
  if (m.hitTest(_root._xmouse, _root._ymouse)) {
    return (true);
  } else {
    return (false);
  }
};
trace(isOnRoll(xx_mc))