Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 Viewswhy can't i do something like:
carryFunc(enemiesArray[i]);
function carryFunc(enemiesArray[i]){
bla bla bla
}
how do i get around this??
EDIT: sorry this is the code
carryFunc(enemiesArray[i]);
function carryFunc(enemiesArray[i]){
_root.enenemiesArray[i].health._xscale -=5 ;
} Are you trying to pass your array into your function as a parameter?
It can be done like this:
array = new Array();
//fill your array here, then when you want to call your function
function funcName (array)
{
//function code here
}
You can pass in as many parameters as you like, as long as they are separated by a comma. If you want your manipulations to exist outside the function, be sure to return the value. I'd recommend you google functions, as this should be basic stuff.
At 5/3/12 08:36 PM, denmen7 wrote: EDIT: sorry this is the code
carryFunc(enemiesArray[i]);
function carryFunc(enemiesArray[i]){
_root.enenemiesArray[i].health._xscale -=5 ;
}
Ah, didn't see this.
carryFunc(enemiesArray, 2);
function carryFunction(enemyArray:Array, i:int)
{
_root.enemyArray[i].health._xscale -=5;
}
You have to pass the entire array into the function, and the index(i) that you are trying to access must also be passed in separately.
so i just have to separate the array name itself, from the [i]??
seems easy enough, how come thou??
Functions only accept addresses of memory locations as parameters. So when you create a variable...
var x:int = 0
The computer automatically creates a block in your ram to store (in this case) an int, which is equal to a four bit block. So, for simplicity, lets assume the bits are 0x000001, 0x000002, 0x000003, and 0x000004.
x stores the location, 0x000001.
When you access x, it points the computer to that location, to retrieve the stored value. In this case, the stored value is 0.
Say you create an array of 10 integers.
var a:Array = {1,2,3,4,5,6,7,8,9}
This is equal to 40 bits(or something). Same as before, the computer reserves those blocks in your ram.
a points to the beginning of this block, with it's table of contents. (Most languages have each array index automatically point to the value, so a[1] is and address, a[2] is an address, but actionscript is different.) So, accessing a[5] points to the table of contents, looks up what the 5th index is and returns the value.
So in actionscript, x will return an address to the computer. a[5] will not return an address. It will return a value it received from the table of contents.
At 5/3/12 10:22 PM, denmen7 wrote: so i just have to separate the array name itself, from the [i]??
seems easy enough, how come thou??
No true.
You can pass an array element to a function...
var a = new Array(x, y, z);
function func(a:Type){
a.doWhatever
}
Your actual problem is that you don't understand how functions, function calls, and function declarations work.
Functions:
1. Put something in, get something out (Not always, but ALMOST always)
2. Are declared generically
What so i mean by Declared generally?
Say i'm stupid enough to want to make an "add function" that does the + operation on 2 numbers pf my choosing.
I would declare the function with the name i want. anything at all, but maybe something cool like "myAddFunction", in the ()'s, you need to add how many EXDPECTED inputs are going to be passed in, and their type.
So If i knew at what some point, i know that i want to add the nubers 10, and 23 together, I WOULD NOT DO:
myAddFunction(10, 23){
return 10 + 23;
}
INSTEAD, I would declare it generically buy parameter type. Since I know I'm going to use 2 Integers as inputs, I need to give them arbitrary names to use in the function, like 'a' and 'b' Like this:
myAddFunction( a:int, b:int ){
return a + b;
}
Now i can reuse this function all i want. i would call it like below. Realize that whatever you pass first gets assigned as a, and the other is assigned to b.
x = 10;
y = 23;
z = 7;
myAddFunction(x, y); // Return --> 23
myAddFunction(x, z); // Return --> 17
myAddFunction(x, x); // Return --> 20
myAddFunction(10, 5); // Return --> 15
myAddFunction(x, 10); // Return --> 20
I hope you have a better understanding of functions as a whole now.