Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-17-2008, 12:55 AM   #1
lordofthestring
Registered User
 
Join Date: Jul 2008
Posts: 5
Default addEventListener(Event.COMPLETE, onLoaded) function problem

hi, everyone, i am new and i need help
i hope my problem is not one everyone knows and talks about, but i couldn't really find an answear to it via google, and am a little bit desperate to find a solution..

problem is: the
addEventListener(Event.COMPLETE, onLoaded) does not finish what it should do before i call the
addEventListener(Event.ENTER_FRAME, loop) function, or so it seems..
WHY!?!?

i do not really understand why this is happening, maybe someone can explain to me where my thinking is wrong..

the error msg is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.

that shouldn't be possible tho, since i initialize the concerning variable (sc:Soundchannel) beforehand..

code:

ActionScript Code:
package {     import flash.display.Sprite;     import flash.events.Event;     import flash.media.Sound;     import flash.media.SoundChannel;     import flash.net.URLLoader;     import flash.net.URLRequest;     public class testprojekt extends Sprite     {         public var loader:URLLoader = new URLLoader();         public var sound: Sound = new Sound();         public var sc:SoundChannel = null;         public var xml:XML;         public var il: XMLList = new XMLList();                 public var soundIndex:Number = 0;         public var soundEndIndex:Number;                 public var speed:Number = 100;                 public var chordList:XMLList;         public var testString:String;                 public function testprojekt()         {             loader.load(new URLRequest("sound.xml"));                         loader.addEventListener(Event.COMPLETE, onLoaded);             // chordList from SoundInput class             var sInput: SoundInput = new SoundInput("soundFile_test.xml");             chordList = sInput.getSoundInput();             addEventListener(Event.ENTER_FRAME, loop);         }                 public function onLoaded(e:Event):void         {                // xml loaded             xml = new XML(loader.data);             il = xml.tracks.sound;             soundEndIndex = il.name.length();                         // start first sound             sound = new Sound(new URLRequest("sounds/" + il.name[soundIndex]) );                        sc = sound.play();                     }                 public function loop(e:Event):void         {             //trace("here is the error and i do not know why");             if (sc.position >= speed)             {                 sc.stop();                 setSound();             }            }         public function setSound():void         {             soundIndex++;             if (soundIndex < soundEndIndex)             {                 trace(soundIndex);                 sound = new Sound(new URLRequest("sounds/" + il.name[soundIndex]) );                 trace(il.name[soundIndex]);                 sc = sound.play();             }             else             {                 removeEventListener(Event.ENTER_FRAME, loop);             }            }     } }



thx for anyone reading this through
lordofthestring is offline   Reply With Quote
Old 07-17-2008, 01:17 AM   #2
bloodstyle
Registered User
 
Join Date: Feb 2008
Posts: 187
Default

I don't have the faintest clue about sounds, but it looks like you don't initialize the sc variable until the Event.COMPLETE is fired, so that'd be why.
bloodstyle is offline   Reply With Quote
Old 07-17-2008, 01:29 AM   #3
lordofthestring
Registered User
 
Join Date: Jul 2008
Posts: 5
Default

true.. but the Event.COMPLETE should always come and finish the initialisation before the Event.EnterFrame (loop), where sc is needed.. so why is that a problem??
lordofthestring is offline   Reply With Quote
Old 07-17-2008, 01:54 AM   #4
bloodstyle
Registered User
 
Join Date: Feb 2008
Posts: 187
Default

Apparently loading takes longer than the first enter frame event. Why not just add the enter frame listener in the load complete function as well?
bloodstyle is offline   Reply With Quote
Old 07-17-2008, 02:00 AM   #5
lordofthestring
Registered User
 
Join Date: Jul 2008
Posts: 5
Default

omg.. of course.. lol.. works..

don't really get it tho.. why does it not wait until it is loaded before moving on with the code..?

strange behaviour..
lordofthestring is offline   Reply With Quote
Old 07-17-2008, 02:25 AM   #6
lordofthestring
Registered User
 
Join Date: Jul 2008
Posts: 5
Default

great, i think i have the same problem again, but this time i cannot solve it like before..

new class with constructor:
ActionScript Code:
private var chordList: XMLList = null; public function SoundInput(sInput:String)         {             soundFileInput = sInput;                loader.load(new URLRequest(soundFileInput));                         loader.addEventListener(Event.COMPLETE, onLoaded);         } private function onLoaded(e:Event):void         {                // xml loaded             xml = new XML(loader.data);             chordList = xml.trackName.chords.ch;                         trace("SoundInput in constructor: " + chordList);                  }

and getter

ActionScript Code:
ublic function get getSoundInput():XMLList         {             return chordList;         }

but the chordList variable in the getter method does not have a value (null, as initialized above), because at the time i call the getter, the event.complete doesn not seem to be finished..
lordofthestring is offline   Reply With Quote
Old 07-17-2008, 03:57 AM   #7
QuantumTiger
Member
 
QuantumTiger's Avatar
 
Join Date: Jul 2008
Location: London
Posts: 69
Default

Quote:
Originally Posted by lordofthestring View Post
don't really get it tho.. why does it not wait until it is loaded before moving on with the code..?

strange behaviour..
Events allow you to place behaviour asynchronously (ie it is exactly so that it does not wait before moving on) which means that you can request a loader and do other things in the meantime (such as display a splash screen, a loading progress bar etc). It does, however, take a bit of getting used to if you're new to it particularly because sometimes you have no option but to wait for things to complete.

In terms of waiting there are numerous options for how to achieve that, for example passing a function to your onLoaded event which it calls once it is ready to continue, using a timer to poll your getter (ie if it returns null you wait a short period and try again) or dispatching a custom event from your onLoaded function and setting up an event listener for that which then calls the getter and contines with your code.
QuantumTiger is offline   Reply With Quote
Old 07-17-2008, 08:11 AM   #8
lordofthestring
Registered User
 
Join Date: Jul 2008
Posts: 5
Default

hi, thanks for explaining that behaviour.. didn't really make sense to me so far..

unfortunately i still am struggling creating a timer or custom event to wait for it to finish loading..

could you tell me how to access the getter from another class, making sure the value is not null??

i tried it something like that,
ActionScript Code:
while (this.chordList == null)             {                 this.timer.addEventListener(TimerEvent.TIMER, sInput.getSoundInput);                 this.timer.start();             }
but it doesn't work that way of course..

from the getter i tried:
ActionScript Code:
public function get getSoundInput():XMLList         {             while (chordList == null)             {                 this.timer.addEventListener(TimerEvent.TIMER, onComplete);                 this.timer.start();             }             return this.chordList;         }

but i guess i am on the wrong track again...
lordofthestring is offline   Reply With Quote
Old 07-17-2008, 08:58 AM   #9
Dail
noponies
 
Join Date: Sep 2007
Location: New Zealand
Posts: 221
Send a message via ICQ to Dail Send a message via AIM to Dail
Default

cant you just test if the value is null and return something if it is?

return value!=null ?returnValue:returnOtherValue
Dail is offline   Reply With Quote
Old 07-17-2008, 01:19 PM   #10
QuantumTiger
Member
 
QuantumTiger's Avatar
 
Join Date: Jul 2008
Location: London
Posts: 69
Default

You could try something like
ActionScript Code:
private var t:Timer private var chordList: XMLList = null; public function CheckXMLReady():void {   t = new Timer(500, 0);   t.addEventListener("timer", GetSoundInput);   t.start(); }         private function GetSoundInput(e:Event):void {   chordList = sInput.getSoundInput();   if (chordList != null)   {     // Kill the timer and remove the listener     t.removeEventListener("timer", GetSoundInput);     t.stop();     // call your continuation code here   } }
Probably a bit of a kludge - you may be better using custom events (see http://curiousmindsmedia.wordpress.c...vent-tutorial/ for a tutorial)

Last edited by QuantumTiger; 07-17-2008 at 01:22 PM.
QuantumTiger is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with MCL and external txt and jpg files. nilshenrik ActionScript 2.0 4 08-01-2005 11:13 PM
Maybe a clearInterval problem or some other solution to stop a timed function jlu ActionScript 2.0 0 06-20-2005 05:55 PM
Warnings from using "Trace and debug from your browser" Dire Entity ActionScript 2.0 4 06-17-2005 04:28 PM
Photo switch on runtime problem provision ActionScript 2.0 0 01-17-2005 11:29 AM
flash movie plays in browser too slow m1cky ActionScript 2.0 7 01-11-2005 08:11 PM


All times are GMT. The time now is 09:24 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.