Action Script 3. How to disable Allow Click function? - actionscript-3

I'm creating Flash "memory" game, Idea to discover 2 equal cards, but I have 1 problem.
When I discover second card program write: "Wrong turn" and It is shown for 1 second, but at this time I can discover other cards, 3rd, 4th etc. I need to add something like "AllowClick = false" function, till 2 cards are shown.
As I understand I need to use this:
...RemoveEventListener(MouseEvent.CLICK, checkCards); Could you help me to use It correct?
DEMO
Here is part of my code:
else
{
trace("Wrong");
_message = "Wrong";
message_txt.text = _message;
_secondCard = event.currentTarget;
var timer:Timer = new Timer(1000, 1); //antros kortos atsivertimo laikas
timer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack);
timer.start();
}
}
protected function flipBack(event:TimerEvent):void
{
_firstCard.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = _secondCard = undefined;
}
I hope you understood my question. Could you help me, please? Thank you very much.

Did you tried to set .mouseEnabled to false and .mouseChildren to false to all other elements? See here: as3 mouseEnabled still a problem for me

Related

How can I make a symbol appear after clicking in two different buttons in actionscript 3.0?

I am a little bit new in this programming stuff and I need help to program in ActionScript 3.0 (Adobe Animate CC). I want to make a symbol (graph) visible but only after clicking in two different buttons (button1 and button 2). I can make that with just one button, but I can't make it with two buttons... Can anyone help me? I tried this code but it isn't working as it should:
button1.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
button2.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
function fl_MouseClickHandler_1(event:MouseEvent):void
{
graph.visible = true;
}
Tom
Simplest way would be to do this, although there may be other ways to do it as well:
var isButton1Clicked:Boolean = false;
var isButton2Clicked:Boolean = false;
button1.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
button2.addEventListener (MouseEvent.CLICK, fl_MouseClickHandler_1);
function fl_MouseClickHandler_1(event:MouseEvent):void
{
if (event.currentTarget == button2)
isButton2Clicked = true;
else if (event.currentTarget == button1)
isButton1Clicked = true;
if (isButton1Clicked && isButton2Clicked)
{
graph.visible = true;
isButton1Clicked = isButton2Clicked = false;
}
}
Note that I reset both the Boolean values to false, once the graph is visible so that it works like a reset.
On a side note, I would recommend to use better names for your buttons and your event handlers. Just best practice.
Hope this helps. Cheers.

Flash AS3 - Drag and Drop multiple objects to one target?

