2011年7月4日 星期一

Flash Actionscript Physics Engine(APE) III about Roulette

基於上一個拆解  APE的DEMO

右上角的小搖桿,把其想法做了一個   輪子(Wheel)及在上面釘一些點(pin)

並希望這些點可碰撞到一個針   讓這個針可在  被點碰撞到的情況做的小擺動

我們先做一個    由於這些點及輪子會一起連動所以使用了   Composite來把這些解構結合

程式如下:

  1: package comp
  2: {
  3:     import org.cove.ape.*;
  4: 
  5:     import ui.*;
  6: 
  7:     public class Wheel extends Composite
  8:     {
  9:         public function Wheel()
 10:         {
 11:             super();
 12: 
 13:             var wheel:WheelParticle=new WheelParticle(100, 100, 20, true);
 14:             var myWheel:ui.MyWheel;
 15:             wheel.setDisplay(myWheel=new ui.MyWheel());
 16:             wheel.alwaysRepaint=true;
 17:             //wheel.angularVelocity=0.0001;
 18:             //圓轉 結合的釘子不會動
 19: 
 20:             this.addParticle(wheel);
 21: 
 22:             var rCount:uint=0;
 23:             var number:uint=10;
 24:             var r:uint=360 / number;
 25:             for (var i:uint=0; i < number; i++)
 26:             {
 27:                 var X2:uint=Math.cos(rCount / 180 * Math.PI) * 80 + 100;
 28:                 var Y2:uint=Math.sin(rCount / 180 * Math.PI) * 80 + 100;
 29: 
 30:                 rCount+=r;
 31: 
 32:                 var pin:CircleParticle=new CircleParticle(X2, Y2, 2, true);
 33:                 pin.setDisplay(new ui.Pin());
 34:                 pin.alwaysRepaint=true;
 35:                 this.addParticle(pin);
 36:             }
 37:         }
 38:     }
 39: }
 40: 


Line 13~16:使用WheelParticle製作一個 輪子並由flash中指定一個  MovieClip給這個Particle



Line 22~36:在輪子上增加點(pin),目前增加20個點,使用三角涵數環繞擺放,並將flash中的MovieClip指定給各個點



以上即完成我們要的輪子



接著我們要在主場景上,放上一個針,這個針會由一個圓粒子去挓住這個針,讓它不會被地心引力帶著往下掉



  1: //needle start
  2:             var point1:CircleParticle=new CircleParticle(100, 180, 2);
  3:             
  4:             group2.addParticle(point1);
  5: 
  6:             var point2:CircleParticle=new CircleParticle(100, 220, 1, true);
  7:             
  8:             group2.addParticle(point2);
  9: 
 10:             var line:SpringConstraint=new SpringConstraint(point1, point2, 1, false, 0);
 11:             line.setDisplay(new ui.MyNeedle());
 12:             group2.addConstraint(line);
 13:             //needle end
 14: 
 15: 
 16: 
 17:             //Drag line start
 18:             var point_left:CircleParticle=new CircleParticle(100, 165, 0, true);
 19:             
 20:             group2.addParticle(point_left);
 21: 
 22:             var line_left:SpringConstraint=new SpringConstraint(point1, point_left, 0.01, true, 1);
 23:             
 24:             line_left.restLength=2;
 25:             group2.addConstraint(line_left);
 26:             //Drag line end

 



line 2~12為主要的針的解構



line 18~25為挓住針的解構(座標會在針的正上方)



  1: package
  2: {
  3:     import comp.Wheel;
  4: 
  5:     import flash.display.Sprite;
  6:     import flash.events.Event;
  7: 
  8:     import org.cove.ape.*;
  9: 
 10:     import ui.*;
 11: 
 12:     public class MyWheel extends Sprite
 13:     {
 14:         private var wheel:comp.Wheel;
 15: 
 16:         public function MyWheel()
 17:         {
 18:             super();
 19:             this.addEventListener(Event.ENTER_FRAME, run);
 20:             APEngine.init(1 / 4);
 21:             APEngine.container=this;
 22:             APEngine.addForce(new org.cove.ape.Vector(0, 2));
 23: 
 24:             var groupWheel:Group=new Group();
 25:             groupWheel.collideInternal=true;
 26: 
 27:             wheel=new comp.Wheel();
 28: 
 29:             groupWheel.addComposite(wheel);
 30: 
 31: 
 32:             var groupNeedle:Group=new Group();
 33:             groupNeedle.collideInternal=true;
 34: 
 35: 
 36:             //needle start
 37:             var point1:CircleParticle=new CircleParticle(100, 180, 2);
 38:             point1.visible=false;
 39:             groupNeedle.addParticle(point1);
 40: 
 41:             var point2:CircleParticle=new CircleParticle(100, 220, 1, true);
 42:             point2.visible=false;
 43:             groupNeedle.addParticle(point2);
 44: 
 45:             var line:SpringConstraint=new SpringConstraint(point1, point2, 1, false, 0);
 46:             line.setDisplay(new ui.MyNeedle());
 47:             groupNeedle.addConstraint(line);
 48:             //needle end
 49: 
 50: 
 51: 
 52:             //Drag line start
 53:             var point_left:CircleParticle=new CircleParticle(100, 165, 0, true);
 54:             point_left.visible=false;
 55:             groupNeedle.addParticle(point_left);
 56: 
 57:             var line_left:SpringConstraint=new SpringConstraint(point1, point_left, 0.01, true, 1);
 58:             line_left.visible=false;
 59:             line_left.restLength=2;
 60:             groupNeedle.addConstraint(line_left);
 61:             //Drag line end
 62: 
 63:             APEngine.addGroup(groupWheel);
 64:             APEngine.addGroup(groupNeedle);
 65: 
 66:             groupWheel.addCollidable(groupNeedle);
 67:         }
 68: 
 69:         private function run(event:Event):void
 70:         {
 71:             APEngine.step();
 72:             APEngine.paint();
 73:             wheel.rotateByRadian(0.02, new org.cove.ape.Vector(100, 100));
 74:         }
 75:     }
 76: }
 77: 


line 24~29 接下來 把我們做的輪子放上 (groupWheel)



line 32~60 針及拖曳線拼起來(groupNeedle)



line 60   這二個不同的Group知道彼此的存在做碰撞(Collision  detection)



line 73  不停的轉動輪子



P.S Collision detection  only apply for praticle


沒有留言:

張貼留言