PDA

View Full Version : Working with events (help with code?)


Smokey_Da_Funk
11-30-2010, 08:10 PM
Hi, I'm a noob who somehow found his way into a situation where he needs to do stuff with flash with no previous understanding.

I'm trying to make a program that grabs info from a text file then uses the info in the file to assign where certain balls will rotate (eventually I'll configure it to looking like dots rotating around an invisible sphere)

Now, I can do these two things separately, load info from a file and make blue balls rotate around given points... but I've been tripping up hard on making them work together, and I think my problem is in how I handle the events and events listeners.

to work with the code, you'll need to start a new flash AS3 file
Then on the stage create a circle with the oval tool
convert the circle to a symbol named "BlueCircle"
and name the linkage class as "BlueCircle"

then paste this code...

var longLatLoader:URLLoader = new URLLoader();
longLatLoader.dataFormat=URLLoaderDataFormat.VARIA BLES;

longLatLoader.addEventListener(Event.COMPLETE, onLoaded);

//longLatLoader.addEventListener(Event.COMPLETE, addCircles);

var city:Array = new Array();
var country:Array = new Array();
var longDeg:Array = new Array();
var longmin:Array = new Array();
var longDir:Array = new Array();
var latDeg:Array = new Array();
var latMin:Array = new Array();
var latDir:Array = new Array();

function onLoaded(e:Event):void {

//This bit traces the info from the file, verifying a good load.
trace(e.target.data.city);
trace("\n");
trace(e.target.data.country);
trace("\n");
trace(e.target.data.longDeg);
trace("\n");
trace(e.target.data.longmin);
trace("\n");
trace(e.target.data.longDir);
trace("\n");
trace(e.target.data.latDeg);
trace("\n");
trace(e.target.data.latMin);
trace("\n");
trace(e.target.data.latDir);
trace("\n");

for(var f=0;f<=118;f++)
{

city.push(e.target.data.city[f]);//Right now this block of code is useless as I can't access either
country.push(e.target.data.country[f]);// array outside of the onLoaded function
longDeg.push(e.target.data.longDeg[f]);
longmin.push(e.target.data.longmin[f]);
longDir.push(e.target.data.longDir[f]);
latDeg.push(e.target.data.latDeg[f]);
latMin.push(e.target.data.latMin[f]);
latDir.push(e.target.data.latDir[f]);
addCircles();

longLatLoader.addEventListener(Event.COMPLETEe, addCircles); //Been trying to figure out how
//to place this code
function addCircles(evt:Event)
{

var cy = 360; //center y
function AddCircle(evt:Event):void {
if (e.target.data.longDir[f] == 'N')
{
cy = 560 + int(e.target.data.longDeg[f]);
trace ("cy is ",cy,"\n");
}else if (e.target.data.longDir[f] == 'S')
{
cy = 560 - int(e.target.data.longDeg[f]);
trace ("cy is ",cy,"\n");
}
}
var cx = 500; //center x
var a = 400; //radius x
var b = 100; //radius y
var r;
var t;

// Adding a circle to the stage
var newCircle:BlueCircle = new BlueCircle();
addEventListener(Event.ENTER_FRAME, dotRotation);
function dotRotation(evt:Event):void
{
t += .01;
//r += .1; //just for funsies
newCircle.x = cx + a*Math.cos(r)*Math.cos(t) - b*Math.sin(r)*Math.sin(t);
newCircle.y = cy + a*Math.sin(r)*Math.cos(t) + b*Math.cos(r)*Math.sin(t);
}
addChild(newCircle);
newCircle.scaleX = 1;
newCircle.scaleY = 1;
newCircle.alpha = 1;
addChild(newCircle);

}
}
}

longLatLoader.load(new URLRequest("LongLatf.txt"));


into the first frame.

You'll also need a text file named "LongLatf.txt"

Here it is,

rapidshare.com/files/434145343/LongLatf.txt

in the same folder as the AS file

I know it's a lot, but I really want to get this done, and I really want to learn how to do it right...


Thanks a bunch in advanced!:)

Smokey_Da_Funk
12-01-2010, 02:03 AM
Actually, I narrowed down the problem, it's definitely something with an event or the listener. I managed to woodshed the code to where it will at least put the blue circle (s) on the screen.

But the function that will put the circles on different rotations isn't getting triggered for some reason, at around line 73.

Does anyone know why it isn't using the dotRotation function?

I tried putting the listener for dots rotation at the begining of the program, but I got an error message saying dotRotation was an undefined property.
and when I tried to take the dot Rotation function out of the onloaded function it couldn't call any of the variables from with in the onloaded function.


var longLatLoader:URLLoader = new URLLoader();
longLatLoader.dataFormat=URLLoaderDataFormat.VARIA BLES;
longLatLoader.addEventListener(Event.COMPLETE, onLoaded);
//longLatLoader.addEventListener(Event.COMPLETE, AddCircle);

var city:Array = new Array();
var country:Array = new Array();
var longDeg:Array = new Array();
var longmin:Array = new Array();
var longDir:Array = new Array();
var latDeg:Array = new Array();
var latMin:Array = new Array();
var latDir:Array = new Array();
function onLoaded(e:Event):void {



//This bit traces the info from the file, verifying a good load.
trace(e.target.data.city);
trace("\n");
trace(e.target.data.country);
trace("\n");
trace(e.target.data.longDeg);
trace("\n");
trace(e.target.data.longmin);
trace("\n");
trace(e.target.data.longDir);
trace("\n");
trace(e.target.data.latDeg);
trace("\n");
trace(e.target.data.latMin);
trace("\n");
trace(e.target.data.latDir);
trace("\n");

for(var f=0;f<=118;f++)
{

city.push(e.target.data.city[f]);//Right now this block of code is useless as I can't access either
country.push(e.target.data.country[f]);// array outside of the onLoaded function
longDeg.push(e.target.data.longDeg[f]);
longmin.push(e.target.data.longmin[f]);
longDir.push(e.target.data.longDir[f]);
latDeg.push(e.target.data.latDeg[f]);
latMin.push(e.target.data.latMin[f]);
latDir.push(e.target.data.latDir[f]);


var newCircle:BlueCircle = new BlueCircle();


var cy = 360; //center y

if (e.target.data.longDir[f] == 'N')
{
cy = 560 + int(e.target.data.longDeg[f]);
trace ("cy is ",cy,"\n");
}else if (e.target.data.longDir[f] == 'S')
{
cy = 560 - int(e.target.data.longDeg[f]);
trace ("cy is ",cy,"\n");
}

var cx = 500; //center x
var a = 400; //radius x
var b = 100; //radius y
var r;
var t;

addChild(newCircle);


addEventListener(Event.ENTER_FRAME, dotRotation);


function dotRotation(evt:Event)

{
t += .01;
//r += .1; //just for funsies
newCircle.x = cx + a*Math.cos(r)*Math.cos(t) - b*Math.sin(r)*Math.sin(t);
newCircle.y = cy + a*Math.sin(r)*Math.cos(t) + b*Math.cos(r)*Math.sin(t);
}
newCircle.scaleX = 2;
newCircle.scaleY = 2;
newCircle.alpha = 1;


}
}
//}


longLatLoader.load(new URLRequest("LongLatf.txt"));