Movimiento aleatorio AS3
2 seguidores
Estoy diseñando unos clips para simular movimientos de bichos de luz (se imaginan), movimiento aleatorio en un area especifica, se trata de clase math.random:
var velX:Number=Math.random()*5;
var velY:Number=Math.random()*5;
addEventListener(Event.EnterFrame, movimientoAleatorio);
function movimientoAleatorio(e:Event){
bug.x+=velX;
bug.y+=velY;
}
Me aparecen errores de compilador 1119 y 1120, que falta aqui.
lukanicos
BuenAS:
Prueba
import flash.display.Shape;
import flash.events.Event;
import flash.display.GradientType;
import flash.geom.Matrix;
var num = 100;
var _w:Number = stage.stageWidth;
var _h:Number = stage.stageHeight;
var p:Vector.<Shape> = new Vector.<Shape>(num, true);
var s:Shape;
var m:Matrix = new Matrix();
m.createGradientBox(6, 6, 0, -3, -3);
var colors:Array = [0xFFFFFF, 0x000000];
var alphas:Array = [1, 1];
var ratios:Array = [0, 255];
for (var i:int = 0; i < num; ++i) {
s = new Shape();
s.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, m);
s.graphics.drawCircle(0, 0, 3);
s.x = _w*Math.random();
s.y = _h*Math.random();
this.addChild(s);
p[i] = s
};
this.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void
{
for each(var s:Shape in p) {
s.x += Math.round(5*Math.random()) - 2;
s.y += Math.round(5*Math.random()) - 2;
if (s.x < 0)
s.x += _w
else if(s.x > _w)
s.x -= _w;
if (s.y < 0)
s.y += _h
else if(s.y > _h)
s.y -= _h;
};
}
El Math.round podrías cambiarlo por Math.floor (dentro del controlador loop) si quieres que las "luciérnagas no avancen, y permanezcan estacionarias moviendose alrededor de su posición inicial.
Un saludo.
sirio
Ehhh, gracias Lukanico, funciona desde el principio.
Lo que hace la programacion, :( , me ayuda muchisimo.
Saludos