A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Variable scope type problem thing ...

  1. #1
    Arrrr, this stupid thing is driving me insane. I have a movie called mainBg with this code:

    Code:
    onClipEvent (load) {
    	var bgRand;
    	bgSpeed = 30;
    
    	function attachBg() {
    		bgRand = random(4);
    		if (bgRand == 0) {
    			bgRand = 1;
    		}
    		
    		this.attachMovie("bgTile" + bgRand, "tile" + bgRand, 1);
    	}
    	
    	attachBg();
    	bgStartX = this._x;
    }
    
    onClipEvent (enterFrame) {	
    	this._x -= bgSpeed;
    	if (this._x <= (bgStartX - this["tile" + bgRand]._width)){
    		this.removeMovieClip("tile" + bgRand)
    		attachBg();
    		this._x = bgStartX - bgSpeed;
    	}
    }
    Then I have another movie clip called tileBg. In this mc all I'm trying to do is

    Code:
    onClipEvent (load) {
        trace(_root.mainBg.bgRand);
    }
    All it traces is undefined! Any ideas, please?!

  2. #2
    gskinner.com
    Join Date
    Feb 2002
    Location
    N.America
    Posts
    455
    The problem is that you are limiting the scope of the variable by defining it with var. bgRand only exists while the load event is occuring, and is deleted immediately thereafter. Remove the line:

    var bgRand;

    and it should work fine.

    Cheers,
    ZE.

  3. #3
    Sorry, that's still not working ... don't I need to delcare it anyway so that the statements in the enterFrame event can use bgRand (as it was defined in the function)?

    What I have now:

    MainBg
    Code:
    onClipEvent (load) {
    	bgSpeed = 30;
    
    	function attachBg() {
    		bgRand = random(4);
    		if (bgRand == 0) {
    			bgRand = 1;
    		}
    		
    		this.attachMovie("bgTile" + bgRand, "tile" + bgRand, 1);
    	}
    	
    	attachBg();
    	bgStartX = this._x;
    }
    
    onClipEvent (enterFrame) {	
    	this._x -= bgSpeed;
    	if (this._x <= (bgStartX - this["tile" + bgRand]._width)){
    		this.removeMovieClip("tile" + bgRand)
    		attachBg();
    		this._x = bgStartX - bgSpeed;
    	}
    }
    tileBg
    Code:
    onClipEvent (load) {
    	trace("load: " + _root.mainBg.bgRand);
    }
    
    onClipEvent (enterFrame) {
    	trace("enterFrame: " + _root.mainBg.bgRand);
    }
    Please help! This is driving me insane ...

  4. #4
    gskinner.com
    Join Date
    Feb 2002
    Location
    N.America
    Posts
    455
    Are you instantiating both MCs in the same frame? Try instantiating tileBg one frame after mainBg.

    Worth a try.

    Cheers,
    ZE.

  5. #5
    Nope, no luck ... could it be something to do with the use of attachMovie or smth? Or the that the bgRand function is in the load event?

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