Skip to main content
Participant
March 24, 2012
Answered

How to exclude frame when randomising

  • March 24, 2012
  • 1 reply
  • 604 views

Hi, I am creating a 'make your own' style game which contains a randomise button. I have this code attached to the first keyframe of the movie clip at the moment:

MovieClip(parent).random_mc.buttonMode = true;

MovieClip(parent).random_mc.addEventListener(MouseEvent.CLICK, randomMouths);

function randomMouths(event:MouseEvent):void{

    visible = true;

    var pic_number : Number = 16;

    var randomFrame: Number = Math.ceil(Math.random() * pic_number);

    gotoAndStop(randomFrame);

}

However, I would like it to exclude the first frame as it's just the empty default frame. Is this possible? Thanks in advance.

This topic has been closed for replies.
Correct answer iPixelen

Thanks for the help, unfortunately now it's ONLY showing the empty frame with none of the other frames being randomised!

EDIT: Derp! Just realised the obvious solution - swapped around the if statements you gave and it worked! Thanks very much.

In case anyone's interested, here's the final code - I had to also put in a parenthesis at the end.

MovieClip(parent).random_mc.buttonMode = true;

MovieClip(parent).random_mc.addEventListener(MouseEvent.CLICK, randomMouths);

function randomMouths(event:MouseEvent):void{

    visible = true;

    var pic_number : Number = 16;

    var randomFrame: Number = randomF(2,pic_number);

    gotoAndStop(randomFrame);

}

function randomF(n1:int,n2:int):int{

    if(n1<2){

    return -1;

    } else {

    return n1+Math.floor((n2-n1+1)*Math.random());

    }

}

Message was edited by: iPixelen

1 reply

kglad
Community Expert
Community Expert
March 24, 2012

you can use:

MovieClip(parent).random_mc.buttonMode = true;

MovieClip(parent).random_mc.addEventListener(MouseEvent.CLICK, randomMouths);

function randomMouths(event:MouseEvent):void{

    visible = true;

    var pic_number : Number = 16;

    var randomFrame: Number = randomF(2,pic_number);

    gotoAndStop(randomFrame);

}

function randomF(n1:int,n2:int):int{

if(n1<2){

return n1+Math.floor((n2-n1+1)*Math.random());

} else {

return -1;

}

iPixelenAuthorCorrect answer
Participant
March 24, 2012

Thanks for the help, unfortunately now it's ONLY showing the empty frame with none of the other frames being randomised!

EDIT: Derp! Just realised the obvious solution - swapped around the if statements you gave and it worked! Thanks very much.

In case anyone's interested, here's the final code - I had to also put in a parenthesis at the end.

MovieClip(parent).random_mc.buttonMode = true;

MovieClip(parent).random_mc.addEventListener(MouseEvent.CLICK, randomMouths);

function randomMouths(event:MouseEvent):void{

    visible = true;

    var pic_number : Number = 16;

    var randomFrame: Number = randomF(2,pic_number);

    gotoAndStop(randomFrame);

}

function randomF(n1:int,n2:int):int{

    if(n1<2){

    return -1;

    } else {

    return n1+Math.floor((n2-n1+1)*Math.random());

    }

}

Message was edited by: iPixelen

kglad
Community Expert
Community Expert
March 25, 2012

no, that's a typo in my message and should be:

function randomF(n1:int,n2:int):int{

    if(n1<n2){

    return -1;

    } else {

    return n1+Math.floor((n2-n1+1)*Math.random());

    }

}