Change Variable on Mouse Event - actionscript-3

I'm trying to change the variable that's set to 0 into 1 on a mouse event.
I have 3 movie clips 'maskedbgmc' and when clicked its supposed to change a variable.
But it doesn't change the variable as far as i can see. What do i miss?
Thanks for your time :)
var checkCard1:Number = 0;
maskedbg_mc.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked1);
function cardChecked1 (event:MouseEvent):void {
checkCard1 = 1;
}
var checkCard2:Number = 0;
maskedbg_mc2.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked2);
function cardChecked2 (event:MouseEvent):void {
checkCard2 = 1;
}
var checkCard3:Number = 0;
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
}
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}

Note that events happen asynchronously. This means that your if statements will execute the moment this code is interpreted, but the variables will only be modified when the mouse events occur. If you trace your variables value inside your event handler functions, you will likely see that it does indeed change when the mouse button is pressed.
Maybe what you want to do is add your if statements into a function, like so:
function checkCards() : void {
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}
You can then invoke this method inside your event listeners, and it will check the card variables using the above logic. An example of it used inside the cardChecked3() method:
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
checkCards();
}
Hope this helps.

Does the event have to be a MouseEvent.MOUSE_DOWN? If you are only interested in clicks I would try changing that to a MouseEvent.CLICK.
Just a thought tho.

Try changing your code to this for the last part
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}

Related

how to disable movieclip?

I am using Adobe Flash CS5, Action Script 3.0. I want to disable my movie clip. How to do that?
My code:
var index:Array = ([0,1,2,3]);
var radioGroup1:RadioButtonGroup = new RadioButtonGroup("question1");
rb1.group = radioGroup1;
rb2.group = radioGroup1;
rb3.group = radioGroup1;
rb4.group = radioGroup1;
b1.addEventListener(MouseEvent.CLICK, submitclick,false);
function submitclick(event:MouseEvent):void
{
if (radioGroup1.selectedData == null)
{
b1.mouseEnabled = false;
//ExternalInterface.call("Test","Please selecte one answer");
//return;
} else {
if (radioGroup1.getRadioButtonIndex(radioGroup1.selection) == 2)
{
myscore += 10;
}
}
if (compquest>=maxquest)
{
gotoAndStop(1,"Scene 6");
}
else
{
MovieClip(this.root).gotoAndStop(1, "Scene " + questions[compquest++]);
}
}
I want to disable this b1 movieclip. So anyone could tell me what am I doing wrong?
If, by disable, you mean to make it unclickable and faded like a disabled button. Just reduce the alpha a bit and set its mouseEnabled property to false.
function submitClick(event:MouseEvent){
b1.alpha = .6;
b1.mouseEnabled = false;
}
This way the user will not be able to click it and it appear disabled.
First you could write, because event.target is p1:
function submitclick(event:MouseEvent):void
{
event.target.mouseEnabled = false;
}
On the other hand, the condition you want to evaluate in your if statement is probably false:
radioGroup1.selectedData == null
Therefore the associated code isn't executed:
b1.mouseEnabled = false;
You should test in your function and before your if statement:
trace(radioGroup1.selectedData == null);

Verify if buttons are clicked actionscript3

I have 9 movieclips, all with their function and i want to show a message after the user clicked on all the movieclips.
EX:
button1.addEventListener(MouseEvent.CLICK, showText1);
button2.addEventListener(MouseEvent.CLICK, showText2);
button3.addEventListener(MouseEvent.CLICK, showText3);
function showText1(e.Event)
{
text1.visible=true;
}
...
How do i check if all the buttons are clicked and after that, show a message?
Thank you.
Call a function like test on each click. In that function do something similar to:
function test() {
for (var i:uint = 0; i < 9; i++) {
if (this["text" + i].visible != false) { // do proper check; maybe visible, or maybe some variables - whatever fits your needs
return false;
}
}
return true; // do whatever you want here - all are 'clicked';
}

AS3 - Removing event listener failed

