actionscript 3 - Targeting Colors from an Array -
in project want color choose tracked box. problem code track 1 of colors if there 2 of same colors on stage. if can me out thank every much.
main.as
package { import flash.display.sprite; import flash.events.event; import flash.geom.point; [swf(framerate='31', width='1000', height='500')] public class main extends sprite { private var balls:array; private var directions:array = [new point(-1,-1),new point(0,-1),new point(1,-1), new point(-1,0),new point(1,0), new point(-1,1),new point(0,1),new point(1,1) ]; private var ballnum: number = 10; private var ball:ball; private var box:box; /*private var padding:number = 20; private var ay:number = 5; private var gravity:number = 6; private var bounce:number = -0.9; */ public function main() { init(); } private function init():void { balls = new array(); for(var i:number = 0; < ballnum; i++) { ball = new ball(math.random() * 30); ball.x = math.random() * stage.stagewidth; ball.y = math.random() * stage.stageheight; ball.direction = directions[math.floor(math.random()*directions.length)]; addchild(ball); balls.push(ball); } box = new box(); addchild(box); addeventlistener(event.enter_frame, onenterframe); } private function onenterframe(event:event):void { for(var i:int = 0; < balls.length; i++) { balls[i].x += balls[i].direction.x; balls[i].y += balls[i].direction.y; if (balls[i].colors == 0x79dcf4) { box.x = balls[i].x - box.width / 2; box.y = balls[i].y - box.height / 2; } } } } }
ball.as
package{ import flash.display.sprite; import flash.geom.point; public class ball extends sprite { public var colors:array = [0xffff33, 0xa5a5a5, 0x79dcf4, 0xff3333, 0xffcc33, 0x99cc33]; public var color:uint; public var radius:number; public var direction:point; public function ball(radius:number = 15, color:uint = 0xcccccc) { this.color = colors[randomcolor(0, colors.length - 1)]; this.radius = radius; init(); } private function randomcolor(min:number, max:number):number { var randomnumber:number = math.round(math.random() * (max - min) + min); return randomnumber; } private function init():void { graphics.beginfill(color); graphics.drawcircle(0,0,radius); graphics.endfill(); } } }
i think want track color, not colors
so change line
if (balls[i].colors == 0x79dcf4) {
to
if (balls[i].color == 0x79dcf4) {
try track 1 ball in each frame
private var blueballs:array = [];//to save blue balls private function init():void { balls = new array(); for(var i:number = 0; < ballnum; i++) { ball = new ball(math.random() * 30); ball.x = math.random() * stage.stagewidth; ball.y = math.random() * stage.stageheight; ball.direction = directions[math.floor(math.random()*directions.length)]; addchild(ball); balls.push(ball); if (ball.color == 0x79dcf4) { blueballs.push(ball); } } box = new box(); addchild(box); addeventlistener(event.enter_frame, onenterframe); } private var lastblueballindex:int = -1;//save blue ball has been tracked in blueballs private var hasfind:boolean = false; private function onenterframe(event:event):void { hasfind = false; for(var i:int = 0; < balls.length; i++) { balls[i].x += balls[i].direction.x; balls[i].y += balls[i].direction.y; var index:int = blueballs.indexof(balls[i]); if (index != -1 && !hasfind ) { //here main part, when reach last blue ball, reset lastblueballindex if (index == blueballs.length - 1) {//if last blue ball lastblueballindex = -1; } else if (lastblueballindex >= index) {//if ball has been tracked continue; } else { lastblueballindex = index;//set current ball index } hasfind = true; box.x = balls[i].x - box.width / 2; box.y = balls[i].y - box.height / 2; } } }
edit
use timer instead of enter_frame , remember remove event.enter_frame listerer.
private function init():void { /*the old code*/ var timer:timer = new timer(1000);//set interval time longger if want // can change onenterframe name, ontimer timer.addeventlistener(timerevent.timer, onenterframe); timer.start(); }
Comments
Post a Comment