Title is more or less self-explanatory, I’ve been working through many different tutorials and I’m not exactly too good with AS3 to be perfectly honest. (Image above shows what I'm aiming for)
Anyway, what I notice in most of the online tutorials I see, the drag and drop tutorials are either based on one object to one target or multiple objects to multiple targets, so I was wondering if someone is kind enough to help me and explaining how I can get multiple objects to connect to one target.
And, if possible making it switchable, as in for example, if object 1 is already in place on the target when I drag object 2 over, then object 1 returns to its original position and object two takes its place.
A easier way to explain this is to say that I'm trying to create a game where there are three statues and the user can pick one of the three to place in a set target zone.
I apologies if what I say doesn't make much sense, will clear up anything if that causes confusion. Here's the AS3 code I'm using at the moment.
var startX:int;
var startY:int;
circle1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var theTargetName:String = "target" + event.target.name;
var theTarget:DisplayObject = getChildByName(theTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == theTarget){
event.target.buttonMode = false;
event.target.x = theTarget.x;
event.target.y = theTarget.y;
}
else{
event.target.x = startX;
event.target.y = startY;
circle1_mc.buttonMode = true;
circle2_mc.buttonMode = true;
circle3_mc.buttonMode = true;
Instead of checking the dropTarget, you can use hitTestObject to see if the dropped object is "touching" theTarget. Otherwise, any other item that has already been dropped onto theTarget may be reported as the dropTarget.
Also, since MovieClip is dynamic, you can store the startX and startY values in each instance.
The following modified code will use a single target_mc as a drop target. When one item is dropped on it, any other items will be moved back to their original spot:
// create an array as #David suggested to keep track of your draggable items
var circles:Array = [circle1_mc, circle2_mc, circle3_mc];
for each(var circleMC:MovieClip in circles)
{
circleMC.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circleMC.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circleMC.startX = circleMC.x;
circleMC.startY = circleMC.y;
}
function pickUp(event:MouseEvent):void
{
// no longer need to keep track of startX & startY here because that's already been done up above
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void
{
event.target.stopDrag();
// check to see if the event target is touching target_mc using hitTestObject
if(event.target.hitTestObject(target_mc)){
event.target.buttonMode = false;
event.target.x = target_mc.x;
event.target.y = target_mc.y;
// move all circles OTHER than the current target back to their original positions
for each(var circleMC:MovieClip in circles)
{
if(event.target != circleMC)
{
circleMC.x = circleMC.startX;
circleMC.y = circleMC.startY;
}
}
}
else
{
// only need to move the event target back if it was dropped outside of target_mc
event.target.x = event.target.startX;
event.target.y = event.target.startY;
event.target.buttonMode = true;
}
}

Something like cookies in Flash/ActionScript

i need to implement something like a cookie in a flash file....and i dont have a clue about ActionScript.
Basically it is a video with a mute/unmute button. If i mute the video and refresh the browser it is not muted again.So i need to persist the mute status somehow.
Here is my complete ActionScript File:
import flash.net.SharedObject;
var a:Boolean = false;
var cookie:SharedObject = sharedobject.getLocal("muted");
if (cookie.data.muted == true) {
SoundMixer.soundTransform = new SoundTransform(0);
Object(root).ton_btn.gotoAndStop(2);
}
ton_btn.addEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
function fl_MouseOverHandler(event:MouseEvent):void
{
Object(root).ton_btn.buttonMode = true;
Object(root).ton_btn.useHandCursor = true;
}
ton_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
if (! a)
{
var muteStatus:Boolean = true;
cookie.data.muted = muteStatus;
SoundMixer.soundTransform = new SoundTransform(0);
Object(root).ton_btn.gotoAndStop(2);
trace(a);
}
else
{
var muteStatus:Boolean = false;
cookie.data.muted = muteStatus;
SoundMixer.soundTransform = new SoundTransform(1);
Object(root).ton_btn.gotoAndStop(1);
trace(a);
}
a = ! a;
}
This is not working, now my mute button is flickering....it seems, that the if clause is constantly executing.
Thanks for any tip, hint or link in advance. ;)
Regards
Nils
Edit:
So stupid...it was just a typo.
var cookie:SharedObject = sharedobject.getLocal("muted");
must be:
var cookie:SharedObject = SharedObject.getLocal("muted");
Now it is working.
I recommend following opensource that is popular ActionScript3.0 Cookie Frameworks. In general, two kinds of cookies in with the framework is known. Please see under open source. Solve your problem more quickly, you may give. Number 1 is a only Sourcode and Tutorial, Number 2 is a full of Frameworks.
Read and Write and Edit using a SharedObject
Actionscript3 Cookie Util

AS3 making an array invisible!? how?

var sunflowers30:Array = [sunflowerpetal1,sunflowerpetal2,sunflowerpetal3,sunflowerpetal4,sunflowerpetal5,sunflowerpetal6];
sunflowers30.visible = false;
Why is the code up there not working?? what am i doing wrong?? ( trying to make the array invisible).
also should this code not work as well? (below)( trying to go to a different scene once array ( all instances) are hidden/invisible).
if(sunflowers30.visible == false)
{
gotoAndPlay(1, "theplace")
}
;
Sunflowerpetal 1-6 are instances that are on my stage currently
Sunflowers30 is the array i made from the instances on stage.
"Theplace" is the next scene
Help and comments are much appreciated I'm kind of new to AS3 and code in general but i bet you code gurus could help me out, many thanks ahead of time!
Array does not have a visible property.
What you need to do is loop through the array and set the property on each element of that array.
var sunflowers30:Array = [sunflowerpetal1,sunflowerpetal2,sunflowerpetal3,sunflowerpetal4,sunflowerpetal5,sunflowerpetal6];
for each( var obj:Object in sunflowers30 ){
obj.visible = false;
}
// or another way or doing it
for( var i:int = 0; i<sunflowers30.length; i++){
obj.visible = false;
}
And as your second question asks if it should work the answer is no.
You are again targeting the array and not the object you want to test if it is visible.
if(sunflowerpetal1.visible == false)
{
gotoAndPlay(1, "theplace")
}
;

Flex 4 timers keep Firing

I'm trying to create a simple flex4 project which involves some timers that trigger other functions.
I don't have much experience with Action Script and even less with timer events.
Here is a bit of my code it seems to be working for the most part but you lines were I'm adding up the total score (score = score +1;) seems to just keep adding and adding when I test the application. I think its because the timers keep firing the function but I'm not sure.
private var score:int = 0;
private function submit():void {
this.currentState = 'loading';
var timer:Timer = new Timer(2200);
timer.addEventListener(TimerEvent.TIMER, removeLoading);
timer.start();
}
private function removeLoading(event:TimerEvent):void{
removeloading.play();
var timer1:Timer = new Timer(1000);
timer1.addEventListener(TimerEvent.TIMER, viewResults);
timer1.start();
this.currentState = 'results';
}
private function viewResults(event:TimerEvent):void{
if (q1_t.selected == true){
answer1m.text = 'You Answer the Question Correctly.';
score = score +1;
} else {
answer1m.text ='The Correct answer was: '+ q1_t.label;
}
if (q2_f.selected == true){
answer2m.text = 'You Answer the Question Correctly.';
score = score +1;
} else {
answer2m.text ='The Correct answer was: '+ q2_f.label;
}
finalscore.text = score.toString();
}
So I did a bit more research and turns out I hadn't included the second timer parameter.
The second parameter is the number of times that the TimerEvent.TIMER event will be dispatched before stopping. If you set the second parameter to 0 (zero) or omitted it completely, the timer would run forever (or until you called the stop() method on the timer instance.
Since I only want to run the event once I need to add 1.
From this:
var timer:Timer = new Timer(2200);
To this:
var timer:Timer = new Timer(2200,1);