Tutorial: Control de personaje, parte 2 Año: 2011 V.: AS3
[e]
En mi empeño por optimizar el código del ejemplo anterior (Ver ejemplo anterior)
E rebajado el numero de líneas a 57, abusando un poco del condicional ternario y eliminando las funciones keyEvent y outKey.
También e evitado procesar las formulas trigonométricas innecesariamente, ahora solo se procesan al rotar el mapa.
Finalmente tenemos el mismo resultado con menos código y mas ligero para la cpu.
Haz click en el juego para obtener el foco.
Utiliza las teclas [W] [A] [S] [D] o las teclas de dirección [UP] [DOWN] [LEFT] [RIGHT]
This movie requires Flash Player 9
.
[download id=»25″ format=»2″]
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
var sR:int = 4;//speedRotationKeyBoard var xyVel:int = 10; var consPi:Number = Math.PI / 180; var rad:Number =(world.rotation-90)*consPi; var cos:Number = xyVel * Math.cos(rad); var sen:Number = xyVel * Math.sin(rad); var mapa:Object = world.mapa; var ka:Array = new Array(); var key:int; var pres:int = 3;//0 = tecla soltada. 1 = Tecla presionada. 3 = evitar bucle. stage.addEventListener(Event.ENTER_FRAME,onFrame); stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey); stage.addEventListener(KeyboardEvent.KEY_UP,onKey); function onKey(event:KeyboardEvent):void { var k:int = event.keyCode; event.type == "keyUp" ? [pres = 0,key == k ? key = 0:[]]:[key != k ? [pres = 1,key = k]:[]]; if (pres == 0 || pres == 1) { if (k == 65 || k == 68 || k == 37 || k == 39 || k == 87 || k == 40 || k == 38 || k == 83) { ka[k] = pres; if ((ka[87]||ka[38])&&ka[68]) {//Key[W] or Key[Up] and Key[D] then move UpRigth("ArribaDerecha"); goTo("move",-150); } else if (ka[68]&&(ka[40]||ka[83])) {//Key[D] and Key[S] or Key[Down] then move DownRigth("AbajoDerecha"); goTo("atras",150); } else if ((ka[40]||ka[83])&&ka[65]) {//Key[S] or Key[Down] and Key[A] then move DownLeft("AbajoIzquierda"); goTo("atras",-150); } else if (ka[65]&&(ka[87]||ka[38])) {//Key[A] and Key[W] or Key[Up] then move UpLeft("IzquierdaArriba"); goTo("move",150); } else if (ka[87] || ka[38]) {//Key[W] or Key[Up] then move Up("Arriba"); goTo("move",180); } else if (ka[68]) {//Key[D] then move Rigth("Derecha"); hero.scaleX = -1; goTo("desplzLateral",200); } else if (ka[83] || ka[40]) {//Key[S] or Key[Down] then move Down("Abajo"); goTo("atras",180); } else if (ka[65]) {//Key[A] then move Left("Izquierda"); goTo("desplzLateral",160); hero.scaleX = 1; } else if (ka[37]) {//Key[Left] then rotate Left("Rotar Izquierda"); goTo("relax",180); } else if (ka[39]) {//Key[Rigth] then rotate Rigth("Rotar Derecha"); goTo("relax",180); } else {//No Key? then Relax goTo("relax",180); } }//End if check Keys pres = 3;//bloqueamos mientras no se vuelva a pulsar o soltar un tecla } } function goTo(action,rotate):void { action == "relax" ? hero.gotoAndStop(action):hero.gotoAndPlay(action); hero.rotation = rotate; } function onFrame(e:Event):void { ka[37] ? [rad =((world.rotation +=sR)-90)*consPi,cos = xyVel * Math.cos(rad),sen = xyVel * Math.sin(rad)]:[ka[39] ? [rad =((world.rotation -=sR)-90)*consPi,cos = xyVel * Math.cos(rad),sen = xyVel * Math.sin(rad)]:[]]; ka[65] ? [mapa.y -= cos,mapa.x -= sen]:[ka[68] ? [mapa.y -= - cos,mapa.x -= - sen]:[]]; ka[38] || ka[87] ? [mapa.x += cos,mapa.y -= sen]:[ka[40] || ka[83] ? [mapa.x += - cos,mapa.y -= - sen]:[]]; } |