A-A+

flash N个小球碰撞效果制作

2015年09月17日 前端设计 暂无评论 阅读 10 views 次

flash 多个小球进行碰撞的效果感觉很好玩,当然碰撞的效果也是需要使用 as 代码来完成的,只是觉得好玩,暂时还没想到用在什么地方,新手可以拿 as 代码来测试一下,整个 as 代码如下:

  1. m._visible = 0;  
  2. function setMc(m_width,w,h,speed) {   
  3. stage_w = w-m_width;  
  4. stage_h = h-m_width;  
  5. for (i=1; i<11; i++) {  
  6. M = m.duplicateMovieClip("m"+i, i);  
  7. M_x = m_width/2+random(stage_w);  
  8. M_y = m_width/2+random(stage_h);  
  9. if (i == 1) {  
  10. M._x = M_x;  
  11. M._y = M_y;  
  12. }  
  13. if (i>1) {  
  14. for (j=1; j<i; j++) {  
  15. if (Math.sqrt((this["m"+j]._x-M_x)*(this["m"+j]._x-M_x)+(this["m"+j]._y-M  
  16. _y)*(this["m"+j]._y-M_y))<m_width) {  
  17. i--;  
  18. break;  
  19. }  
  20. }  
  21. if (j == i) {  
  22. M._x = M_x;  
  23. M._y = M_y;  
  24. }  
  25. }  
  26. //以上这些是做小球在一定范围内不重合的随机排列  
  27. M.rotation = random(2*Math.PI);  
  28. M.xspeed = speed;  
  29. M.yspeed = speed;  
  30. //初始每个小球的速度,角度  
  31. M.onEnterFrame = function() {  
  32. this._x += this.xspeed*Math.cos(this.rotation);  
  33. this._y += this.yspeed*Math.sin(this.rotation);  
  34. //--------------  
  35. for (i=1; i<11; i++) {  
  36. if (i == this._name.substr(1, this._name.length)) {  
  37. continue;  
  38. } else if (Math.sqrt((this._y-_root["m"+i]._y)*(this._y-_root["m"+i]._y)+(t  
  39. his._x-_root["m"+i]._x)*(this._x-_root["m"+i]._x))<=m_width) {  
  40. this.rotation = Math.atan2(this._y-_root["m"+i]._y, this._x-_root["m"+i]._x);  
  41. this.xspeed = speed;  
  42. this.yspeed = speed;  
  43. this._x += this.xspeed*Math.cos(this.rotation);  
  44. this._y += this.yspeed*Math.sin(this.rotation);  
  45. break;  
  46. }  
  47. //每个小球对场景内的其他所有小球进行接触判断,这里的接触用的是距离判断  
  48. }  
  49. //---------------  
  50. if (this._x<=m_width/2) {  
  51. this._x = m_width/2+.1;  
  52. this.xspeed *= -1;  
  53. } else if (this._x>=300-m_width/2) {  
  54. this._x = 300-m_width/2-.1;  
  55. this.xspeed *= -1;  
  56. } else if (this._y<=m_width/2) {  
  57. this._y = m_width/2+.1;  
  58. this.yspeed *= -1;  
  59. } else if (this._y>=300-m_width/2) {  
  60. this._y = 300-m_width/2-.1;   
  61. //这里-.1的处理是为了防止了边界出现问题,你可以删掉这句看看,原因还是自己动脑想   
  62. this.yspeed *= -1; }  
  63. };  
  64. //这里是对小球碰到边界的判断  
  65. }  
  66. }  
  67. setMc(m._width,300,300,5);//参数分别是小球的宽度  
标签:

给我留言