PDA

View Full Version : WHY?! - 1120: Access of undefined property *HEAD-DESK*



nikomax
April 21st, 2008, 06:45 AM
I can feel me posting a lot in my learning journey if AS3!
Sorry if the answer is really obvious but i cant figure it out.

I'm still working on a pre-loader, and I'm having loads of "1120: Access of undefined property _loadBar." errors. I really don't know why I'm getting these, so I hope someone can explain what I am doing wrong.

This is my code :


package com.utils{

import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import com.utils.Logo;
import com.utils._loaderBar;

public class Preloader extends MovieClip {

public function Preloader() {

var _myLoader:Loader = new Loader();
var _myRequest:URLRequest = new URLRequest("lesson.swf");
_myLoader.contentLoaderInfo.addEventListener(Event .OPEN, showLoader);
_myLoader.contentLoaderInfo.addEventListener(Progr essEvent.PROGRESS, showProgress);
_myLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, showLesson);
_myLoader.load(_myRequest);

}
function showLoader(evt:Event) {
var _loaderLogo:Logo = new Logo();
_loaderLogo.x = (stage.stageWidth / 2) ;
_loaderLogo.y = (stage.stageHeight / 2)- 30;
addChild(_loaderLogo);

var _loadBar:_loaderBar = new _loaderBar();
_loadBar.x = (stage.stageWidth / 2) - 150;
_loadBar.y = (stage.stageHeight /2);
_loadBar.width = 300;
addChild(_loadBar);

}

function showProgress(evt:ProgressEvent) {
var _percent = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
var _percentWidth = _percent * 3;
//_loadBar.width += _percentWidth;
trace(_percent);

}
function showLesson(evt:Event) {
//removeChild(_loaderLogo);
//removeChild(_loadBar);
//addChild(_myloader);
}
}
}
The commented lines are the problem lines each one causes a 1120 error.
What am I doing wrong?

Thanks

Maqrkk
April 21st, 2008, 06:48 AM
I think the problem is , you are making the loadbar in a function, without it being public. So the other function doesn't recognize it.


package com.utils{

import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import com.utils.Logo;
import com.utils._loaderBar;

public class Preloader extends MovieClip {

public var _loadbar:_loaderBar;

public function Preloader() {

var _myLoader:Loader = new Loader();
var _myRequest:URLRequest = new URLRequest("lesson.swf");
_myLoader.contentLoaderInfo.addEventListener(Event .OPEN, showLoader);
_myLoader.contentLoaderInfo.addEventListener(Progr essEvent.PROGRESS, showProgress);
_myLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, showLesson);
_myLoader.load(_myRequest);

}
function showLoader(evt:Event) {
var _loaderLogo:Logo = new Logo();
_loaderLogo.x = (stage.stageWidth / 2) ;
_loaderLogo.y = (stage.stageHeight / 2)- 30;
addChild(_loaderLogo);

_loadBar:_loaderBar = new _loaderBar();
_loadBar.x = (stage.stageWidth / 2) - 150;
_loadBar.y = (stage.stageHeight /2);
_loadBar.width = 300;
addChild(_loadBar);

}

function showProgress(evt:ProgressEvent) {
var _percent = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
var _percentWidth = _percent * 3;
//_loadBar.width += _percentWidth;
trace(_percent);

}
function showLesson(evt:Event) {
//removeChild(_loaderLogo);
//removeChild(_loadBar);
//addChild(_myloader);
}
}
} Check this example and see if it works, I removed the var declaration, and added public var outside the functions (as seen in the orange line).

EDIT: you should probably do the same for loaderLogo and myLoader I think.

thatguythatsme
April 21st, 2008, 06:53 AM
At first glance you problem seems to be because you are creating instances of your classes inside your class methods. This means that they are not accessible outside your methods, if you want to use something in more than one method then you must define it in your class...

I've made an example below, all I've done is move 3 of your lines of code up to the top and given them a private setting. Should work now.


package com.utils{

import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import com.utils.Logo;
import com.utils._loaderBar;

public class Preloader extends MovieClip {

private var _loadBar:_loaderBar = new _loaderBar();
private var _loaderLogo:Logo = new Logo();
private var _myLoader:Loader = new Loader();

public function Preloader() {

var _myRequest:URLRequest = new URLRequest("lesson.swf");
_myLoader.contentLoaderInfo.addEventListener(Event .OPEN, showLoader);
_myLoader.contentLoaderInfo.addEventListener(Progr essEvent.PROGRESS, showProgress);
_myLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, showLesson);
_myLoader.load(_myRequest);

}
function showLoader(evt:Event) {
_loaderLogo.x = (stage.stageWidth / 2) ;
_loaderLogo.y = (stage.stageHeight / 2)- 30;
addChild(_loaderLogo);

_loadBar.x = (stage.stageWidth / 2) - 150;
_loadBar.y = (stage.stageHeight /2);
_loadBar.width = 300;
addChild(_loadBar);

}

function showProgress(evt:ProgressEvent) {
var _percent = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
var _percentWidth = _percent * 3;
_loadBar.width += _percentWidth;
trace(_percent);

}
function showLesson(evt:Event) {
removeChild(_loaderLogo);
removeChild(_loadBar);
addChild(_myloader);
}
}
}


Dave

nikomax
April 21st, 2008, 06:39 PM
Thanks a lot guys =D

I had a feeling I was something like that. I did wonder if the other function could access the classes, but I thought that the classes would then be placed on stage and then when I called on something to change it would find the object easily!

Other then a typo I made it works fine!

Thanks again!