JCole
JCole

Reputation: 13

Cannot push past 1 element to basic AS3 Array

I am very new to this and hoping it's something that should have been obvious.

When I run the code below, the Array newHole and newArray both return 1 on the trace. Originally the code was built with only the newHole array, but I created the newArray in the hopes of troubleshooting. It did not help. The class for bulletHole contains no extra code so I didn't post that.

Thank you.

import flash.display.*;
import flash.events.*;
import flash.ui.Mouse;

Mouse.hide();

var myReticle:MovieClip;
var holeArray:Array = new Array();
var randomHole:Number = randomNumber(1, 5);
var newHole:bulletHole = new bulletHole();
var newArray:Array = new Array();

stage.addEventListener(MouseEvent.MOUSE_MOVE, followReticle);
stage.addEventListener(MouseEvent.CLICK, myFire);
stage.addEventListener(MouseEvent.CLICK, checkCount);

function followReticle(event:MouseEvent):void
{
    myReticle.x = mouseX;
    myReticle.y = mouseY;
}

function myFire(int):void
{
    stage.addChild(newHole);
    newHole.x = myReticle.x;
    newHole.y = myReticle.y;
    //holeArray.push(newHole);
    newHole.gotoAndStop(randomHole);
    //trace(holeArray.length);
}

function checkCount(int):void
{
    newArray.push("A");
    trace(newArray.length);
}

function randomNumber(low:Number=0, high:Number=1):Number
{
    return Math.floor(Math.random() * (1+high-low)) + low;
}

Upvotes: 1

Views: 64

Answers (2)

AntBirch
AntBirch

Reputation: 272

This line is incorrect:

function myFire(int):void {

Because the function is triggered from a mouse event listener it should read:

function myFire(e:MouseEvent):void {

What you are doing is passing a undefined int to the function. Hope this helps.

EDIT: You should delete the clickCount event listener and function as they're not needed.

Also notice that you should move this line to the top of your myFire function or else you will keep replacing this MovieClip instead of creating it again:

var newHole:bulletHole = new bulletHole();

Upvotes: 0

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Most likely the issue is that the code you've posted is running over and over again. In other words, you have a looping timeline that eventually goes back to the frame that the code you've shown is on.

Whenever that frame is reached, you have the following:

var holeArray:Array = new Array();

Which creates a new array replacing what used to be in that var.

To solve this, you either need to:

  • take the code out of the timeline (put it in a class file and attach that as the document class of your project)
  • re-architect your timeline so the first frame is only reached 1 time
  • put some checks in so that the code only runs the first time the frame is reached.

Here is an example of the latter option:

//just define the array, don't create it
var holeArray:Array;

//if the array is null, create it (it will only be null the first time this code is run
if(!holeArray){
    holeArray = new Array();
}

Upvotes: 1

Related Questions