Be a Supporter!

function() tutorial

  • 2,759 Views
  • 2 Replies
New Topic Respond to this Topic
mikebud
mikebud
  • Member since: Sep. 19, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
function() tutorial 2006-03-16 18:56:12 Reply

.Intro
This confused me for a while, but I just created something really NEAT with the function tool. Most of you are familiar with the function by going to Flashkit.com and using the inventory script, you know you did it before, I did. It looks like:

function addToinven(item) {
item._x = eval(slot+currentslotnum);
item._y = eval(slot+ciurentslotnum);
}

Purpose

The best way to describe it is it makes your coding more flexible. The inventory system is a perfect example. in an RPG usually you have plenty of objects to gather, correct? Well you don't want to keep making hitTest() on every object in the whole RPG. So we substitute it in the function().

Example

I'm not giving you anything except the function code. If you know enough Flash you'll understand it and make your own engine out of this. Look at this code below:

function bubbleMove(currentFriend) {
bubble._x = currentFriend._x;
bubble._y = currentFriend._y;
bubble.talk = eval(currentFriend+"t");
}

The first line we always write the word "function" to tell the Flash player it is actually a function. The next word "bubbleMove" was a word I made to describe the function basically. If you have a RPG and your making the inventory you would name the function something like: function addToInventory() or function collect().

Inside the parenthesis we have currentFriend. Once again I named it that because I can. This is the part that will be subsituted by a movie-clip (MC) name/instance name.

Just to give an example if I told "Jay" to substitute currentFriend re-look the code.

function bubbleMove(Jay) {
bubble._x = Jay._x;
bubble._y = Jay._y;
bubble.talk = eval(Jay+"t");
}

We have the bubble x and y equaled to Jay x and y coordinates. Then we have bubble.tallk which is a dyanmic text box equal to Jay+"t" which comes out to the name Jayt.

How do we subsitute currentFriend?

Easy, just do it. For a hitTest you could do:

if (man.hitTest(Jay)) {
bubbleMove(Jay))
}

This says if the MC man hits the MC Jay we call a function. Notice I put bubbleMove? Except this time in parenthesis I put the name Jay. Now this just substituted everything in bubbleMove.

That is basically for an inventory like the one on Flashkit you would say do something like:

if (man.hitTest(runelong)) {
addToTheMotherdarninventory(runelong);
}

Easy huh?

More complexed code same thing though

Same engine except made for the use of my game:

var fu = new Array('friend1', 'friend2', 'friend3');
friend1t = "Call me Jay if you must, I created this awesome engine.";
friend2t = "I'm Mike, I wish to be Jay. Much jealous!";
friend3t = "I am just standing here because I was created to test to see if this engine worked.";
function bubbleMove(currentFriend) {
bub._x = currentFriend._x;
bub._y = currentFriend._y;
bub.talk = eval(currentFriend+"t");
}

The hitTest part:

for (z in fu) {
if (man.hitTest(this[fu[z]])) {
bubbleMove(this[fu[z]]);
}
}

Ending

I hope this helped some of you. I was fuzzy on it, but I get it now and I thought some noobs like myself would find this tutorial useful.

mynamewontfitin
mynamewontfitin
  • Member since: Sep. 1, 2003
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to function() tutorial 2006-03-16 20:47:32 Reply

Beautiful tutorial! Very helpful and imformative. Functions are a great way to keep organized.

mikebud
mikebud
  • Member since: Sep. 19, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to function() tutorial 2006-03-16 21:01:02 Reply

Thanks I think I could of been more informative maybe about it. But a topic like this probably shouldn't be to hard to understand so if they read it a couple times it should make sense. I hope XD