how to apply condition on all frames? - actionscript-3

I want to apply this condition on all frame of adobe flash cs 6:
if(pauseMusic.visible==true) {
playMusic.visible=false;
} else if(pauseMusic.visible==false) {
playMusic.visible=true;
}
But I don't want to copy it one by one of frame actions. What cant I do?

!
create a function in your first frame
if you have more than one frame in your time line, at the first frame insert it:
if(this._condition == null) {
this._condition = function():void {
if(pauseMusic.visible==true) {
playMusic.visible=false;
} else if(pauseMusic.visible==false) {
playMusic.visible=true;
}
}
}
and simply call it in other frames : this._condition();

I agree with #payamsbr but this is shorter...
_condition = function():void {
pauseMusic.visible = !pauseMusic.visible;
}
So you may avoid the if... else... check.
The initial Boolean should be defined to
true or false
So all You have to do is to call
_condition();
[EDIT]
OR in case of uint 1 ->-1 or -1 to 1:
var i:uint=1
function changeUint():void{
i = *=-1
}
changeUint();
-> 1 to -1 or -1 to 1
1 to -1 or -1 to 1...
[/EDIT]
But You want to check if the function exist as he said.

I used this function in first frame:
stage.addEventListener(Event.ENTER_FRAME, onFrameEnter);
function onFrameEnter(Event):void
{
if(pauseMusic.visible==true)
{
playMusic.visible=false;
}
else if(pauseMusic.visible==false)
{
playMusic.visible=true;
}
}

Related

how does the "for each in" loop work?

for some reason this code does not work as intended, I've cut out most of my program but, all I think I need to tell you is that I have a tree array, and I am trying to make collisions with said trees so I thought that this would work:
function collisions(loopEvent:Event):void
{
for each (var a:tree in TreeArray)
{
if (brettMc.right1.hitTestObject(a.stump))
{
rightcoll = false;
}
else
{
rightcoll = true;
}
if (brettMc.left1.hitTestObject(a.stump))
{
leftcoll = false;
}
else
{
leftcoll = true;
}
if (brettMc.up1.hitTestObject(a.stump))
{
upcoll = false;
}
else
{
upcoll = true;
}
if (brettMc.down1.hitTestObject(a.stump))
{
downcoll = false;
}
else
{
downcoll = true;
}
}
}
I am pretty sure that the problem is just that the for each loop is messed up.
I don't see any issue with loop itself
EXCEPT
Is TreeArray actual name of variable? Or it's a Type?
If that's actual variable, can you trace the length of it before loop?

How do you create an if statement to check if many objects style == dispaly:none?

I'm making a memory-match game in which you flip two cards over in order to get them to match.
I'm doing it with simple if statements as shown below:
if(click == 2) //denotes two cards being clicked
{
if(flippedArray[1].src === flippedArray[0].src) // if click 1 == click 2 then refer to function 'delayMatch' which sets click 1 and 2 cards to not be displayed
{
window.setTimeout(function() { delayMatch() }, 500);
console.log("EQUAL");
score = +25000;
}
else
{
window.setTimeout(function() { delayNoMatch() }, 500); // if click 1 != click 2 then display card.png
console.log("NOT EQUAL");
score = -1999;
}
function delayMatch() //function for matching pairs
{
flippedArray[0].style = "display:none;";
flippedArray[1].style = "display:none;";
}
function delayNoMatch() //function for non-matching pairs
{
flippedArray[0].src = "card.png";
flippedArray[1].src = "card.png";
}
click = 0; // when clicked two cards set click back to zero
}
As you can see if two cards match they're set to display:none. What I'm trying to do is link to an "end game" html page once all 36 divs are set to display: none or I guess once the function delayMatch() has been called 18 times.
I'm completely at a loss as how I can do this.
my goal is something like this:
flippedArray[0] and flippedArray[1] is just a temporary array to check if the two cards currently in play are a match or not.
I was thinking something like:
endGameCounter =0;
endGameCounter++; //in the matching if-statement
then if(endGameCounter == 18)
{
location.href='link here'
}
You can use one variable for count inside the page.
If you are doing it in JSP, JSTL can help you here.
use to set a variable and just check the value time to time.
c:set var="COUNT" value="SOMETHING"/>
Try
function GameIsOver(){
for (var i = 0; i < allCards.length; i++) {
if(allCards[i].style.display === 'none')return false;
}
return true;
}

Actions Script 3.0 if else statements

