I*'m using flash cs3 and as3, just to make this very clear.*
Hey all. I figure I’m missing something tragically simple, but I’ve got a problem when calling a function of a run time created movieclip.
I create the object, add it to the container at root, which I call home.
I then call a function of the created object, and then i get this problem where the function I’m calling doesn’t have a clue about the declared vars in the created movie clip.
I event have to declare vars and events in order for them to work, which is why I know I’m missing something as I’ve done this exact thing before and it worked fine.
the stage code :
var home:MovieClip = this;
var mGraph:MovieClip = new movGraph();
var mGraphMask:MovieClip = new maskGraph();
home.addChild(mGraphMask);
home.addChild(mGraph);
home.mGraphMask.x = 10;
home.mGraphMask.y = 10;
home.mGraphMask.width = stage.stageWidth-20;
home.mGraphMask.height = stage.stageHeight-20;
home.mGraph.mask = home.mGraphMask;
home.mGraph.loadIndex("dow");
the ( cutdown, its a lot of drawing code ) movGraph code:
var theGraph:MovieClip = this;
var loader:URLLoader = new URLLoader();
var mouseXOffset:Number;
var mouseYOffset:Number;
loader.addEventListener(Event.COMPLETE,onLoaded);
loader.addEventListener(ProgressEvent.PROGRESS,onChange);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
theGraph.addEventListener(MouseEvent.MOUSE_DOWN,moveOn);
theGraph.addEventListener(MouseEvent.MOUSE_UP,moveOff);
function moveOn(e:MouseEvent):void {
mouseXOffset = e.target.localX;
mouseYOffset = e.target.localY;
trace("moving");
theGraph.addEventListener(MouseEvent.MOUSE_MOVE,moveGraph);
}
function moveOff(e:MouseEvent):void {
theGraph.removeEventListener(MouseEvent.MOUSE_MOVE,moveGraph);
}
function moveGraph(e:MouseEvent):void {
theGraph.x = e.localX - mouseXOffset;
theGraph.y = e.localY - mouseYOffset;
trace("X : " + theGraph.x + " Y : " + theGraph.y);
}
//loader.addEventListener(Event.ENTER_FRAME,onChange);
var aDataSlot:Array = new Array();
var xml:XML;
//store **** in the array now.
var items:Array = new Array();
var baseLineX:Number = 1;
var cursor:Number = 0;
var xScale:Number = 1;
var yScale:Number = 1;
var outputX:Number;
var outputY:Number;
var yMin:Number;
function onChange(e:ProgressEvent):void {
//info.text = e.bytesLoaded;
trace(loader.bytesLoaded);
}
function onLoaded(e:Event):void {
var yMin:Number;
trace("loading");
var xml = new XML(e.target.data);
outputX = theGraph.width * theGraph.scaleX;
outputY = theGraph.height * theGraph.scaleY;
var numOfItems:Number = xml.day.length();
yMin = xml.day[0].low;
for (var i=0; i < xml.day.length();i++) {
var t:MovieClip = new movDataSlot();
t.dataOpen = xml.day*.open;
t.dataHigh = xml.day*.high;
t.dataClose = xml.day*.last;
t.dataLow = xml.day*.low;
t.dataDate = xml.day*.date;
t.init();
t.addEventListener(MouseEvent.MOUSE_OVER,traceMe);
cursor=(t.width)*i;
t.x = cursor;
t.y = -t.dataHigh;
theGraph.addChild(t);
aDataSlot.push(t);
if (yMin > xml.day*.low) {
yMin = xml.day*.low;
}
}
//position graph
trace (yMin);
theGraph.scaleY = theGraph.mask.height / (Math.abs(theGraph.height) ) ;
theGraph.scaleX = theGraph.mask.width / theGraph.width;
trace(theGraph.scaleY);
theGraph.y = 400 +theGraph.height;
trace(" this is : " +this);
/*var gridY:Number = (yMax-yMin) / 10;
g.lineStyle(0,0x0000FF,.2);
for (var i=1;i<=10;i++) {
g.moveTo(0,i*gridY*yScale);
g.lineTo(numOfItems*3+55,i*gridY*yScale);
var txtVal:TextField= new TextField();
txtVal.textColor = 0x9999FF;
txtVal.text = styler.format(Math.round(yMin+(gridY*(10-i))));
txtVal.y = i*gridY*yScale-15;
txtVal.x = 0;
aGraph.push(txtVal);
sp.addChild(txtVal);
trace(Number(yMin+(gridY*i)));
}*/
}
function traceMe(e:MouseEvent):void {
trace(e.target.dataHigh);
}
function loadIndex(file:String):void {
trace("told to load " +file);
//loader = new URLLoader();
//loader.addEventListener(Event.COMPLETE,onLoaded);
theGraph.loader.load(new URLRequest(file + ".php"));
trace(this.mask);
//this.addEventListener(MouseEvent.MOUSE_DOWN,moveOn);
//this.addEventListener(MouseEvent.MOUSE_UP,moveOff);
}
function ioErrorListener(event:IOErrorEvent):void {
trace("Failure! Unable to load the configuration file." + event.toString);
}
and the output :
“told to load dow
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at movGraph/loadIndex()
at passoverlord_fla::MainTimeline/frame1()”
Now I was putting up with the fact i had to call the loader definition from inside the load function in the movGraph, but then when mouse events refused to do anything ( ie; trace “moving” ) i figured something was wrong. If i un-comment the lines in the function that create a new loader it does infact load a series of movieclips which stack together horizontally and have different heights, as they should. So the code works, I just seem to be having a problem with scope or something.
Any help would be greatly appreciated.
Anyone able to tell me whats up ?