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 ViewsSo I've been trying to make a combo system in my game, and I need to check the current combo.
The current combo are stored in array of integers. And I quickly realized that I can't compare arrays anything like I want to.
var a:Array = [1, 2, 3];
trace(a == [1, 2, 3]);
That come out false for a reason I don't understand, so how would I properly compare an array?
Gotta loop through each array and check each value individually:
equal = true;
for (i=0; i<a.length;i++)
{
if (a[i] != b[i])
{
equal = false;
break;
}
}
if (equal)
{
trace ("yo arrays are crunk fo shizzle");
} Well that's going to be annoying with like 20 combos.
At 8/4/11 01:29 AM, MintPaw wrote: Well that's going to be annoying with like 20 combos.
Nah, just use a multi-dimensional array to store your combos, then loop through that. Some pseudocode off the top of my head on what it might look like:
var comboArr:Array = [[1, 2, 3], [2, 1, 3], [3, 2, 1]]
var comboToCheck:Array;
for each(var combo:Array in comboArr){
for(var i = 0; i < combo.length; i++){
if (comboToCheck[i] != combo[i]){
//do your check here
//if combo matches, save it and execute after loops or something Arrays are passed by reference, not by value.
Comparing references will figure out if both reference the same object, not if the members of each object have the same values.
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* comparing arrays
* @author milchreis
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var a:Array = [1, 2, 3];
var b:Array = [1, 2, 3];
var c:Array = ["a", "b", "c"];
var d:Array = [1, 2, 3, 4];
trace (areEqualArrays(a, a));
trace (areEqualArrays(a, b));
trace (areEqualArrays(a, c));
trace (areEqualArrays(a, d));
trace (areEqualArrays(c, d));
}
private function areEqualArrays(a:Array, b:Array):Boolean
{
if (a == null || b == null) return false;
var l:int = a.length;
if (l != b.length) return false;
for (var i:int = l - 1; i >= 0; --i)
{
if (a[i] != b[i]) return false;
}
return true;
}
}
} I know this isn't quite the answer you were looking for, but I'm thinking that using arrays for comparison at run time is going to be a bit processor intensive. An alternative approach would be to use something similar to this to store the combinations: http://en.wikipedia.org/wiki/Trie#As_rep lacement_of_other_data_structures Searching the tree should much faster than comparing against each array. You can still store each combination into an array at compile time and load the tree from them at runtime.
The Internet is like a screwdriver. You can use it to take an engine apart and understand it, or you can see how far you can stick it in your ear until you hit resistance.
At 8/4/11 03:23 AM, milchreis wrote: Arrays are passed by reference, not by value.
Ah ok, I get it now, I'm just pretty much going to steal the function you got there.