-
Problem with showing and hiding buttons
Hi there,
I'm using CS5.5 and I want to set up a flash file so that a button will only show up when two other buttons have been clicked. I thought that I could do this with scoring (as I possibly want to scale up to lots of buttons) so I tried this code ('one' and 'two' are the buttons and 'answer' is the button that should show only once both one and two have been clicked).
import flash.events.MouseEvent;
var score:Number = 0;
one.addEventListener(MouseEvent.CLICK, clickone);
two.addEventListener(MouseEvent.CLICK, clicktwo);
function clickone(e:MouseEvent):void {
score += 1;
}
function clicktwo(e:MouseEvent):void {
score += 2;
}
if (score == 3) {
answer.visible = true;
}
else {
answer.visible = false;
}
The problem is, I can't get the answer button to ever appear (or disappear if I swap the visible false and true statements). Does anyone know what I'm doing wrong? Or could suggest a simpler way of doing this?
Thanks!
-
Dignitary
The if statement needs a trigger:
Code:
function clickone(e:MouseEvent):void {
score += 1;
checkIt();
}
function clicktwo(e:MouseEvent):void {
score += 2;
checkIt();
}
function checkIt():void{
if (score == 3) {
answer.visible = true;
}
else {
answer.visible = false;
}
}
-
Prid - Outing
EDIT: sorry, rynoe, had this tab open for a couple of hours
--------
The answer is simple, your if conditional code is static, it's only executed once, since it's just written standalone in your Frame, without any event listeners or such. So Flash only checks if score is equals to 3, once, and then is when you enter that Frame. What you can do, is to turn that if statement into a function, and call that function for each click, because Flash doesn't keep checking whether score is equals to 3!
A better way would be to make 2 boolean variables, and set them to true when both buttons have been pressed, and then check if both are equals to true, because with your current code, score will keep increasing, and if you press the first button twice, and then the second button, score will be equals to 4, so even if you have clicked both of the buttons, the third button will not show up, and if you press those two buttons, making the third one visible, pressing them again would result in the third button disappearing.
Actionscript Code:
import flash.events.MouseEvent;
var btn1:Boolean = false; var btn2:Boolean = false; checkBtn3();
one.addEventListener(MouseEvent.CLICK, clickone); two.addEventListener(MouseEvent.CLICK, clicktwo);
function clickone(e:MouseEvent):void { btn1 = true; checkBtn3(); }
function clicktwo(e:MouseEvent):void { btn2 = true; checkBtn3(); }
function checkBtn3(){ if (btn1 == true && btn2 == true) { answer.visible = true; } else { answer.visible = false; } }
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Dignitary
Actually, Nig, your code reflects coding standards much better
-
Prid - Outing
oh, thanks
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Thank you so much for your help, and for explaining the problem. This is exactly what I wanted!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|