03 February 2009

Robot Robby and his everyday work (AS3)

They say how best practice when learning new language is to write small chunks of code, mini test apps all the time. Since reading about motions in actionscript3 right now, small useless idea came to me and robot Robby was born.

Robby is movie clip that moves around the stage and draws some random lines until reaches his limit or your cpu burns out in flames.

How to create Robby the robot?

He is movie clip, right? So, make one and add linkage class property robot, base class flash.display.MovieClip. To place a robot on the stage and name it use following code:

var robby:MovieClip = new robot();
robby.x = 250;
robby.y = 200;
addChild(robby);

and that's it, the robot is born. But this particular robot (named robby) doesn't do much, so let's make something about it. Robby will draw some lines, he will create some random art. Let's decide what kind of lines are good for robby. Make your taste plays with you here.

var sp:Sprite = new Sprite();
addChild(sp);
var g:Graphics = sp.graphics;
g.lineStyle(2, 0x000099);
g.moveTo(robby.x, robby.y);

Ok, robby is in a posession of one line and now he will get some instructions how to draw and until when.

var angle:Number = 45;
var radians:Number = deg2rad(angle);
var speed:Number = 50;
var xVel:Number = Math.cos(radians) * speed;
var yVel:Number= Math.sin(radians) * speed;
var counter:Number = 0;
var stopCounter:Number = 0;

Everything is obvious here, more or less. Counter tells robby when to start with new line and stopCounter tells him when work is done. Measure is number of frames, not seconds. If you wondered how deg2rad function looks like here it is:

function deg2rad(deg:Number):Number {
return deg * (Math.PI/180)
}

But robby won't start new line only when counter tells him to do so, it will create new line when he found himself out of stage. There is no point to draw lines somewhere we can't see, right? When this happens, robby will be instructed to go back, in the stage center and start all over again, but only after new line properties are calculated, using function with same name:

function calculate():void {
angle = Math.random()*360;
radians = deg2rad(angle);
speed = 50;
xVel = Math.cos(radians) * speed;
yVel= Math.sin(radians) * speed;
}

Only instructions are left and here it is, event listener and it's companion function onLoop:

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);

function onLoop(evt:Event):void{
if (stopCounter < 1000) {
if (counter < 20) {
if((robby.x > 500)||(robby.x < 0)) {
robby.x = 250;
robby.y = 200;
}
if((robby.y > 400)||(robby.y < 0)) {
robby.y = 250;
robby.x = 200;
}
robby.x += xVel;
robby.y += yVel;
counter++;
g.lineTo(robby.x, robby.y);
}else{
counter = 0;
calculate();
}
stopCounter++;
}else{
stop();
}
}

Now, programming robby is fun. You can change lines color or robby's speed and even completely randomize it. You can even clone robby and add him different instructions! If you do so, I would really like to know about it.



.

0 comments:

 

template by blogger templates