00:00
00:00
Newgrounds Background Image Theme

Arnahan just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

function() tutorial

3,579 Views | 2 Replies
New Topic Respond to this Topic

function() tutorial 2006-03-16 18:56:12


.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.

Response to function() tutorial 2006-03-16 20:47:32


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

Response to function() tutorial 2006-03-16 21:01:02


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