Problem with prototype

I’ve been trying to learn how to use prototypes recently, and I want to make one that resizes a box, with elastic action.

There is a button, and a box movieclip. What I want to happen is when you click the button, a prototype is run to resize the box. But, when I click the button, nothing happens. I feel I’m close, but lost :S

// Resizing prototype
 MovieClip.prototype.resizeBox = function(w,h) {
 	this.onEnterFrame = function() {
 		this.inertia = .6;
 		this.k = .5;
 	
 		this.wp = this.wp * this.inertia + this.w*this.k;
 		this.hp = this.hp * this.inertia + this.h*this.k;
 	
 		this._width += this.wp;
 		this._height += this.hp;
 	}
 }
 
 // Button
 _root.button1.onRelease = function() {
 	_root.box.resizeBox(500,300);
 }

What is wrong with the above code?

It’s because this.wp and this.w aren’t defined. Therefore you are adding NaN to this._width and this._height which results in no change. Make sure you define your variables before using them. I suppose you mean the arguments w and h by this.w and this.h ? But this.wp, I don’t know.

I was expanding on an elastic box thread I found, here.

I don’t know what he meant by wp either, but it worked. Yeah, I want the “w” and “h” to be provided through the argument.

This should do:


MovieClip.prototype.spring = function(iw,ih){
	var wp = 0;
	var hp = 0;
	var k = 0.1;
	var inertia = 0.9;
	this.onEnterFrame = function(){
		var w = iw-this._width;
		var h = ih-this._height;
		wp = wp*inertia+w*k;
		hp = hp*inertia+h*k;
		this._width += wp;
		this._height += hp;
	}
}

Right, but I’m trying to expand upon that, and have the w and h be defined by arguments in the prototype, no by the mouse.

// Resizing prototype
 MovieClip.prototype.resizeBox = function(w,h) {
 	var wp = 0;
 	var hp = 0;
 	var inertia = 0.6;
 	var k = 0.5;
 	this.onEnterFrame = function() {
 		wp = wp * inertia + w*k;
 		hp = hp * inertia + h*k;
 	
 		this._width += wp;
 		this._height += hp;
 	}
 }
 
 // Buttons
 _root.button1.onRelease = function() {
 	_root.box.resizeBox(500,300);
 }

Ehm … isn’t that what my code does ?

Whoa! What the heck, I swear the gosh I saw _root._xmouse…then I refresh, and it was iw…well then. Uhm. I must be going insane with boredom today :smiley:

You sure you didn’t edit your post really fast? o.0 Haha.

Haha, it works :slight_smile: Thanks for bearing with yet another n00b, I’m sure its made your day much brighter, haha. But seriously, thanks for your patience :slight_smile:

You’re welcome :slight_smile: