A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: AS2 Class: method don't see property

  1. #1
    Junior Member
    Join Date
    Dec 2000
    Location
    Russia
    Posts
    5

    AS2 Class: method don't see property

    If have a MusicFile class that I'll use to control sound from mp3-file. I work in FAME SDK.

    Code:
    class game.MusicFile extends game.MusicObject {
    	private var mp3file:Sound	
    	
    	public function MusicFile () {
    	}
    	/*
    	* loading file (not finished yet)
    	*/
    	public function load(mp3fileLocation:String):Void {	
            TRACE(Flashout.INFO + "mp3 started loading");	
    		var z = scopeRef.createEmptyMovieClip("mp3fileContainer", scopeRef.getNextHighestDepth());
    		mp3file = new Sound(scopeRef.mp3fileContainer);
    		TRACE(Flashout.DEBUG + "setting onLoad");		
    		mp3file.onLoad = this.onLoad;
    		TRACE(Flashout.DEBUG + "onLoad set");
    		mp3file.loadSound("music.mp3", false);
    	}
    	public function startPlay():Void {
    		TRACE(Flashout.DEBUG + "this is mp3file="+mp3file);
    		mp3file.start();
    		TRACE(Flashout.DEBUG + "mp3 playing");
    	}
    	private function onLoad(success:Boolean):Void {	
    		var suc = success ? "successfully" : "not successfully";
    		TRACE(Flashout.DEBUG + "onLoad called. loaded "+suc);
    		TRACE(Flashout.DEBUG + scopeRef);
    		if (success) {
    			TRACE(Flashout.DEBUG + instanceof(this));	
    			TRACE(Flashout.INFO + "mp3file=" + mp3file);			
    			this.startPlay();		
    		} else {
    			TRACE(Flashout.ERROR + "mp3 not loaded");
    		}
    	}
    }
    When I try to compile it, it traces:

    [45:14:786] info: game.Game.Game()
    Application started

    [45:14:786] info: game.MusicFile.load()
    mp3 started loading

    [45:14:786] debug: game.MusicFile.load()
    setting onLoad

    [45:14:786] debug: game.MusicFile.load()
    onLoad set

    [45:14:997] debug: game.MusicFile.onLoad()
    onLoad called. loaded successfully

    [45:14:997] debug: game.MusicFile.onLoad()
    _level0

    [45:14:997] debug: game.MusicFile.onLoad()
    undefined

    [45:15:007] info: game.MusicFile.onLoad()
    mp3file=undefined // what da f#ck???

    why this method don't see 'mp3file' property? where am I wrong?

    please, help me. work had stuck and I have no idea, how to solve this problem.

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    The reason you are getting undefined is that the scope of the onLoad function is different to that of the class. The onLoad function has the scope of the sound object (references to this in the function refer to the sound object and not the class object).

    You can use the Delegate class to get round this and run the onLoad function within the scope of the class rather than of the sound object..

    Code:
    mp3file.onLoad = mx.utils.Delegate.create(this, onLoad);
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center