Skip to main content
Inspiring
May 5, 2014
Answered

Weird throw for indexOf with Array

  • May 5, 2014
  • 1 reply
  • 593 views

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.

This topic has been closed for replies.
Correct answer Ned Murphy

The indexOf function returns -1 for nothing found and the actual index of the item in the array when an item is found.

1 reply

Ned Murphy
Legend
May 5, 2014

`What is "caveLocations" and why are you expecting to only get -1 or 0?

Inspiring
May 6, 2014

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.

Ned Murphy
Ned MurphyCorrect answer
Legend
May 6, 2014

The indexOf function returns -1 for nothing found and the actual index of the item in the array when an item is found.