A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Questions about how to use prototypes

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Posts
    233

    Questions about how to use prototypes

    1)
    Code:
    Color.prototype.fade = function() {
    	trace("asdf");
    }
    
    this.onEnterFrame = function() {
    	var col:Color;
    	col.fade();
    }
    I have this code in the first frame of my movie. When I try running it, I get the following error:
    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 16: There is no method with the name 'fade'.
    col.fade();

    2) Is it possible to have prototype functions defined in a seperate .as file and then imported/included into the main program? I've tried doing this a couple of times and I wither get a compilation error or the function doesn't get called when I try to use it.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    say you have a movieclip on stage, a yellow circle with instance name - ball
    using this code, you can change the color to red
    Code:
    Color.prototype.setTint = function (r, g, b, amount) { 
     var trans = new Object (); 
     trans.ra = trans.ga = trans.ba = 100 - amount; 
     var ratio = amount / 100; 
     trans.rb = r * ratio; 
     trans.gb = g * ratio; 
     trans.bb = b * ratio; 
     this.setTransform (trans); 
    }; 
    
    myColor = new Color (_root.ball); 
    myColor.setTint (255, 0, 0, 100);
    you can cut the code into a tint.as file and load it with -
    Code:
    // Flash -
    #include "tint.as"
    
    //tint.as -
    Color.prototype.setTint = function (r, g, b, amount) { 
     var trans = new Object (); 
     trans.ra = trans.ga = trans.ba = 100 - amount; 
     var ratio = amount / 100; 
     trans.rb = r * ratio; 
     trans.gb = g * ratio; 
     trans.bb = b * ratio; 
     this.setTransform (trans); 
    }; 
    
    
    function applyTintOverTime (r, g, b, amount, steps) { 
     var counter = 0; 
     var increase = amount / steps; 
     amount = increase; 
     _root.onEnterFrame = function () { 
      if (counter >= steps) { 
       delete this.onEnterFrame; 
      } else { 
       myColor.setTint (r, g, b, increase); 
       increase += amount; 
       counter++; 
      } 
     }; 
    
    } 
    
    
    myColor = new Color (_root.ball); 
    applyTintOverTime (255, 0, 255, 100, 20); 
    // the first three numbers are RGB values, 
    // then tint value, then no. of frames
    this will fade the color gradually

    hth

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