2012-11-10 37 views
0

我正在製作一個教程,在YouTube上學習一些動作腳本3.我有我的成品,它基本上是symbol,被稱爲ball,它有一個名爲_ball的實例。這裏顯示了教程中的成品。Actionscript 3 - 旋轉符號?

Tutorial Video - Youtube

所以基本上我要實現的是旋轉球,這取決於哪種方式球動我將如何去實現呢?我是新來的行動腳本,所以一些代碼示例將不勝感激或深入的解釋。

因爲任何人都想要一份代碼 - 就是這樣 - 我已經在某些方面對它進行了編輯,但效果不是很好。

package 
{ 
    import flash.display.MovieClip 
    import flash.text.TextField 
    import flash.events.Event 
    import flash.events.MouseEvent 

    public class DocumentMain extends MovieClip 
    { 
     public const GRAVITY:Number = 2; // Declaring a const variable known as gravity 
     public const BOUNCE:Number = 0.8; 
     public const HIT:Number = 15; 

     public var _bounces:TextField; 
     public var _highscore:TextField; 
     public var _ball:Ball; 

     private var _vx:Number; // Declaring a variable known as _vx 
     private var _vy:Number; // Declaring a variable knwon as _vy 

     public function DocumentMain(): void 
     { 
      _vx = Math.random(); // Initalising _vx 
      _vy = Math.random(); // Initalising _vy 

      _ball.buttonMode = true; 

      addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
      addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
     } 

     private function enterFrameHandler (e:Event):void 
     { 
      // Gravitate the Ball 
      _vy += GRAVITY; // The ball is effected by gravity each frame 

      // Move The Ball 
      _ball.x += _vx; 
      _ball.y += _vy; 

      // Check Stage Boundaries For Collisions 
      checkBoundaryCollision(); 
     } 

     private function mouseDownHandler (e:MouseEvent):void 
     { 
      // Hit the ball if it has been clicked 
      if (e.target == _ball) 
      { 
       hit(e.target.mouseX, e.target.mouseY); 
      } 
     } 

     private function checkBoundaryCollision():void 
     { 
      var left:Number; 
      var right:Number; 
      var bottom:Number; 
      var top:Number; 

      left = _ball.x - (_ball.width/2); 
      right = _ball.x + (_ball.width/2); 
      bottom = _ball.y + (_ball.height/2); 
      top = _ball.y - (_ball.height/2); 

      if (left < 0 && _vx < 0) 
      { 
       _ball.x = (_ball.width/2) 
       _vx *= -1; 
      } 
      else if (right > stage.stageWidth && _vx > 0) 
      { 
       _ball.x = stage.stageWidth - (_ball.width/2) 
       _vx *= -1; 
      } 

      if (top <= 42.70 && _vy < 0) 
      { 
       _ball.y = (_ball.height/2) 
       _vy *= -1; 
      } 
      else if (bottom > stage.stageHeight && _vy > 0) 
      { 
       _ball.y = stage.stageHeight - (_ball.height/2) 
       _vy *= -BOUNCE; 
       _vx *= BOUNCE; 

       if (Number(_bounces.text) > Number(_highscore.text)) 
       { 
        _highscore.text = _bounces.text; 
       } 

       _bounces.text = "0"; 
      } 
     } 

     private function hit(hitX:Number, hitY:Number):void 
     { 
      // increment bounces 
      _bounces.text = String(Number(_bounces.text) + 1); 
      // Adjust vertical velocity 
      if (_vy > 0) 
      { 
       _vy *= -BOUNCE/2; 
      } 

      _vy -= HIT; 

      //adjust horizontal veloity 
      if (_vx * hitX > 0) 
      { 
       _vx *= -BOUNCE; 
      } 

      _vx -= (hitX/_ball.width * HIT); 
     } 
    }  
} 

回答

0

我現在不能測試此權利,但它應該是爲更新enterFrameHandler由基值乘以球的X速度旋轉每一幀的球一樣簡單:

public const ROTATION:Number = 1; 

    private function enterFrameHandler (e:Event):void 
    { 
     // Gravitate the Ball 
     _vy += GRAVITY; // The ball is effected by gravity each frame 

     // Move The Ball 
     _ball.x += _vx; 
     _ball.y += _vy; 

     // Rotate the ball each frame at a speed and direction 
     // related to the ball's current x velocity. A negative 
     // x velocity will result in a negative rotation. 
     _ball.rotation += ROTATION * _vx; 

     // Check Stage Boundaries For Collisions 
     checkBoundaryCollision(); 
    } 
+0

這就是訣竅!謝謝 :)! – Andy