user1587341
user1587341

Reputation: 5

A little help here with Action Script 3

Well to cut the chase, I believe that this function is causing my problem.

function getRandomLabel():String {
    var labels:Array = new Array(24,62,49,74,37);
    var index:Number = Math.floor(Math.random() * labels.length);
    return labels[index];
}
this.gotoAndPlay(getRandomLabel());

This function is used for randomization of the 5 digits above (24,62,49,74,37) So that my moles (5 moles) will appear randomly. Currently working on a whack-a-mole flash game

Current scene: 2 out of 3

Somehow, when I test play it it loops back to scene 1 frame 1.

One more thing, the problem started when I tried to put the "game proper" into scene 2 where the rest of my codes are in (menu, mechanics, etc.)

Upvotes: 0

Views: 155

Answers (1)

davidbuzatto
davidbuzatto

Reputation: 9444

As Andrew said, you don't have asked nothing. You just expose what you have but did not what is happening. Well, your function does not have problem. I'm pretty rusty with AS, but I would write it this way:

function getRandomFrame( frameNumbers: Array ): int {
    return frameNumbers[Math.floor( Math.random() * frameNumbers.length )];
}
gotoAndPlay( getRandomFrame( [24, 62, 49, 74, 37] ) );

You are getting frame numbers not labels, so I changed its name. Since you are calling gotoAndPlay, your movie "play head" will go to the selected frame and it will be played until it reaches the end of the timeline, and the movie, by default, will start over. If you need to stop the animation, you need to set an stop in the desired frame. Try your code with gotoAndStop instead of gotoAndPlay. The movie will go to the random frame and it will stop. If you need to get frame labels, create an array of Strings:

function getRandomLabel( labels: Array ): String {
    return labels[Math.floor( Math.random() * labels.length )];
}
gotoAndPlay( getRandomLabel( ["car", "dog", "music"] ) );

It seems that you are a Flash newbie, so you need to study more the tool that you are dealing with ;) The ActionScript default version in Flash CS5 is the third.

Upvotes: 1

Related Questions