# Floating Sprite Tags (TagCloud)

**URL:** <https://forum.kirupa.com/t/floating-sprite-tags-tagcloud/302563>\
**Category:** flash\
**Created:** [January 3, 2010, 1:42pm UTC](https://forum.kirupa.com/t/floating-sprite-tags-tagcloud/302563 "2010-01-03T13:42:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hmarcel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hmarcel](https://forum.kirupa.com/u/Hmarcel)\
**Post date:** [January 3, 2010, 1:42pm UTC](https://forum.kirupa.com/t/floating-sprite-tags-tagcloud/302563/1 "2010-01-03T13:42:17Z")

</div>

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:

```auto

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

```auto
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.

```auto

 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 ☹
