Library Item to Stage
I have a library item that is called tomato and AS linkage called tomatoPieces. I am trying to get it to add the item to the stage and then randomally move the item around. Here is the code so far but the problem it seems to stop add the addEventListener to the stage. I would appreciate any help or direction. Thanks..
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.*;
var newX:Number = 0;
var newY:Number = 0;
var speed:Number;
var speedX:Number;
var speedY:Number;
var totalDistance:Number;
var previousDistance:Number = 0;
var currentDistance:Number = 0;
function main()
{
for (var i:Number = 0; i<9; i++)
{
trace(i);
var pieces:tomatoPiece = new tomatoPiece();
pieces.scaleX = .25;
pieces.scaleY = .25;
addChild(pieces);
//trace(this.addChild(pieces).name);
tomatoPieces();
}
}
function tomatoPieces()
{
trace("setup listener");
addEventListener(Event.ADDED_TO_STAGE, Setstage);
}
function Setstage(event:Event):void
{
trace("Setup");
this.x = this.GetRandomXPosition();
this.y = this.GetRandomYPosition();
speed = Math.round(.5 + Math.random() * 5);
this.addEventListener(Event.ENTER_FRAME, MoveCircle);
}
function GetRandomXPosition():Number
{
//
//basic formula: Math.floor(Math.random()*(1+High-Low))+Low;
//
return Math.floor(Math.random() * (1+ (stage.stageWidth + this.width) + this.width) - this.width);
}
function GetRandomYPosition():Number
{
//
//basic formula: Math.floor(Math.random()*(1+High-Low))+Low;
//
return Math.floor(Math.random() * (1+ (stage.stageHeight + this.height) + this.height) - this.height);
}
function MoveCircle(e:Event)
{
this.previousDistance = this.currentDistance;
this.currentDistance = this.GetDistance();
if (this.currentDistance < this.previousDistance)
{
this.x += this.speedX;
this.y += this.speedY;
}
else
{
this.SetNewPosition();
}
}
function GetDistance():Number
{
return Math.sqrt(Math.pow(this.x - this.newX,2) + Math.pow(this.y - this.newY,2));
}
function SetNewPosition()
{
this.newX = this.GetRandomXPosition();
this.newY = this.GetRandomYPosition();
this.totalDistance = this.GetDistance();
var time:Number = this.totalDistance / this.speed;
speedX = (this.newX - this.x)/time;
speedY = (this.newY - this.y)/time;
}