here's my code, i don't know why it doesn't remove the event listener.
What i want to do is having some button to turn the sound on and off according to the beat. On the first click it worked perfectly, but when i want to remove the sound on the second click it cannot remove the event listener.
plat.BEAT1.addEventListener(TouchEvent.TOUCH_BEGIN, BEATDown(plat.BEAT1, hihatz, 2));
plat.BEAT2.addEventListener(TouchEvent.TOUCH_BEGIN, BEATDown(plat.BEAT2,cymbalz, 4));
function BEATDown (padNum, sounz, tiempo) {
return function (e:TouchEvent) {
var currentSound:Sound = null;
var currentSoundChannel:SoundChannel;
var active:int;
if (padNum.currentFrame == 1) {
padNum.gotoAndStop(3);
padNum.addEventListener(Event.ENTER_FRAME, PlayBeats);
active = 1;
} else {
padNum.gotoAndStop(1);
padNum.removeEventListener(Event.ENTER_FRAME, PlayBeats);
active = 0;
}
function playSound(sound:Sound):void
{
if (active == 0)
{
// Stop playing ANY sound
currentSound = null;
currentSoundChannel = null;
}
else
{
// Play a different sound
currentSound = sound;
currentSoundChannel = sound.play();
}
}
function PlayBeats(event:Event):void
{
if (tiempo == 1) {
if (fl_SecondsElapsed <= 4) {
playSound(sounz);
}
}
if (tiempo == 2) {
if (fl_SecondsElapsed == 1 || fl_SecondsElapsed == 3) {
playSound(sounz);
}
}
if (tiempo == 4) {
if (fl_SecondsElapsed == 1) {
playSound(sounz);
}
}
}
}
}
EDIT:
The listener i want to remove is padNum.addEventListener(Event.ENTER_FRAME, PlayBeats);
plat.BEAT1 is the button instance. I use multiple instance to trigger different sound, each toggle sound on and off according to the Tiempo count.
I would say it's because you don't add / remove your listener on the same object;
is plat.BEAT1 et plat.BEAT2 are the same? I guess not.
I would recommend you to try by adding the EnterFrame listener on stage on this or on the parent element to the if the event is Removed correctly. Then if you want your object works independently, try to target the right object.
if (padNum.currentFrame == 1) {
padNum.gotoAndStop(3);
stage.addEventListener(Event.ENTER_FRAME, PlayBeats);
active = 1;
} else {
padNum.gotoAndStop(1);
stage.removeEventListener(Event.ENTER_FRAME, PlayBeats);
active = 0;
}

else statement always run even the answer is true

tnx! it works. but when i tried to add another object named, p1_2 and add the "trace" thing to the code,
it goes back to the same problem.
p1_1.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
question.text = "shape?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss);
function onClickss(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "circle")
{
p1_1.visible = false;
}
else
{
gotoAndStop(6);
}
}
p1_2.addEventListener(MouseEvent.CLICK, onClick2);
function onClick2(e:MouseEvent):void
{
question.text = "Color?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss2);
function onClickss2(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "red")
{
p1_2.visible = false;
}
else
{
gotoAndStop(6);
}
}
what should i do.? do i nid to seperate p1_2 in another frame
and make another inputtextfield for it?
im planning to add 5 objects on the stage. until p1_5. -_-
Before the line...
if(ans.text == "circle") {
...add:
trace("ans.text = "+ans.text);
Clicking the submit button will trace the actual value of ans.text to the output panel when you test your movie in Flash. If you get a null reference error then ans has not been instantiated. Otherwise, knowing the actual value of ans.text should point you in the right direction to solve the problem.

to create quiz using StartDrag and stopDrag

Actually i have a quiz.fla. In the file ,two of them fill inthe blank questions and others are
multiple questions.
square1_mc must run only once not twice. İf user correct selected, doesnt run it again.
However,if mybadscoretext is 1 not increase 2,3,4. :S
how i can do all?
stop();
var myScore:Number = 0;
var myBadScore:Number=0;
square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
square1_mc.stopDrag();
if (square1_mc.hitTestObject(square2_mc)== true)
{
square1_mc.x=129;
square1_mc.y=133;
this.graphics.clear();
this.graphics.lineStyle(2, 000000);
this.graphics.moveTo(square1_mc.x, square1_mc.y);
this.graphics.lineTo(square2_mc.x, square2_mc.y);
this.graphics.endFill();
myScore += 1;
score_txt.text=String(myScore);
square1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
}
else
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
}
}
Add a variable to keep track of whether the bad score has been added:
var badMarked:Boolean = false;
function drag(e:MouseEvent):void
{
e.target.startDrag();
badMarked = false;
}
[...]
function drop(e:MouseEvent):void
{
if (square1_mc.hitTestObject(square2_mc)== true)
{
[...]
}
else if ( !badMarked )
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
badMarked = true;
}
}