# AS3: method/function in class only called on one class instance?

**URL:** https://forum.kirupa.com/t/as3-method-function-in-class-only-called-on-one-class-instance/331132
**Category:** flash
**Created:** [May 24, 2013, 1:49pm UTC](https://forum.kirupa.com/t/as3-method-function-in-class-only-called-on-one-class-instance/331132 "2013-05-24T13:49:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Annergh](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Annergh](https://forum.kirupa.com/u/Annergh)
#### Post date: [May 24, 2013, 1:49pm UTC](https://forum.kirupa.com/t/as3-method-function-in-class-only-called-on-one-class-instance/331132/1 "2013-05-24T13:49:32Z")

</div>

[SIZE=2][COLOR=#000000]So I’m quite a noob to programming, so please bear with me.  
[/COLOR]  
[COLOR=#000000]I have a Neighbour class. In another class, I instantiate 3 separate instances of the Neighbour class, stored into an array (collisionArray\_Neighbour). There is a function (randomisePosition) in Neighbour class.  
[/COLOR]  
[COLOR=#000000]The problem is: the randomisePosition() function is only called by one Neighbour instance. Even though I iterate through the instances with an array, to call the Neighbour update() function on each. This update function calls the randomisePosition() function.[/COLOR]  
[COLOR=#000000]  
It may be due to the arrays, as if I remove an the one instance calling randomisePosition() from the array, by colliding with an instance in-game, another instance, but still only one out of the array, calls the function randomisePosition.[/COLOR]

This is the code that adds the instances to an array:[/SIZE]

```auto

[COLOR=#00008B]public[/COLOR] [COLOR=#00008B]function[/COLOR] populateNeighbourCollisionArray()
{[INDENT][COLOR=#00008B]for[/COLOR] ([COLOR=#00008B]var[/COLOR] iN:[COLOR=#00008B]int[/COLOR] = [COLOR=#800000]0[/COLOR]; iN < enemyMaxAmount; iN++) [/INDENT]
[INDENT]{ [/INDENT]
[INDENT=3] [COLOR=#00008B]if[/COLOR] (collisionArray_Neighbour.length < [COLOR=#800000]3[/COLOR]) [/INDENT]
[INDENT=3]{ [/INDENT]
[INDENT=4]collisionArray_Neighbour.push([COLOR=#00008B]new[/COLOR] [COLOR=#2B91AF]Neighbour[/COLOR]([COLOR=#00008B]this[/COLOR])); level01.addChild(collisionArray_Neighbour[collisionArray_Neighbour.length - [COLOR=#800000]1[/COLOR]]); [/INDENT]
[INDENT=3]} [/INDENT]
[INDENT]}[/INDENT]
}

```

[SIZE=2][FONT=arial]  
[COLOR=#000000]This is the randomisePosition function that seems to be only called on one Neighbour instance:  
[/COLOR][/FONT][/SIZE]

```auto

[COLOR=#00008B]private[/COLOR] [COLOR=#00008B]function[/COLOR] randomisePosition()
{[INDENT]trace([COLOR=#800000]"position randomised"[/COLOR]);    
[COLOR=#00008B]var[/COLOR] randomX = [COLOR=#2B91AF]Math[/COLOR].ceil(randomRange([COLOR=#800000]0[/COLOR], [COLOR=#800000]100[/COLOR]));    
[COLOR=#00008B]var[/COLOR] randomY = [COLOR=#2B91AF]Math[/COLOR].ceil(randomRange([COLOR=#800000]50[/COLOR], [COLOR=#800000]400[/COLOR]));    
[COLOR=#00008B]this[/COLOR].x = parentClassRef.player.x + randomX;    
[COLOR=#00008B]this[/COLOR].y = randomY; [/INDENT]
}

```
