Colocar este codigo en el escenario y crear el objeto felicesfiestas en el stage y la clase Copito en la libreria.
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 |
var ancho:int = 800; var alto:int = 400; var velocidadCaidaCopito:Number = 1.5; var velocidadLateralCopito:Number = 0.3; var rangoDesplazamientoLateralCopito:int = 45; var COPITOS:Array = new Array(); function addCopito():void { var copito:Copito = new Copito(); copito.x = Math.floor(Math.random() * ancho); copito.posIniX = copito.x; copito.direccionInicial = Math.floor(Math.random() * 2);//0 para izquierda 1 para derecha copito.rangoIz = Math.floor(Math.random() * rangoDesplazamientoLateralCopito); copito.rangoDe = int(Math.random() * rangoDesplazamientoLateralCopito); addChild(copito); COPITOS.push(copito); } addEventListener(Event.ENTER_FRAME,renderFrame); function renderFrame(e:Event):void { var r:int = Math.floor(Math.random() * 4); if (r==1) { addCopito(); } for (var n:int=0; n<COPITOS.length; n++) { if (felicesfiestas.hitTestPoint(COPITOS[n].x,COPITOS[n].y,true)) { COPITOS[n].y += Math.random() * 1.5; COPITOS[n].y = COPITOS[n].y - felicesfiestas.y; COPITOS[n].x = COPITOS[n].x - felicesfiestas.x; felicesfiestas.addChild(COPITOS[n]); COPITOS.splice(n, 1); break; } else if (COPITOS[n].y > alto) { removeChild(COPITOS[n]); COPITOS.splice(n, 1); break; } COPITOS[n].y += velocidadCaidaCopito; if (COPITOS[n].direccionInicial == 0) {//Izquierda COPITOS[n].x -= velocidadLateralCopito; if (COPITOS[n].posIniX - COPITOS[n].x >= COPITOS[n].rangoIz) { COPITOS[n].direccionInicial = 1; } } else {//Derecha COPITOS[n].x += velocidadLateralCopito; if (COPITOS[n].x - COPITOS[n].posIniX >= COPITOS[n].rangoDe) { COPITOS[n].direccionInicial = 0; } } } } |