2017-02-23 15 views
1

プレイヤーが押さえているときにDPADを持っています。左のDPADと言いましょう。自分のタッチを上のDPADに移動して行こうとすると、左方向。DPADをホバリングすると、ある方向に無限にキャラクターが移動することがあります

また、方向を保持している場合は上方向に移動し、下方向に移動する場合は上方向に移動します。方向をクリックすると、あなたの運動中かどうかのトリガーチェック、または すでにこれらの変数を強制的に衝突を設定

  • 別の方向に行くには

    • :私はこれを防ぐために試してみた何

      偽になる。別名 あなたが何かを押すと、inMotion = falseなど

    これは私がこれを解決する方法について考えることができるすべてです。

    また、私はチェックのために多くの変数を使用していますが、ブール値を返すように関数を変更する方が良いですか?ちょっと興味があるんだけど。

    ゲームクラス

    package 
    { 
        import flash.display.MovieClip; 
        import flash.utils.Timer; 
        import flash.events.TimerEvent; 
    
        public class Game extends MovieClip 
        { 
         public var area1:Boolean = true; 
         public var area2:Boolean = false; 
         public var area3:Boolean = false; 
    
         public var player1:Boolean = true; 
         public var player2:Boolean = false; 
    
         public var upWalkspeed:Number = -5; 
         public var downWalkspeed:Number = 5; 
         public var leftWalkspeed:Number = -5; 
         public var rightWalkspeed:Number = 5; 
    
         public var inMotion:Boolean = false; 
         public var goingUp:Boolean = false; 
         public var goingDown:Boolean = false; 
         public var goingLeft:Boolean = false; 
         public var goingRight:Boolean = false; 
    
         public var playerPosKeeper_mc:MovieClip = new mc_PlayerPosKeeper(); 
    
         public var up_dpad:MovieClip = new dpad_Up(); 
         public var down_dpad:MovieClip = new dpad_Down(); 
         public var left_dpad:MovieClip = new dpad_Left(); 
         public var right_dpad:MovieClip = new dpad_Right(); 
         public var menu_dpad:MovieClip = new dpad_Menu(); 
         public var run_dpad:MovieClip = new dpad_Menu(); 
    
         public var barrierRoof1_game:MovieClip = new game_BarrierRoof(); 
         public var barrierRoof2_game:MovieClip = new game_BarrierRoof(); 
         public var barrierSide1_game:MovieClip = new game_BarrierSide(); 
         public var barrierSide2_game:MovieClip = new game_BarrierSide(); 
    
         public var StageCollisions:Array = new Array(barrierRoof1_game, barrierRoof2_game, barrierSide1_game, barrierSide2_game); 
         // fix MC goes after not before ||| public var player1States:Array = new Array(mc_P1D1,mc_P1D2,"mc_P1L1","mc_P1L2","mc_P1R1","mc_P1R2","mc_P1U1","mc_P1U2"); 
    
         public function Game() 
         { 
          trace("SUCCESS | Constructed Game Class"); 
    
          var aMove:Movement = new Movement(this); 
          addChild(aMove); 
    
         } 
        } 
    } 
    

    ムーブメントクラス

    package 
    { 
        import flash.display.Stage; 
        import flash.display.MovieClip; 
        import flash.events.Event; 
        import flash.events.TouchEvent; 
        import flash.net.dns.AAAARecord; 
        import flash.ui.Multitouch; 
        import flash.ui.MultitouchInputMode; 
    
    
        public class Movement extends MovieClip 
        { 
         public function Movement(main:Game) 
         { 
          trace("SUCCESS | Constructed Movement Class"); 
    
          addChild(main.playerPosKeeper_mc); 
          main.playerPosKeeper_mc.x = 384; 
          main.playerPosKeeper_mc.y = 46; 
    
          addChild(main.up_dpad); 
          main.up_dpad.x = 55; 
          main.up_dpad.y = 336; 
    
          addChild(main.down_dpad); 
          main.down_dpad.x = 57; 
          main.down_dpad.y = 432; 
    
          addChild(main.left_dpad); 
          main.left_dpad.x = 19; 
          main.left_dpad.y = 372; 
    
          addChild(main.right_dpad); 
          main.right_dpad.x = 118; 
          main.right_dpad.y = 372; 
    
          addChild(main.menu_dpad); 
          main.menu_dpad.x = 61; 
          main.menu_dpad.y = 377; 
    
          addChild(main.run_dpad); 
          main.run_dpad.x = 684; 
          main.run_dpad.y = 369; 
    
          addChild(main.barrierRoof1_game); 
          main.barrierRoof1_game.x = 0; 
          main.barrierRoof1_game.y = 0; 
    
          addChild(main.barrierRoof2_game); 
          main.barrierRoof2_game.x = 0; 
          main.barrierRoof2_game.y = 470; 
    
          addChild(main.barrierSide1_game); 
          main.barrierSide1_game.x = 0; 
          main.barrierSide1_game.y = 0; 
    
          addChild(main.barrierSide2_game); 
          main.barrierSide2_game.x = 790; 
          main.barrierSide2_game.y = 0; 
    
          Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; 
    
          main.up_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, upBeginInput); 
          main.down_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, downBeginInput); 
          main.left_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, leftBeginInput); 
          main.right_dpad.addEventListener(TouchEvent.TOUCH_BEGIN, rightBeginInput); 
    
          // Maybe add diagnol direction buttons in the future?; 
    
    
          // !! NOTE !! 
          // Use some sort of value, maybe a return or a variable to sync up animations 
          // to if the player is moving, in the future 
    
          // Movement Directions 
    
          // Start of UP Movement 
          function upBeginInput(e:TouchEvent):void 
          { 
           main.inMotion = true; 
           main.goingUp = true; 
           main.goingDown = false; 
           main.goingLeft = false; 
           main.goingRight = false; 
           main.up_dpad.addEventListener(TouchEvent.TOUCH_END, upEndInput); 
           addEventListener(Event.ENTER_FRAME,sendUpMovement); 
          } 
          function upEndInput(e:TouchEvent):void 
          { 
           main.inMotion = false; 
           main.goingUp = false; 
           main.up_dpad.removeEventListener(TouchEvent.TOUCH_END, upEndInput); 
           removeEventListener(Event.ENTER_FRAME,sendUpMovement); 
          } 
          function sendUpMovement():void 
          { 
           if (main.inMotion == true && main.goingUp == true && main.goingDown == false && main.goingLeft == false && main.goingRight == false) 
           { 
            movePlayer(0, main.upWalkspeed); 
           } 
           else 
           { 
           } 
          } 
          // End of UP Movement 
    
    
    
          // Start of DOWN Movement 
          function downBeginInput(e:TouchEvent):void 
          { 
           main.inMotion = true; 
           main.goingUp = false; 
           main.goingDown = true; 
           main.goingLeft = false; 
           main.goingRight = false; 
           main.down_dpad.addEventListener(TouchEvent.TOUCH_END, downEndInput); 
           addEventListener(Event.ENTER_FRAME,sendDownMovement); 
          } 
          function downEndInput(e:TouchEvent):void 
          { 
           main.inMotion = false; 
           main.goingDown = false; 
           main.down_dpad.removeEventListener(TouchEvent.TOUCH_END, downEndInput); 
           removeEventListener(Event.ENTER_FRAME,sendDownMovement); 
          } 
          function sendDownMovement():void 
          { 
           if (main.inMotion == true && main.goingUp == false && main.goingDown == true && main.goingLeft == false && main.goingRight == false) 
           { 
            movePlayer(0, main.downWalkspeed); 
           } 
           else 
           { 
           } 
          } 
          // End of DOWN Movement 
    
    
    
          // Start of LEFT Movement 
          function leftBeginInput(e:TouchEvent):void 
          { 
           main.inMotion = true; 
           main.goingUp = false; 
           main.goingDown = false; 
           main.goingLeft = true; 
           main.goingRight = false; 
           main.left_dpad.addEventListener(TouchEvent.TOUCH_END, leftEndInput); 
           addEventListener(Event.ENTER_FRAME,sendLeftMovement); 
          } 
          function leftEndInput(e:TouchEvent):void 
          { 
           main.inMotion = false; 
           main.goingLeft = false; 
           main.left_dpad.removeEventListener(TouchEvent.TOUCH_END, leftEndInput); 
           removeEventListener(Event.ENTER_FRAME,sendLeftMovement); 
          } 
          function sendLeftMovement():void 
          { 
           if (main.inMotion == true && main.goingUp == false && main.goingDown == false && main.goingLeft == true && main.goingRight == false) 
           { 
            movePlayer(main.leftWalkspeed, 0); 
           } 
           else 
           { 
           } 
          } 
          // End of LEFT Movement 
    
    
    
          // Start of RIGHT Movement 
          function rightBeginInput(e:TouchEvent):void 
          { 
           main.inMotion = true; 
           main.goingUp = false; 
           main.goingDown = false; 
           main.goingLeft = false; 
           main.goingRight = true; 
           main.right_dpad.addEventListener(TouchEvent.TOUCH_END, rightEndInput); 
           addEventListener(Event.ENTER_FRAME,sendRightMovement); 
          } 
          function rightEndInput(e:TouchEvent):void 
          { 
           main.inMotion = false; 
           main.goingRight = false; 
           main.right_dpad.removeEventListener(TouchEvent.TOUCH_END, rightEndInput); 
           removeEventListener(Event.ENTER_FRAME,sendRightMovement); 
          } 
          function sendRightMovement():void 
          { 
           if (main.inMotion == true && main.goingUp == false && main.goingDown == false && main.goingLeft == false && main.goingRight == true) 
           { 
            movePlayer(main.rightWalkspeed, 0); 
           } 
           else 
           { 
           } 
          } 
          // End of RIGHT Movement 
    
    
          function movePlayer(movementX:Number, movementY:Number):void 
          { 
           var originalX:Number = main.playerPosKeeper_mc.x; 
           var originalY:Number = main.playerPosKeeper_mc.y; 
           main.playerPosKeeper_mc.x += movementX; 
           if (checkCollision()) 
           { 
            main.playerPosKeeper_mc.x = originalX; 
           } 
           main.playerPosKeeper_mc.y += movementY; 
           if (checkCollision()) 
           { 
            main.playerPosKeeper_mc.y = originalY; 
           } 
          } 
    
          function checkCollision():Boolean 
          { 
           for each (var StageCollisions:MovieClip in main.StageCollisions) 
           { 
            if (main.playerPosKeeper_mc.hitTestObject(StageCollisions)) 
            { 
             return true; 
             main.inMotion = false; 
             main.goingUp = false; 
             main.goingDown = false; 
             main.goingLeft = false; 
             main.goingRight = false; 
            } 
           } 
           return false; 
          } 
         } 
        } 
    } 
    
  • 答えて

    4

    あなたも、デバッグ行動を開始する前に、アルゴリズム的アプローチが何であるかを理解する必要があります。マイナーな細部が異なる4つの類似したコードがあり、スクリプト全体が長くなりすぎて読めなくなり、管理が難しくなります。ここではガイドラインがあります:

    var isMoving:Boolean; 
    var Direction:Point = new Point; 
    var Buttons:Array = [Up, Down, Left, Right]; 
    
    // Subscribe all buttons for the same handlers and behaviors. 
    for each (var aButton:InteractiveObject in Buttons) 
    { 
        aButton.addEventListener(MouseEvent.MOUSE_DOWN, onDown); 
        aButton.addEventListener(MouseEvent.MOUSE_OUT, onUp); 
        aButton.addEventListener(MouseEvent.MOUSE_UP, onUp); 
    } 
    
    function onDown(e:MouseEvent):void 
    { 
        // Figure which button was pressed. 
        switch (e.currentTarget) 
        { 
         case Up: 
          Direction.x = 0; 
          Direction.y = -1; 
          break; 
    
         case Down: 
          Direction.x = 0; 
          Direction.y = 1; 
          break; 
    
         case Left: 
          Direction.x = -1; 
          Direction.y = 0; 
          break; 
    
         case Up: 
          Direction.x = 1; 
          Direction.y = 0; 
          break; 
        } 
    
        // Now start moving Hero into the Direction. 
        if (!isMoving) 
        { 
         isMoving = true; 
         addEventListener(Event.ENTER_FRAME, onFrame); 
        } 
    } 
    
    function onFrame(e:Event):void 
    { 
        Hero.x += Direction.x; 
        Hero.y += Direction.y; 
    } 
    
    function onUp(e:MouseEvent):void 
    { 
        // If any of buttons is released or mouse out, stop moving Hero. 
        removeEventListener(Event.ENTER_FRAME, onFrame); 
    
        Direction.x = 0; 
        Direction.y = 0; 
    
        isMoving = false; 
    } 
    

    あなたが見ることができるように、アルゴリズム的アプローチの美しさはかなり類似している(例の場合には、あなたが必要は異なる方法で処理することを違ったのみ物事を処理する必要があるということですあなたが作成したいものに)移動方向を設定するのはブロックです。残りのコードはすべてのボタンで同じです。そのため、スクリプト全体が短くて読みやすく、理解しやすく、管理しやすいものです。

    関連する問題