2015-08-31 89 views
0

大家好再次:)我有一個邊界實際上有關創造邊界的問題。在我的舞臺上,我得到了2個mc的名字:分配器和拔器。分配器創建粒子和拉拔器正在拉動它們。我只需要像路徑一樣設置邊界。 enter image description here
但我不能這樣要求你的幫助:)創造邊界

BDW的代碼是從這個網站:http://www.freeactionscript.com/?s=puller&x=0&y=0

package 
{ 
    import flash.display.BitmapData; 
    import flash.display.Sprite; 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    public class Main extends Sprite 
    { 
     // you can play around with these 
     private var particlesTotal:Number = 50; 
     private var particleSpeed:Number = 10; 
     private var particleFadeoutSpeed:Number = .0175; 

     // don't change these 
     private var particlesCurrent:Number = 0; 
     private var particlesArray:Array;  
     private var radians:Number; 
     private var _sayac:uint; 

     public function Main():void 
     { 
      init(); 
     } 

     private function init():void 
     { 
      radians = 180/Math.PI; 
      particlesArray = []; 

      addEventListener(Event.ENTER_FRAME, onEnterFrameLoop); 

      dispenser_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown); 
      puller_mc.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown); 

      dispenser_mc.addEventListener(MouseEvent.MOUSE_UP, onThingUp); 
      puller_mc.addEventListener(MouseEvent.MOUSE_UP, onThingUp); 

     } 

     private function onThingDown(event:MouseEvent):void 
     { 
      event.currentTarget.startDrag(); 
     } 
     private function onThingUp(event:MouseEvent):void 
     { 
      event.currentTarget.stopDrag(); 
     } 

     private function createParticle(target1:MovieClip, target2:MovieClip):void 
     {  
      if(particlesTotal <= particlesCurrent) 
      { 
       return; 
      } 
      particlesCurrent++; 

      var tempParticle:Particle = new Particle(); 

      tempParticle.x = (target1.x - target1.width/2) + (Math.random() * target1.width); 
      tempParticle.y = (target1.y - target1.height/2) + (Math.random() * target1.height); 
      tempParticle.rotation = Math.random()*360; 

      tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x); 
      tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians/particleSpeed; 
      tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians/particleSpeed; 
      tempParticle.mass = tempParticle.width/2 + tempParticle.height/2; 

      particlesArray.push(tempParticle); 

      addChild(tempParticle); 

     } 

     private function updateParticle():void 
     { 
      for (var i = 0; i < particlesArray.length; i++) 
      { 
       var tempParticle:MovieClip = particlesArray[i]; 

       tempParticle.x -= tempParticle.xSpeed; 
       tempParticle.y -= tempParticle.ySpeed; 
       tempParticle.alpha -= particleFadeoutSpeed; 
       // I know i can set boundries here but idk how to so :) 

       if(tempParticle.hitTestObject(puller_mc)) 
       { 
        destroyParticle(tempParticle); 
        _sayac++; 
        trace(_sayac); 
       } 
       else if (tempParticle.alpha <= 0) 
       { 
        destroyParticle(tempParticle); 
       } 
       else if (tempParticle.x < 0) 
       { 
        destroyParticle(tempParticle); 
       } 
       else if (tempParticle.x > stage.stageWidth) 
       { 
        destroyParticle(tempParticle); 
       } 
       else if (tempParticle.y < 0) 
       { 
        destroyParticle(tempParticle); 
       } 
       else if (tempParticle.y > stage.stageHeight) 
       { 
        destroyParticle(tempParticle); 
       } 
      } 
     } 

     private function destroyParticle(particle:MovieClip):void 
     { 
      for (var i = 0; i < particlesArray.length; i++) 
      { 
       var tempParticle:MovieClip = particlesArray[i]; 
       if (tempParticle == particle) 
       { 
        particlesCurrent--; 
        particlesArray.splice(i,1); 
        removeChild(tempParticle); 
       } 
      } 
     } 

     private function onEnterFrameLoop(event:Event):void 
     { 
      createParticle(dispenser_mc, puller_mc); 
      updateParticle(); 
     } 

    } 
} 

回答

0

居然有這個代碼沒有邊界。讓我來解釋一下這個代碼的主要部分爲您提供:

這是最重要的部分:

 tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x); 
     tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians/particleSpeed; 
     tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians/particleSpeed; 

rot是達到車伕的角度。
xSpeed是粒子應該到達每個幀的正確位置以達到牽引器的程度。
ySpeed是顆粒應該落到每一幀以達到牽引器的程度。

updateParticle函數中,按照xSpeedySpeed的順序,該部分將移動。

所以正確的xSpeedYSpeed會導致粒子直行。不是邊界!

我h☻p e我解釋得很好,
我希望這有助於!

+0

是的你的描述是全部爲真。但我想要的是我想創建這些代碼的邊界:) –

+0

編程沒有邊界! – HITMAN

+0

在這一個是沒有任何。但是你可以用舞臺作爲邊界嗎?我想用一些MC來做這個。 –