A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Action script question Prototype and movieLoader

  1. #1
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    Action script question Prototype and movieLoader

    What am I doing wrong here. This is pulled from a test for a larger project, here I have a movieclip mc1 with child movieclip called canvas to load an image into. So far so good. Now using the movieLoader I want when loadComplete to scale the clip mc1 down sort of a thumbnail representation of the image. Plan to add loading prograss bar next but When I call a prototype function from a movie that has been loaded this way it doesn't seem to work.
    code:

    movieClip.prototype.scale=function(amt){
    this._xscale=amt;
    this._yscale=amt;
    }
    movieClip.prototype.fadeDown=function(){
    this.onEnterFrame=function(){
    while (this._alpha>0){
    this._alpha-=1;
    }//end while
    }//end enter frame
    }//end

    mcLoader = new MovieClipLoader();
    listener = new Object();

    listener.onLoadComplete=function(target){
    target.scale(50)
    target._parent._rotation=5
    target._parent.fadeDown()


    }
    mc2.fadeDown;
    mcLoader.addListener(listener);
    mcLoader.loadClip("galleries/gallery1/balloons.jpg", mc1.canvas);

    Last edited by blanius; 08-18-2006 at 09:14 PM. Reason: forgot to include code

  2. #2
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    Bret, these are causing the problems.

    - The class should be MovieClip with a capital M .
    - mc2.fadeDown should be mc2.fadeDown()
    - the while construction should be an if construction otherwise the alpha value will already become 0 on the first enterFrame event.

    A little suggestion.

    When using an event function like onEnterFrame iside a prototype function it might be wise to store the original function and call it from within the new function otherwise the function will be replaced if it already exists. It's also best to clear the onEnterFrame function when the fade is completed so it doesn't take up cpu power anymore.

  3. #3
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Thanks man, Tired eyes from too many hours at the keys

  4. #4
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Quote Originally Posted by w.brants
    When using an event function like onEnterFrame iside a prototype function it might be wise to store the original function and call it from within the new function otherwise the function will be replaced if it already exists. It's also best to clear the onEnterFrame function when the fade is completed so it doesn't take up cpu power anymore.

    Could you show me an example of how I store the original function and clearing the function.

  5. #5
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    You can clear an onEnterFrame function by setting it to null .
    Storing is something like this
    Code:
    MovieClip.prototype.fadeDown=function(){
     if (!this.oldEnterFrame) this.oldEnterFrame = this.onEnterFrame;
     this.onEnterFrame = function(){
      this.oldEnterFrame();
      if (this._alpha > 0){
       this._alpha -= 1;
      } else {
       this.onEnterFrame = this.oldEnterFrame;
       this.oldEnterFrame = null;		
      }
     }
    }
    You copy the existing onEnterFrame function into oldEnterFrame (you may name it differently). You call that each onEnterFrame and your own routine. When the fade is finished, you copy back the old function. That way if the mc already had an onEnterFrame fuction it still will work. If there was no function, it will set back that state when the fade is finished so the onEnterFrame function is cleared.

  6. #6
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    I had wondered about the practice of setting onEnterFrame like this. Maybe it would be better to use a setInterval funtcion instead.

    I have several prototype things I'd like to use often like EaseTo fadeDown fadeUp and a few others I'm working on. These are things I use often so prototyping them seem to make sense. I was planning on making a set of these that I can include at the start of all my movies that need them.

  7. #7
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    Quote Originally Posted by blanius
    Maybe it would be better to use a setInterval funtcion instead.
    That might be a good choise.

  8. #8
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Ok this is what I've got now but it doesn't work I imagine I'm thinking it out wrong.

    Code:
    MovieClip.prototype.fadeUp=function(){
    
    	this.doFade=function(){
    
    		if (this._alpha<100){
    
    				this._alpha+=10;
    
    				}
    		if (this._alpha>=100){
    			 clearInterval(this.intervalID);
    			 }
    
    		}//end do fade function
    		this.intervalID=setInterval(doFade,50);
    	}//end

  9. #9
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    Try this
    Code:
    this.intervalID = setInterval(this,'doFade',50);

  10. #10
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    This is working great.. one more question though. If I try to have both fadeUp and fadeDown functions only fadeUp seems to work.
    Code:
    MovieClip.prototype.fadeDown=function(){
    
    	this.doFade=function(){
    
    		if (this._alpha>0){
    
    				this._alpha-=10;
    
    				}//end if
    	if (this._alpha<=0){
    			 clearInterval(this.intervalID);
    			 }
    		}//end do 
    		this.intervalID=setInterval(this,'dofade',50);
    	}//end
    
    MovieClip.prototype.fadeUp=function(){
    
    	this.doFade=function(){
    
    		if (this._alpha<100){
    
    				this._alpha+=10;
    
    				}
    		if (this._alpha>=100){
    			 clearInterval(this.intervalID);
    			 }
    
    		}//end do fade function
    		this.intervalID=setInterval(this,'doFade',50);
    	}//end

  11. #11
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    The fadeDown function works for me.
    Here's another code example I just created. It addes two properties to a movieclip. _alphastep and _2alpha. When you set a value to _2alpha it will automatically fade to that alpha value. _alphastep controls the speed.
    Code:
    // declare local variables g and s
    
    var g,s;
    
    // add $fnInterval internal function
    
    MovieClip.prototype.$fnInterval = function(){
     if (this.$2alpha > this._alpha){
      this._alpha = Math.min(this._alpha + this.$alphastep, this.$2alpha);
     } else {
      this._alpha = Math.max(this._alpha - this.$alphastep, this.$2alpha);
     }
     if (this.$2alpha == this._alpha){
      clearInterval(this.$iID);
      this.$iID = null;
     }
    }
    
    // implement _alphastep property
    
    g = function(){ return this.$alphastep; }
    s = function(v){ this.$alphastep = Math.max(0.1,Math.min(v,100)); }
    MovieClip.prototype.addProperty('_alphastep',g,s);
    
    // set an initial value for _alphastep
    
    MovieClip.prototype.$alphastep = 5;
    
    // implement _2alpha property
    
    g = function(){ return this.$2alpha; }
    s = function(v){
     this.$2alpha = Math.max(0,Math.min(v,100));
     if (!this.$iID){
      this.$iID = setInterval(this,'$fnInterval',40);
     }
    }
    MovieClip.prototype.addProperty('_2alpha',g,s);
    
    // delete local variables g and s
    
    delete g;
    delete s;
    
    
    // test the code assuming you have 2 movieclips
    // named mc1 and mc2
    
    mc1._alphastep = 1;
    mc1._2alpha = 60;
    
    mc2._alphastep = 3;
    mc2._2alpha = 10;
    Last edited by w.brants; 08-23-2006 at 12:10 PM.

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