# Storing a function in an array

**URL:** <https://forum.kirupa.com/t/storing-a-function-in-an-array/259348>\
**Category:** flash\
**Created:** [May 3, 2008, 10:52pm UTC](https://forum.kirupa.com/t/storing-a-function-in-an-array/259348 "2008-05-03T22:52:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![superpwnage](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@superpwnage](https://forum.kirupa.com/u/superpwnage)\
**Post date:** [May 3, 2008, 10:52pm UTC](https://forum.kirupa.com/t/storing-a-function-in-an-array/259348/1 "2008-05-03T22:52:14Z")

</div>

The game I’m working on is a racing game where the track is generated randomly. But I’m having a problem, here’s the code I’m working on:

```
ActionScript Code:
[FONT=Courier New][LEFT][COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **function** [/COLOR] TrackContainer[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]:[COLOR=#0000FF]void[/COLOR][COLOR=#000000]{[/COLOR]
        trackDb=[COLOR=#000000][[/COLOR]
                 [COLOR=#000000]{[/COLOR]getObject:[COLOR=#000000] **function** [/COLOR] newTrackSquare[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#0000FF]return[/COLOR] [COLOR=#000000] **new** [/COLOR] TrackSquare[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];[COLOR=#000000]}[/COLOR], currX:[COLOR=#000080]0[/COLOR], currY:-[COLOR=#000080]50[/COLOR][COLOR=#000000]}[/COLOR],
                 [COLOR=#000000]{[/COLOR]getObject:[COLOR=#000000] **function** [/COLOR] newTrackLeftTurn[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#0000FF]return[/COLOR] [COLOR=#000000] **new** [/COLOR] TrackLeftTurn[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];[COLOR=#000000]}[/COLOR], currX:-[COLOR=#000080]50[/COLOR], currY:-[COLOR=#000080]50[/COLOR][COLOR=#000000]}[/COLOR],
                 [COLOR=#000000]{[/COLOR]getObject:[COLOR=#000000] **function** [/COLOR] newTrackRightTurn[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#0000FF]return[/COLOR] [COLOR=#000000] **new** [/COLOR] TrackRightTurn[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];[COLOR=#000000]}[/COLOR], currX:[COLOR=#000080]50[/COLOR], currY:-[COLOR=#000080]50[/COLOR][COLOR=#000000]}[/COLOR]
                 [COLOR=#000000]][/COLOR];
    [COLOR=#000000]}[/COLOR]

```

[/LEFT]  
[/FONT]

Basically, the idea is to create an array to hold **all** the track information. The important part of this array is the getObject function, which will help me build the track in this code-

```
ActionScript Code:
[FONT=Courier New][LEFT][COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **function** [/COLOR] updateTrack[COLOR=#000000]([/COLOR]nextTrack:[COLOR=#0000FF]Number[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]

```

[COLOR=#000000] **var** [/COLOR] nextTile; [COLOR=#808080]_// This will become the object returned by the getObject function_[/COLOR]  
[COLOR=#000000] **var** [/COLOR] getNextTile:[COLOR=#000000] **Function** [/COLOR];  
getNextTile=trackDb[COLOR=#000000][[/COLOR]nextTrack[COLOR=#000000]][/COLOR].[COLOR=#000080]getObject[/COLOR]; [COLOR=#808080]_// reference to the getObject function held in trackDb array_[/COLOR]  
nextTile=getNextTileCOLOR=#000000[/COLOR]; [COLOR=#808080]_// Perform getObject function to return a new track object_[/COLOR]  
addChildCOLOR=#000000[/COLOR]; [COLOR=#808080]_// puts the nextTile on the stage_[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[/LEFT]  
[/FONT]

But I get this strange error-

```
ActionScript Code:
[FONT=Courier New][LEFT]VerifyError: [COLOR=#0000FF]Error[/COLOR] [COLOR=#808080]*#1107: The ABC data is corrupt, attempt to read out of bounds.*[/COLOR]
at TrackContainer/updateTrack[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]

```

[/LEFT]  
[/FONT]

Anyway, what’s the best way to store a function in an array which returns a new object?  
Blah, hope that made sense. Any help is appreciated,  
Ben :puzzled:
