A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: OOP troubles: Delegate with Iteration

Hybrid View

  1. #1
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    I meant that you could do something like this, where the reference to the class object is passed to the button so that the function in the class object scope can be called from within the button scope.

    It's easier to explain in code ...

    Code:
    class MyMenu extends MovieClip
    {	
    	// declare the button specific vars so you don't get compile errors.
    	var scope:MovieClip, id:Number;
    	
    	// constructor
    	function MyMenu()
    	{
    		var _mc:MovieClip;
    		for(var i:Number=0; i<5; i++)
    		{
    			// create the clips and assign the mouse event
    			_mc = createEmptyMovieClip('_btn'+i, i);
    			_mc.onRelease = buttonRelease;
    			
    			// vars to be passed as argument to the someClassFunc
    			_mc.id = i;
    			// a reference to the class object so that the someClassFunc can be correctly addressed and executed
    			_mc.scope = this;
    			
    			// draw and position the buttons
    			_mc.beginFill(0xFF, 100);
    			_mc.lineTo(50,0);
    			_mc.lineTo(50,50);
    			_mc.lineTo(0,50);
    			_mc.lineTo(0,0);
    			_mc.endFill();
    			_mc._y = i*55;
    		}
    	}
    	
    	// function will run in the class object scope
    	private function someClassFunc(id:Number, btn:MovieClip):Void
    	{
    		trace("Button id: "+id);
    		trace("Button ref: "+btn);
    		trace("Scope of this function: "+this);
    	}
    	
    	// the onRelease function running in the individual button's scopes
    	private function buttonRelease(Void):Void
    	{
    		this.scope.someClassFunc(this.id, this);
    	}
    }
    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?

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    237
    Thanks Lexicon. Much apreciated.

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