Copy link to clipboard
Copied
I am developing an application that spawns a world, and there are caves in the world, I am trying to check if there is a cave block(blank area) above specified coordinates which are in my array, I am checking if indexOf(coordinates) are equal to current coordinates - 1 Ycoordinate. Here is my code: (things I am talking about occur in lines 4-14.)
private function checkCave():void
{
if (caveAlready == false)
{
var blockYMinus = (blockY - 1)
var aboveIndex = ""
aboveIndex = i + "," + (blockYMinus)
var caveAbove = ""
caveAbove = caveLocations.indexOf(aboveIndex)
trace(caveAbove)
if (caveAbove == 0)
{
caveAlready = true; trace("LOCATION WITH CAVE ABOVE: " + i + "," + blockY)
}
}
if (caveAlready == false)
{
var caveRandom:Number = Math.round(Math.random() * 2000 + (caveSpace * .5));
if (caveRandom <= 30)
{
Global.vars.cave = true;
caveAlready = true;
}
}
else
{
caveRandom = Math.round(Math.random() * 33);
if (caveRandom <= 30)
{
Global.vars.cave = true;
caveAlready = true;
caveLocations.push(i + "," + blockY);
}
else
{
Global.vars.cave = false;
caveAlready = false;
}
In line 11, I trace the value of caveAbove, which should only be -1 or 0, but in my output, I get these values and many more up into the thousands:
-1
-1
-1
-1
-1
-1
-1
-1
-1
0
LOCATION WITH ABOVE: 36,10
-1
-1
-1
-1
-1
-1
15
30
31
32
33
34
35
36
37
38
39
40.
My main concern is that I only got a throw of 0 once? And cannot seem to get it more than once... I don't know if I am using this indexOf properly, so please point me in the right direction.
The indexOf function returns -1 for nothing found and the actual index of the item in the array when an item is found.
Copy link to clipboard
Copied
`What is "caveLocations" and why are you expecting to only get -1 or 0?
Copy link to clipboard
Copied
Cave locations was the locations of other cave spawn coordinates, I was expecting a 0 or -1 because apparently it dispatches those numbers to tell you if that indexed search exists. But I found out that I needed to do: caveAbove === caveLocations.indexOf(aboveIndex) 3 equals signs.
Copy link to clipboard
Copied
The indexOf function returns -1 for nothing found and the actual index of the item in the array when an item is found.
Copy link to clipboard
Copied
Hmm.. So that means that my output was showing that it WAS working as it should?
Copy link to clipboard
Copied
The indexOf usage appeared to be working as it is supposed to based on the type of output you say you got. I can't say if your output was producing the result you intended it to.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now