para ejemplos descargar cs6 codigos animate acciones actionscript-3 flash actionscript cs3

actionscript-3 - ejemplos - flash cs3



Al pasar el DPAD a veces el personaje se mueve infinitamente en una dirección (1)

Antes de comenzar a depurar comportamientos, debe comprender qué es el enfoque algorítmico. Tiene 4 piezas de código similares que difieren en detalles menores y hacen que todo el guión sea demasiado extenso, ilegible y difícil de administrar. Aquí está la pauta:

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; }

Como puede ver, la belleza del enfoque algorítmico es que necesita manejar de manera diferente solo las cosas que deben manejarse de forma diferente, en el caso de ejemplo (que es bastante similar a lo que desea crear) es el bloque para establecer el movimiento dirección. El resto del código es idéntico para todos los botones y es por eso que todo el guión es corto y legible, fácil de entender y manejable.

Tengo un DPAD en mi juego cuando el jugador se agacha, digamos el DPAD izquierdo, si mueve su toque hacia el DPAD hacia arriba y vamos, el jugador continúa yendo en la dirección izquierda.

También funciona si mantienes la dirección a, digamos que Up continúa presionando pero que se mueve fuera del UPAD, a veces puedes continuar yendo en esa dirección.

Lo que he intentado evitar esto:

  • En clics de dirección, active las comprobaciones de si está en movimiento o si ya va en una dirección diferente
  • Establecer colisiones para forzar que estas variables se vuelvan falsas; alias, tocas algo, inMotion = false, etc.

Eso es todo lo que puedo pensar sobre cómo solucionar esto.

Además, uso muchas variables para mis cheques, ¿es mejor cambiar mis funciones para devolver booleanos, o es así? Sólo curioso.

Game Class

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); } } }

Clase de movimiento

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; } } } }