need to do several things by it's click event. I'm a beginner to this, so is there any other way to write this code? by clicking this button, it goes to next frame and according to statement several buttons will be visible or not. I wrote the code this way and it says there are syntax error, but I couldn't find any. Hope you guys understand this and will help me. :) Thank you!
review_btn.addEventListener(MouseEvent.CLICK, review1)
function review1(event:MouseEvent):void{
if(rvw1 == "Correct"){
gotoAndStop(3627);
help1.visible = false
}
else{
gotoAndStop(3627);
help1.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review2)
function review2(event:MouseEvent):void{
if(rvw2 == "Correct"){
gotoAndStop(3627);
help2.visible = false
}
else{
gotoAndStop(3627);
help2.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review3)
function review3(event:MouseEvent):void{
if(rvw3 == "Correct"){
gotoAndStop(3627);
help3.visible = false
}
else{
gotoAndStop(3627);
help3.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review4)
function review4(event:MouseEvent):void{
if(rvw4 == "Correct"){
gotoAndStop(3627);
help4.visible = false
}
else{
gotoAndStop(3627);
help4.visible = true
}
}
review_btn.addEventListener(MouseEvent.CLICK, review5)
function review5(event:MouseEvent):void{
if(rvw5 == "Correct"){
gotoAndStop(3627);
help5.visible = false
}
else{
gotoAndStop(3627);
help5.visible = true
}
}
I'll take an attempt at it. It looks like the only difference is that in each method you need to match up "helpX".visible with "rvwX" equals the string "Correct", where X is a number from 1-5. The gotoAndStop() frame is the same regardless. Also, that all five are meant to be off the same button. I'm going to take an assumption that the clips 'help' are movieclips defined on the stage else if they are from something else I would store them in an array for looping through instead of 'building' the name and finding the reference that way just for clarity.
function review(event:MouseEvent):void {
for(var counter:int = 1; counter < 6; counter++){
this["help" + counter].visible = (this["rvw" + counter] != "Correct");
}
this.gotoAndStop(3627);
}
review_btn.addEventListener(MouseEvent.CLICK, review);
I think you have to do a class with 2 fields: "help" and "rvw" (let me call it "Switcher"). Also it may contain a function of setting visibility (may, not must, this function can also be in your main class):
Switcher.as:
import flash.display.MovieClip;
public class Switcher {
private var help:MovieClip;
private var rvw:String;
public function setVisibility() {
help.visible = !(rvw == "Correct");
}
}
Then you have to make an array of Switcher's objects in your main class and to use only one "review" handler:
function review(event:MouseEvent):void {
for each(var sw:Switcher in switchersArray) {
sw.setVisibility();
}
this.gotoAndStop(3627);
}
The code from previous answer will work correctly, but IMHO, creating an Array (or Vector) of similar objects is better than doing lots of help1, help2, help3 etc variables.

as3 reset the 'numPressed' function

i'm quite new to as3 so this may be pretty obvious to most of you out there :P
I'm using the numPressed function (which counts the mouse clicks) and need to reset the mouse clicks after the 6th click...
This is what the code looks like:
var numPressed:Number = 0;
any_mc.addEventListener(MouseEvent.CLICK, countUp);
function countUp(evt:MouseEvent):void {
numPressed++;
if (numPressed == 1) {
any_mc.gotoAndPlay(1);
}
else if (numPressed == 2) {
any_mc.gotoAndPlay(2);
}
else if (numPressed == 3) {
any_mc.gotoAndPlay(3);
}
else if (numPressed == 4) {
any_mc.gotoAndPlay(4);
}
else if (numPressed == 5) {
any_mc.gotoAndPlay(5);
}
}
Any help would be much appreciated!
as Taurayi said but also you could clean your function up a little also like this.
function countUp(evt:MouseEvent):void
{
any_mc.gotoAndPlay(numPressed++);
if(numPressed > 5)
numPressed = 1;
}

leave a for each loop

I want to leave the for each loop as soon as the boolean is set to true.
for each (xmlEvent in arEvents)
{
var xmlEvent:XMLList = XMLList(event);
if(xmlEvent.id == artistEvent.id)
{
boolExists = true;
}
}
I've tried break and such, but this all doesn't work.
Use break operator
for each (xmlEvent in arEvents)
{
var xmlEvent:XMLList = XMLList(event);
if(xmlEvent.id == artistEvent.id)
{
boolExists = true;
break;
}
}
Why not use the some() method? It'll handle all that for you.
boolExists = arEvents.some(
function (xmlEvent:*, index:int, arr:Array):Boolean {
return xmlEvent.id == artistEvent.id;
});
Note: The anonymous function is used only for brevity.
For clarity, the some() method only iterates over the array until the first item that returns true is reached, at which point iteration is terminated and the result (true) is returned.