PDA

View Full Version : [fmx2004]Function Coolio



InsaneMonk
January 27th, 2004, 07:17 PM
I found a great way to make simple movement. (You can add other arguments if you'd like):
in teh frame:
function movement(object, key, xspeed, yspeed) {
if (Key.isDown(key)) {
object._x += xspeed;
object._y += yspeed;
}
}

in teh movie clip:
//move right
movement(this, Key.RIGHT, 5, 0);

u can move in any direction by changeing the key and x & y speeds! :)

senocular
January 27th, 2004, 07:30 PM
Thats cool :D ...but it requires 4 calls for each direction of movement. You can simplify things by doing something like this:


// defined ahead of time in the main timeline
MovieClip.prototype.keyMovement = function(xspeed, yspeed){
if (arguments.length == 1) yspeed = xspeed;
this._x += (Key.isDown(Key.RIGHT) - Key.isDown(Key.LEFT)) * xspeed;
this._y += (Key.isDown(Key.DOWN) - Key.isDown(Key.UP)) * xspeed;
}

// for the movieclip
onClipEvent(enterFrame){
this.keyMovement(10);
}

You have the option of using two values for speed or just one and it will work for both. Because it was defined as a movieclip prototype function, it means that movieclips use it in the form of this.functionName() and the this or the movieclip wont need to be passed in as an argument. It also means you'll never have to target the function because its accessible directly from 'this' itself.

:)

InsaneMonk
January 27th, 2004, 07:33 PM
um... i know nothing about prototypes. could you please explain that to me? wtf did those line about subtracting key.right - key.left? that makes no sense to me...