Hi there,
I want to create a Tagcloud. Each Tag is a Sprite Object which loads its cotent via XML. I want to let them float around like they were flying in the air. It should be a defined area and the movement must me smooth and random.
I got this code:
package marcel{
/******************************
* CLASS: Floating Object
* Marcel
*
*
*
*
*
*
/*****************************/
import flash.display.Sprite;
import flash.geom.Point;
import flash.utils.getTimer;
import flash.display.DisplayObject;
public class FloatingObject{
private var rad:Number=1;
private var pos:Point //current Position
private var force:Point=new Point(0,0);//Forcevector
private var target:Point=new Point();//Target
private var lastTime:uint;
private var assignedTag:Sprite;
private var areaWidth:int = 250;
private var areaHeight:int = 300;
public function FloatingObject(assignedTag,tagX,tagY){
this.assignedTag=assignedTag
pos = new Point(tagX,tagY);
getNewTarget(areaWidth,areaHeight,rad*4);//choose random target
}
public function update():void{
//Richtungsvektor zum Ziel
var toTarget:Point=target.subtract(pos);
if(toTarget.length<220 || getTimer()-lastTime>3000)getNewTarget(areaWidth,areaHeight,rad*4);//neues Ziel, wenn altes zu nah (150px Distanz) oder timeout (3sec)
//Kraft "Weichzeichnen"
var f1:Number=0.9999;
var f2:Number=1-f1;
force.x=force.x*f1+toTarget.x*f2;
force.y=force.y*f1+toTarget.y*f2;
//force.normalize(2);//falls konstante Geschwindigkeit in px/Frame gewünscht
if(force.length>2)force.normalize(2);//max. Geschwindigkeit in px/Frame
//reflect on borders
if(pos.x+force.x<rad || pos.x+force.x>areaWidth-rad)force.x=-force.x;
if(pos.y+force.y<rad || pos.y+force.y>areaHeight-rad)force.y=-force.y;
//Apply movement and assign it to the assigned Tag
pos=pos.add(force);
if(assignedTag){
assignedTag.x=pos.x;
assignedTag.y=pos.y;
}
}
public function getNewTarget(w:Number,h:Number,border:int){
lastTime=getTimer();
target.x=Math.random()*(w-border*2)+border;
target.y=Math.random()*(h-border*2)+border;
}
}
}
In my mainclass the Floatingobjects are created like that
foArray*= new FloatingObject(tag,tagsXML.Tag.tagX*,tagsXML.Tag.tagY*);
After that i call the update-Method of the Class FloatingTag inside a OnEnterFrame-Event.
private function letTagsFloat(a:Event)
{
addEventListener(Event.ENTER_FRAME,update);
}
private function update(e:Event)
{
for(var i:int = 0; i < countTags; i++)
{
foArray*.update();
}
}
It doesnt work right. The Tags are starting outside the stage and are floating in large areas over the whole stage. If I change the " pos = new Point(tagX,tagY);" to pos = new Point(0,0); so that the startingpoint is 0,0 the Tags are starting on the correct position but moving very slowly and only pixel per pixel
Please help me 