PDA

View Full Version : Returning more than 1 variable


M3wThr33
01-17-2002, 07:24 AM
See that signature below this text? I've been working on it for a while. Anywho, I've been working to get that Aquarium customizable for quite some time. How do I get a function to return two variables...? It's not the same as C++!

The function in question:

function dirCalc (fishM, fishie) {
fishM._dist--;
if (fishM._dist < 1) {
fishM._dist = random(30);
fishM._dir = random(360);
fishM._xV = Math.cos(Math.PI/180 * fishM._dir);
fishM._yV = Math.sin(Math.PI/180 * fishM._dir);
}
if (((fishie._x > 0)&&(fishM._xV < 0))||((fishie._x < 500)&&(fishM._xV > 0))) {
setProperty (fishie, _x, fishie._x + fishM._xV);
}
if (((fishie._y > 0)&&(fishM._yV < 0))||((fishie._y < 30)&&(fishM._yV > 0))) {
setProperty (fishie, _y, fishie._y + fishM._yV);
}
}

I need to have both fishM and fishie returned in their new forms.
Right now the calculation may seem excessive, but I am also adding rotation and other nifty stuff. Right now, it just doesn't work!

Jesse
01-17-2002, 08:51 AM
Use an array or an Object.
function myFunction (val) {
return [val,val+10]
}
trace(myFunction(10)[0])
trace(myFunction(10)[1])
// or
answer = myFunction(10)
// This one is better because it runs the function only once.

M3wThr33
01-18-2002, 02:26 AM
I see how that works, but that leads to another problem.
The movie clip I am using is called ' "fish" + fishCount ' so I can't use the "=" to set it without using reference in the function.

Jesse
01-18-2002, 06:11 AM
Sorry I don't understand what you mean. Why can't you use equals? Have you seen the Advanced Pathing tutorial?

M3wThr33
01-18-2002, 07:07 AM
*checks tutorial*
AHh! Just what I needed! Thank you so much!

M3wThr33
01-19-2002, 08:01 AM
Okay. Everything is fine and dandy, but I have no clue what path to use for this thing. Maybe you can figure it out. I left the area that it needs help in ^^^^^^^^^'s. Frame 7, or whatever the last one is, 3rd layer inside Aquarium, 2nd frame's actions. *tears out hair* So close to being just what I need!

Jesse
01-20-2002, 12:54 AM
Your function is somewhat eird and expects the instanc ename as an expression instead of a string, so the trick was to make the value you're passing an expression using eval():
this["fish" + x] = dirCalc(eval("fish" + x));
Took me a whie to get though :) Find attached the working version.

M3wThr33
01-23-2002, 05:48 AM
As you can tell, it's working just peachy now! Thank you.

Actually, I added a depth to the aquarium with 3 levels of fishes. Unfortunately, Mario is always on the bottom depth so he looks out of proportion(Not uploaded in that bar yet). Every time I use swapDepth to make him go to the appropriate level, he goes, but is still at the old depth. How can I make him start, at say depth of 3251?

Jesse
01-25-2002, 01:20 AM
Attach him using attachMovieClip. See the tutorial in the Beginner section.