Tutorial: Control de personaje, parte 4 Año: 2011 V.: AS3
[e]
Nueva actualización de los ejemplos anteriores del juego, esta vez con detección de colisiones.
Para detectar las colisiones he utilizado las clases detectionKid de coreyoneil
Dados los problemas que puede dar el ángulo de rotación del mapa se producen algunos fallos en las limitaciones de movimiento atravesando paredes donde no debería, aunque el sistema no es perfecto sería aceptable para un juego sencillo.
También deja de funcionar correctamente si no colocamos las barreras/movieclips a 90º es decir si rotamos un objeto manualmente dentro del mapa.
Sería de agradecer si algún experto en trigonometría fuera capaz de mejorar este ejemplo implementando alguna fórmula tipo Verlet o algo parecido para evitar que el protagonista atraviese los objetos o barreras de una manera eficiente sin importar el ángulo del mapa, algo que encuentro bastante complejo pero seguro que tiene solución con trigonometría.
CLICK EN EL JUEGO PARA OBTENER EL FOCO
.
[download id=»24″ 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
/*XAVIROBOT.COM 2011*/ import flash.display.Sprite; import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import com.coreyoneil.collision.CollisionList; var hero:MovieClip = world.hero; var _collisionList:CollisionList = new CollisionList(hero); _collisionList.addItem(world.mapa.rectangle1); _collisionList.addItem(world.mapa.rect2); _collisionList.addItem(world.mapa.rect3); _collisionList.addItem(world.mapa.rect4); _collisionList.addItem(world.mapa.rect5); _collisionList.addItem(world.mapa.rect6); 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:MovieClip = world.mapa; var ka:Array = new Array(); var key:int; var pres:int = 3; 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"); } else if (ka[68]&&(ka[40]||ka[83])) {//Key[D] and Key[S] or Key[Down] then move DownRigth("AbajoDerecha"); goTo("atras"); } else if ((ka[40]||ka[83])&&ka[65]) {//Key[S] or Key[Down] and Key[A] then move DownLeft("AbajoIzquierda"); goTo("atras"); } else if (ka[65]&&(ka[87]||ka[38])) {//Key[A] and Key[W] or Key[Up] then move UpLeft("IzquierdaArriba"); goTo("move"); } else if (ka[87] || ka[38]) {//Key[W] or Key[Up] then move Up("Arriba"); goTo("move"); } else if (ka[68]) {//Key[D] then move Rigth("Derecha"); hero.scaleX = -1; goTo("desplzLateral"); } else if (ka[83] || ka[40]) {//Key[S] or Key[Down] then move Down("Abajo"); goTo("atras"); } else if (ka[65]) {//Key[A] then move Left("Izquierda"); goTo("desplzLateral"); hero.scaleX = 1; } else if (ka[37]) {//Key[Left] then rotate Left("Rotar Izquierda"); goTo("relax"); } else if (ka[39]) {//Key[Rigth] then rotate Rigth("Rotar Derecha"); goTo("relax"); } else {//No Key? then Relax goTo("relax"); } }//End if check Keys pres = 3; } } function goTo(action):void { action == "relax" ? hero.gotoAndStop(action):hero.gotoAndPlay(action); } function onFrame(e:Event):void { //Detectamos colisiones var collisions:Array = _collisionList.checkCollisions(); if (collisions.length) { for (var s:int=0; s<collisions.length; s++) { var collision:Object = collisions[s]; //var angle:Number = collision.angle; var overlap:int = collision.overlapping.length; var obj2:Object = collision.object2; var obj1:Object = collision.object1; //Sistema de bloqueo mejorable if (hero.y > (obj2.y + world.mapa.y)) { mapa.y = mapa.y -(overlap*0.03); } else { mapa.y = mapa.y + (overlap*0.03); } if (hero.x > (obj2.x + world.mapa.x)) { mapa.x = mapa.x - (overlap*0.03); } else { mapa.x = mapa.x + (overlap*0.03); } } } //Control if (ka[37]) { rad =((world.rotation +=sR)-90)*consPi; hero.rotation -= sR; cos = xyVel * Math.cos(rad); sen = xyVel * Math.sin(rad); } else if (ka[39]) { rad =((world.rotation -=sR)-90)*consPi; hero.rotation += sR; cos = xyVel * Math.cos(rad); sen = xyVel * Math.sin(rad); } if (ka[65]) { mapa.y -= cos; mapa.x -= sen; } else if (ka[68]) { mapa.y -= - cos; mapa.x -= - sen; } if (ka[38] || ka[87]) { mapa.x += cos; mapa.y -= sen; } else if (ka[40] || ka[83]) { mapa.x += - cos; mapa.y -= - sen; } } |
Autor Javier Vicente Medina
Social Report xavirobot