Make Code Arcade Arrays(?) Uncaught e.findIdx is not a function

73 views Asked by At

I am working on a Microsoft Make Code Arcade Game in VS code using the Microsoft MakeCode Arcade Extension. While attempting to make enemies spawn automatically via a for loop, I ran into an error.

VS Code error msg:

    Uncaught e.findIdx is not a function
       at lvl1 (main.ts:105:1)
       at inline (main.ts:54:57)
       at runButtonEvents (pxt_modules/game/controllerbutton.ts:237:9)
       at inline (pxt_modules/game/controllerbutton.ts:266:45)
       at inline (pxt_modules/base/eventcontext.ts:32:59)

Make Code Arcade Website error msg:

    e.findIdx is not a function
      at lvl1 (line 118)
      at inline (line 57)
      at inline (line 6)

I believe this is caused by the array I was trying to use to create the variables for the sprites. But I do not know for sure.

Section of code Causing the problem:


    let enemys: any = []
    let enemyNumber: number
    for (let i: number = 0; i < 3; i++) {
        // enemy setup
        enemyNumber = i
        enemys[enemyNumber] = sprites.create(assets.image`Basic Enemy`, SpriteKind.Enemy)
        enemys[enemyNumber].vx = -30
    
        // enemy movment setup
        game.onUpdateInterval(200, function () { // evry 0.2 sec          
            if (enemys[enemyNumber].vx == 30) {  // if the player is traveling right
                if (!    (tiles.tileAtLocationIsWall(enemys[enemyNumber].tilemapLocation().getNeighboringLocation(CollisionDirection.Right).getNeighboringLocation(CollisionDirection.Bottom)))) { // if the tile to the below and to the right of the sprite is not a wall
                    enemys[enemyNumber].vx = -30 // make the sprite turn the other direction
                }
            } else {
                if (!        (tiles.tileAtLocationIsWall(enemys[enemyNumber].tilemapLocation().getNeighboringLocation(CollisionDi    rection.Left).getNeighboringLocation(CollisionDirection.Bottom)))) { // if the tile to the below and     to the right of the sprite is not a wall
                    enemys[enemyNumber].vx = 30 // make the sprite turn the other direction
                }
            }
        })
    }
    
    tiles.placeOnTile(enemys[1], tiles.getTileLocation(10, 14))
    tiles.placeOnTile(enemys[2], tiles.getTileLocation(20, 14))
    tiles.placeOnTile(enemys[3], tiles.getTileLocation(29, 11))

The git-hub repo can be found here. The Branch I am working on is called Code-(Colton) and can be found here. The error is caused in main.ts

I tried to comment out lines 115-135, but the error persisted. This may mean that something is a pre-defined asset, but I am verry lost. I do not know what the error message is telling me.

1

There are 1 answers

1
Comrade Bacon On BEST ANSWER

The error was caused by an incorrect definition of the array type. To solve this in Make Code, you simply chang the type to Sprite[] so make code knows the contet of the array is going to be sprites.

line one should now read: let basicenemys: Sprite[] = []

Though, after this another error presents itself. in some locations, make code does not recognize the item in the array as a sprite, or existing. I am loking into this, but will not elaberate furthure on this thread.