Skip to main content
Known Participant
May 11, 2011
Question

Continiuty of a test without adding multiple frames

  • May 11, 2011
  • 2 replies
  • 604 views

Hi. my question is a bit about logic rather then code oriented. sorry but if ı can ,let me explain it.

for instance I have head or tail game. a coin first comes to the screen as head or coin not visible. but program has assigned already for its a game.

and I have two buttons, head and tail. it asks head or tail. I push one of the buttons. (up to here I can handle all the codes). but now if I guess it true, I want the program to ask it again. (I can also handle the code up to here, if I push the timeline for example 100 key frames, as knowing 100 over and over is almost impossible).

But by which code I can make the test continue Without multiple keyframes.

Thnx in advance and for your previou very helpfull answers. br,

This topic has been closed for replies.

2 replies

Inspiring
May 12, 2011

You need to write at least two distinct functional blocks:

1. coin tossing

2. guessing.

To start you with it, below is a code that accomplishes random tossing. Create a new fla, place button on timeline and name it tossBtn.

When you click button you will observe outcome (head or tail).

Read comments:

stop();
// make button on stage with name tossBtn
// assign listener to this button
tossBtn.addEventListener(MouseEvent.CLICK, tossCoin);
// it is either head or tail
// the following variable holds if it is head or not
// obviously, if it is not head - it is tail
var isHead:Boolean = false;

// this function randomly assigns value to isHead
function tossCoin(e:MouseEvent):void {
     // the rule is that if random number is greater than 0.5
     // it is head, otherwise it is not - thus it is tail
     // this way chances for heads and tails are almost equal
     isHead = Math.random() > 0.5;
     // output result
     trace(isHead ? "head" : "tail")
}

Ned Murphy
Legend
May 11, 2011

You shouldn't need any frames for this other than the first.  Each time you click your guess, have code that processes the guess and then randomly chooses the next state of the coin.

ohamsiciAuthor
Known Participant
May 11, 2011

Ned, excuse I am just a very beginner. I can write a code to do it once. but for continuty, which commands specifically can help me.  For example,

as much as I understand;

var starter:Boolean;

var i:int;

i= MathFloor(MathRandom()*1 // to determine head or tail

starter=false; // to ignite the function which will work every time starter is false

function headortail ():void {

if(starter=false){

coin.color = black;

}

}

head_button.addEventListener (MouseEvent.CLICK,

....

Anyway, let me try what you direct me to do tomorrow. What I understand is there no specific command for  problem but conventional coding is enough..

thnx;

Ned Murphy
Legend
May 12, 2011

Aside from just being wrong code, the following will only return a value of zero due to Math.random() returning a value >= 0 but < 1

i= MathFloor(MathRandom()*1