Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Weird throw for indexOf with Array

Explorer ,
May 05, 2014 May 05, 2014

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.

TOPICS
ActionScript
483
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 05, 2014 May 05, 2014

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

Translate
LEGEND ,
May 05, 2014 May 05, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 05, 2014 May 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 05, 2014 May 05, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 05, 2014 May 05, 2014

Hmm.. So that means that my output was showing that it WAS working as it should?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 05, 2014 May 05, 2014
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines