0) {
stop();
}
usernameBox.enabled = false;
goAllButton.enabled = false;
[Embed(source="student-twitter-swarm-images-orb-0.png")] var frame0:Class;
[Embed(source="student-twitter-swarm-images-orb-1.png")] var frame1:Class;
[Embed(source="student-twitter-swarm-images-orb-2.png")] var frame2:Class;
[Embed(source="student-twitter-swarm-images-orb-3.png")] var frame3:Class;
var anim:Array = [];
anim[0] = frame0;
anim[1] = frame1;
anim[2] = frame2;
anim[3] = frame3;
var tweet:student_twitter_swarm_Tweet;
for(var i:int = 0; i < tweets.length(); i++) {
tweet = new student_twitter_swarm_Tweet(tweets[i], emotions, colors);
addBoid(tweet, anim);
}
for(var r:int = 0; r < boids.length; r++) {
boids[r].calculateSimilarities(boids);
}
simClick();
}
private function addBoid (tweet:student_twitter_swarm_Tweet, anim:Array):void {
// Adding a boid requires an image that is added to the screen and
// interactivity with the user to display the message attached to the boid.
var boidImage:Image = new Image();
this.addChild(boidImage);
var boid:student_twitter_swarm_Boid = new student_twitter_swarm_Boid(boidImage, messagePanel, tweet, anim);
boidImage.addEventListener(MouseEvent.CLICK, this.hideAllTweets);
boidImage.addEventListener(MouseEvent.CLICK, boid.showTweet);
boids.push(boid);
}
private function hideAllTweets (event:MouseEvent):void {
for(var i:int = 0; i < boids.length; i++) {
boids[i].hideTweet();
}
}
// Animation
private var animID:uint;
private function start ():void {
// Starts the timer that will move the boids.
animID = setInterval(moveBoids, 50, boids, boids.length);
}
private function stop ():void {
// Removes the timer that moves the boids.
clearInterval(animID);
}
private function moveBoids(boids:Array, numBoids:int):void {
// Each boid is subject to these five forces that drive its behaviour.
// When the forces have changed the direction and speed (vector) of the
// boid the images need to be moved. So the function displayWorld() is
// called.
for(var i:int = 0; i < boids.length; i++) {
boids[i].alignment(boids, 300);
boids[i].cohesion(boids, 400);
boids[i].separation(boids, 15);
boids[i].treeSeeking(trees, 200);
boids[i].nesting(trees);
}
displayWorld();
}
private function displayWorld ():void {
// Moves the boids to their new coordinates using the current vector.
for(var r:int = 0; r < boids.length; r++) {
boids[r].move();
boids[r].moveImage(screenWidth, screenHeight)
}
}
]]>