Sunday, 8 February 2015

IF STATMENTS






=======================================================================
READ MORE:
https://www.youtube.com/watch?v=xWOCDJZoOhw
https://www.youtube.com/watch?v=aUMNAbLpcGk
http://www.republicofcode.com/tutorials/flash/as3keyboard/
http://www.peachpit.com/articles/article.aspx?p=1628372&seqNum=10
========================================================================

-----------------------------------------------------------------------------------------------------------------------
WHEN BOTH BUTTON CLICK THEN  VISIBLE FALSE
------------------------------------------------------------------------------------------------------------------------
bn1.addEventListener(MouseEvent.CLICK,fn1)
function fn1(e:MouseEvent):void{
bn1.label = " A "
trace("A")
bn3.label = " A "
bn4.label = " A "
}
bn2.addEventListener(MouseEvent.CLICK,fn2)
function fn2(e:MouseEvent):void{
bn2.label = " A "
trace("A")
bn3.label = " A "
bn4.label = " A "
}
addEventListener(Event.ENTER_FRAME, fn1fn2);
function fn1fn2(e:Event):void{
if (bn1.label ==  " A "  && bn2.label == " A " ) {
bn1.visible=false;
bn2.visible=false;
}
}

bn3.addEventListener(MouseEvent.CLICK,fn3)
function fn3(e:MouseEvent):void{
bn3.label = " B "
trace("B")

bn1.label = " B "
bn2.label = " B "


}
bn4.addEventListener(MouseEvent.CLICK,fn4)
function fn4(e:MouseEvent):void{
bn4.label = " B "
trace("B")
bn1.label = " B "
bn2.label = " B "

}
addEventListener(Event.ENTER_FRAME, fn3fn4);
function fn3fn4(e:Event):void{
if (bn3.label == " B " && bn4.label == " B " ) {
bn3.visible=false;
bn4.visible=false;
}
}

------------------------------------------------------------------------------------------------------------------
UPPER CODE WITH TEXT BOX
------------------------------------------------------------------------------------------------------------------

var A1:Number;
A1 = 0;
btn1.onPress = function(){
A1 +=1;
txt1.text =A1
txt3.text = 0;
txt4.text = 0;
}
var A2:Number;
A2 = 0;
btn2.onPress=function(){
A2+=1;
txt2.text=A2
txt3.text = 0;
txt4.text = 0;
}

var B1:Number;
B1 = 0;
btn3.onPress = function(){
B1 +=1;
txt3.text =B1
txt1.text = 0;
txt2.text = 0;
}

var B2:Number;
B2 = 0;
btn4.onPress=function(){
B2 +=1;
txt4.text=B2
txt1.text = 0;
txt2.text = 0;
}
this.onEnterFrame=function(){
if(A1==1 && A2==1){
btn1._visible = false;
btn2._visible = false;
}
else{
btn1._visible = true;
btn2._visible = true;
}
if(B1==1 && B2==1){
btn3._visible = false;
btn4._visible = false;
}
else{
btn3._visible = true;
btn4._visible = true;
}
}

--------------------------------------------------------------------------------------------------------------------
INVISIBLE BUTTON MOVIECLIP WITHOUT ENTERFRAME AS2
https://www.youtube.com/watch?v=7SwhptQcw30&feature=youtu.be
http://www.ilike2flash.com/2008_07_01_archive.html
--------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------
MAIN TIMELINE FRAME NO:1 CODE
----------------------------------------------------------------------------------------------------------------------
//MAIN TIMELINE FRAME NO:1 CODE
var Num1:Number = 0;
var Num2:Number = 0;
var Num3:Number = 0;
var Num4:Number = 0;
function RemoveButtons() {
if(Num1==1 && Num2==1){
btn1._visible=false;
btn2._visible=false;
}
if(Num3==1 && Num4==1){
btn3._visible=false;
btn4._visible=false;
}
//Either Statment http://help.adobe.com/en_US/as2/reference/flashlite/WS5b3ccc516d4fbf351e63e3d118ccf9c47f-7f21.html
if((Num1>1)|| (Num2>1)){
gotoAndStop(2);
}
if((Num3>1)|| (Num4>1)){
gotoAndStop(2);
}
if((Num1>1)|| (Num3>1) (Num1>1)|| (Num4>1)){
gotoAndStop(2);
}
if((Num2>1)|| (Num3>1) (Num2>1)|| (Num4>1)){
gotoAndStop(2);
}
if((Num3>1)|| (Num1>1) (Num2>1)|| (Num4>1)){
gotoAndStop(2);
}
}
----------------------------------------------------------------------------------------------------------------------
INSIDE BUTTON CODES
----------------------------------------------------------------------------------------------------------------------
//INSIDE btn1 CODE
on(press){
Num1++
txt1.text = Num1 + Num2;
//ADD FUNCTION TAIL PART INSIDE EACH BUTTON  WITHOUT IT DOSENOT WORK
RemoveButtons();
}
//INSIDE btn2 CODE
on(press){
Num2++
txt1.text = Num1 + Num2;
//ADD FUNCTION TAIL PART INSIDE EACH BUTTON  WITHOUT IT DOSENOT WORK
RemoveButtons();
}

//INSIDE btn3 CODE
on(press){
Num3++
txt1.text = Num3 + Num4;
//ADD FUNCTION TAIL PART INSIDE EACH BUTTON  WITHOUT IT DOSENOT WORK
RemoveButtons();
}
//INSIDE btn4 CODE
on(press){
Num4++
txt1.text = Num3 + Num4;
//ADD FUNCTION TAIL PART INSIDE EACH BUTTON  WITHOUT IT DOSENOT WORK
RemoveButtons();

}
// MAINTIMELINE FRMAE NO:2 INSIDE RESTART BUTTON CODE
on(press){
gotoAndStop(1)

}
=======================================================================
MATCHING STRING AS2
MAKE TWO BUTTON & TWO TEXT BOX
GIVE TEXT BOX INSTANCE NAMES REACTIVELY LIKE txt1, txt2
BUT YOU NEED BUTTON INSTANCE NAMES
=======================================================================

=====================================================================
//MAIN TIME LINE FRAME NO:1 CODE
var Num1:String = "GREEN";
var Num2:String = "RED";
txt1.text = Num1

// FOR RUN FUNTION  ADD EVERY TIME FuntionString() INSIDE BUTTON OR UNDER BUTTON FUNCTION

function FuntionString() {

//IF TEXT MATCH
if(txt1.text==txt2.text){
gotoAndStop(2);
}
//IF TEXT NOT MATCH
if(txt1.text!=txt2.text){
gotoAndStop(3);
// YOU CAN ALSO ADD STRING IN TEXT BOX 3
txt3.text = Num1+Num2;
}
}
=======================================================================
BUTTON INSIDE CODES
=======================================================================
// IF TEXT MATCH BUTTON INSIDE CODE
on(press){
txt2.text = Num1
FuntionString()
}
// IF TEXT NOT MATCH BUTTON INSIDE CODE
on(press){
txt2.text = Num2
FuntionString();
}
//BUTTON RETURN TO FRAME 1 INSIDE CODE
on(press){
gotoAndStop(1);
}
----------------------------------------------------------------------------------------------------------------
LAYER 2 FRAME 1 CODE : stop();
-----------------------------------------------------------------------------------------------------------------
STRING MATCHING GAME IN AS2
https://www.youtube.com/watch?v=OqDNqw8FmYY&feature=youtu.be
---------------------------------------------------------------------------------------------------------------
======================================================================= EXAMPLE NO 1 MAIN TIME LINE FRAME 1 CODE
======================================================================= var Num1:String = "RED"; var Num2:String = "RED"; var Num3:String = "GREEN"; var Num4:String = "GREEN"; function RemoveRedButtons(){ if(txt1.text==txt2.text){ btn1._visible=false; btn2._visible=false; }} function RemoveGreenButtons(){ if(txt1.text==txt2.text){ btn3._visible=false; btn4._visible=false; }} // MAKE BLANK KEY FRAME 2 ON MAIN TIME this.onEnterFrame = function(){
if(txt1.text=="GREEN" && txt2.text=="RED"||txt1.text=="RED" && txt2.text=="GREEN" ){ gotoAndStop(2); }

//OR USE THIS CODE FOR RETURN GAME
this.onEnterFrame = function(){ if(txt1.text=="GREEN" && txt2.text=="RED"||txt1.text=="RED" && txt2.text=="GREEN" ){ txt1.text="" txt2.text="" }
if(txt1.text==txt2.text){ gotoAndStop(1); } } ================================================================== INSERT BUTTON CODE ================================================================== // INSERT btn1 CODE on(press){ txt1.text = Num1 RemoveRedButtons(); } // INSERT btn2 CODE on(press){ txt2.text = Num2 RemoveRedButtons(); } // INSERT btn3 CODE on(press){ txt1.text = Num3 RemoveGreenButtons(); } // INSERT btn4 CODE on(press){ txt2.text = Num4 RemoveGreenButtons(); }

======================================================================= EXAMPLE NO 2 MAIN TIME LINE FRAME 1 CODE ======================================================================= mc1._visible = false; mc2._visible = false; btn1.onPress = function(){ mc1._visible = true; RemoveRedButtons(); } btn2.onPress = function(){ mc2._visible = true; RemoveRedButtons(); } function RemoveRedButtons(){ if((mc1._visible==true)&&(mc2._visible==true)){ btn1._visible = false; btn2._visible = false; }} mc3._visible = false; mc4._visible = false; btn3.onPress = function(){ mc3._visible = true; RemoveGreenButtons(); } btn4.onPress = function(){ mc4._visible = true; RemoveGreenButtons(); } function RemoveGreenButtons(){ if((mc3._visible==true)&&(mc4._visible==true)){ btn3._visible = false; btn4._visible = false; }} this.onEnterFrame = function(){ if((mc1._visible==true)||(mc2._visible==true)){ mc3._visible==false mc4._visible==false } if((mc3._visible==true)||(mc4._visible==true)){ mc1._visible==false mc2._visible==false } }


Our tutorial is divided into two sections:
  1. Listening to Keyboard Events - You will learn the basics on how to use keyboard actions.
  2. Practical Example - A step by step guide on how to create the example shown above.

Listening to Keyboard Events

Using keyboard commands requires listening to keyboard events. This process is identical to the process for listening to any other event in AS3. You need to use the addEventListener()method to register with a KeyboardEvent. However, unlike other objects, due to the fact that the keyboard is not necessary attached to any specific object in the project, the Keyboard Event is usually registered with the stage. In the example below, the stage object registers for a keyboard event to be triggered each time a keyboard key is pressed down.
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
You can learn more about AS3 Event Handling by reviewing our tutorial on this topic.
In addition to the KEY_DOWN event, it is possible to trigger keyboard events for KEY_UP which is triggered when you release the key and not when you press it down.
The code above will trigger the listener function myKeyDown each time a keyboard key is pressed down. The code below defines this function and tells the Flash movie to display the message "Key Pressed" in the output window when this function is triggered.
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
trace("Key Pressed");
}
You can learn more about the trace() command by reviewing our tutorial on this topic.
You can test the movie above (Go through Control>Test Movie) to see that hitting any keyboard key will trigger that function.
The earlier example show how to register a general keyboard event, but it is not helpful much in reality because you do not know which key is being pressed. In order to learn the used key you can use two properties that belong the the Keyboard Event Class:
  • keyCode - The key code is a numeric value representing the position of the key of the keyboard.
  • charCode - The character code is a numeric value representing the character associated with the key.
The difference between these two becomes apparent when you realize that small and capital "A" letters have the same keyCode but different character codes.
These properties can be retrieved from the event listener function by retrieving the it from the KeyboardEvent reference this way:
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
trace(e.keyCode);
trace(e.charCode);
}
Test your movie to see the details of the keys pressed in the output window.
Though this might a good indicative way to inform you that the keyboard is being used, you are more likely need to associate code with a press of a certain key only and not the whole of the keyboard. In order to do this you need to use a conditional to compare the value of the keyCode with a constant representing the key you need to use. The Keyboard Class has a number of constants for all the keys you are likely to need to use.
For example, if you want to pass a specific command to be triggered when the space bar is pressed you can use a conditional to trigger than command this way:
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.SPACE){
trace("Success!");

}
You can learn more about AS3 Conditionals by reviewing our tutorial on this topic.


Keyboard Detection

The keyboard is just as important an interface device as the mouse, and Flash lets you detect events occurring from keystrokes, both the downward keypress and the upward key release. This ability opens the possibility of having navigation based on the keyboard (using the arrow keys or the number keys, for example) or having keyboard shortcuts that duplicate mouse-based navigation schemes. Flash even lets you control live text that the viewer types in empty text fields in a movie; these text fields merit a separate discussion in Chapter 10, “Controlling Text.” This section focuses on single or combination keystrokes with modifiers (like the Ctrl or Shift key) that trigger a response.
Just as a MouseEvent object is created when the user does something with the mouse, aKeyboardEvent object (another subclass of the Event class) is created when the keyboard is used.
You can detect and respond to the KeyboardEvent object by first attaching a listener to the main Stage (or another object like a text field) using the addEventListener method as follows:
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText);
Next, create a function with a KeyboardEvent parameter:
function detectText(myevent:KeyboardEvent):void {
   // do something in response
}
Table 4.2 details the specific properties that describe the events of the KeyboardEvent object.

Table 4.2. KeyboardEvent Properties

Property
Description
KEY_UP
Happens when a key is released
KEY_DOWN
Happens when a key is pressed
Key code values
The KeyboardEvent object is dispatched whenever any key on the keyboard is pressed. But to determine which particular key has been pressed, you have to use key code values. Key code values are specific numbers associated with each key (see Appendix A, “Keyboard Key Codes”). You use these codes to construct a conditional statement to determine a match. The key code for the spacebar, for example, is 32. So to see if the KeyboardEvent object’s key code matches 32, you write the following:
if (myevent.keyCode==32){
   // spacebar was pressed
}
In this example, myevent is the name of the KeyboardEvent object and keyCode is a property whose value is the key code of the key that was pressed. This conditional statement checks if the key code of the key that was pressed matches the code for the spacebar.
Fortunately, you don’t have to use clumsy numeric key codes all the time. The most common keys are conveniently assigned as properties of another class, the Keyboard class. These properties are constants that you can use in place of the key codes. The statement Keyboard.SPACE, for example, is the number 32. Appendix A also lists all the matching Keyboard constants for the key codes.
Two properties of the KeyboardEvent object, shiftKey and ctrlKey, can be used to test whether the Shift or the Ctrl key is being held down. These properties are either true or false.
To detect a keypress
  1. Select the first keyframe in the Timeline, and open the Actions panel.
  2. In the Script pane, add a listener to the Stage with the addEventListener method, as follows:
    stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText);
    When this listener detects a keypress, it triggers the function called detectText.
  3. On the next available line, write a function with the KeyboardEvent object as a parameter, like so:
    function detectText(myevent:Keyboard Event):void {
       myarrow_mc.x += 5;
    }
    Between the curly braces of the function, put actions you want as a response. In this example, any keypress makes a movie clip called myarrow_mc move 5 pixels to the right.
  4. Choose File > Publish Preview > Default to test your movie circle-a.jpg.
To detect a specific keypress
  1. Continue with the file you created in the previous task.
  2. Select the first frame of the Timeline, and open the Actions panel.
  3. Select the code in between the curly braces of the function and replace it with a conditional statement like this:
    if (myevent.keyCode == Keyboard.RIGHT) {
       myarrow_mc.x += 5;
    }
    The double equals symbol (==) checks the equivalence of the items on either side. If they are equivalent, the actions within the curly braces of the if statement are executed.
  4. Choose File > Publish Preview > Default.
    When you press a key, Flash dispatches a KeyboardEvent object and calls the function. Within the function, Flash checks to see if the key that was pressed matches the right-arrow key. If so, the actions are carried out. In this example, a movie clip called myarrow_mc is moved 5 pixels to the right circle-b.jpg.
To detect keystroke combinations
  1. Continue with the file you created in the previous task.
  2. Select the first frame of the Timeline, and open the Actions panel.
  3. Change the code in between the parentheses of the if statement so that the statement reads
    if (myevent.keyCode == Keyboard.RIGHT && myevent.shiftKey == true) {
       myarrow_mc.x += 5;
    }
    The logical and operator (&&) joins two statements so that both must be true for the entire statement to be true.
  4. Choose File > Publish Preview > Default.
    The if statement will perform the action within its curly braces only if both the right-arrow key and the Shift key are pressed at the same time circle-c.jpg.

The 'if' Conditional in ActionScript

By Riyadh Al-Balushi (Blue_Chi) | Flash ActionScript | Beginner
Conditionals are special tools in ActionScript that allow us to execute a specified selection of code upon the satisfaction of a certain condition. The 'if' conditional is one of the several conditionals available in ActionScript and is one of the easiest ones to use. This tutorial will teach you everything you need to know to start using it in your projects. You are required to have some basic ActionScript knowledge to follow this tutorial.
As stated earlier, a conditional is a tool that lets us execute a certain code only if a condition that we specify is satisfied. For example, in a flash game you can let the player move to the next level if he reaches a score of a 100 or higher, in another situation, you can allow a user to access a certain section in a website only if he submits the correct password for that section.
The 'if' conditional statement could be broken into two parts, the first is the condition that needs to be satisfied, and the second is the collection of statements to be executed if the condition is satisfied. It needs to be written in the following structure:
if (condition){
statements;}
Notice that the condition needs to be written between two normal brackets (), while the statements need to be written between two curvy brackets {}. The condition could be any valid statement that the Flash player could consider as true or false. A basic example would be:
if (score>100){
gotoAndStop(10);
trace("You Won!" );
}
In the upper example the message 'You Won!' would pop up if the value of the score is greater than 100, if the score is not greater than 100, then nothing would happen. It is possible to use other comparison operators such as 'smaller than' (<), 'greater than or equal to' (>=), 'smaller than or equal to' (<=), 'equal to' (==) [Notice that this operator is two equal signs and not one], and 'not equal to' (!=). There are other operators that you could use, but the ones that we mentioned are the most common ones.
In addition to the ability to execute a selection of commands if the condition is true, it is also possible to execute another set of commands if the condition mentioned is not satisfied. This is where the else conditional comes in. This conditional is used after the if statement to execute an alternative set of statements if the condition is not satisfied. Here is the structure at which this needs to be written in.
if (condition){
statements to be executed if condition is satisfied;
else {
statements to be executed otherwise;
}
Again, the alternative set of statements needs to be written between curvy brackets {}. Notice that you can put any number of statements to be executed. Here is another game example:
if (score>100){
gotoAndStop(10);
trace ("You Won!");
} else {
gotoAndStop(9);
trace ("You Lost!");
}
Another important related conditional that you need to learn about is the else if conditional, which makes it possible to execute a certain collection of commands, if the first condition IS NOT satisfied AND the second condition IS satisfied.
if (score>100){
gotoAndStop(10);
trace ("You Won!");
else if (score>50){
gotoAndStop(9);
trace ("You lost, you should try again!");
else {
gotoAndStop(9);
trace ("You completely stink, do not bother to play again!");
}
It is also possible to have more than one required condition or alternative conditions using the 'and' (&&) and the 'or' (||) operators. Using the && operator would let the statements to be only executed if BOTH the first and the second conditions are satisfied.
if (time==0 && score>100){
trace ("You Won!");
}
In the upper example, the player will see the "You Won!" message only if the time equals Zero AND his score is more than a 100, if the time did not finish, or the player did not score more than a 100, the player does not win. On the other hand, if we use the or (||) operator, satisfying any one of the conditions will be sufficient to execute the statements. Notice the difference here:
if (time==0 || score>100){
trace ("You Won!");
}
In this example, the player will see the "You Won!" message if the time runs up or if he scores more than a 100. This is different from the previous one in that the player will not need to wait for the time to finish to win the stage, and he will not need to score a 100 to win if he survives until the time runs up.
Those are all the basic ideas that you need to learn to be able to use the 'if' conditional, it is not difficult at all. I hope that you learnt something helpful from this tutorial. You can view the "The Easiest Method for Creating a Password Protected Section in Flash" Tutorial if you would like to view an exam of the 'if' conditional in practice. Feel free to post at the Oman3D Forum if needed any help.
- End of Tutorial.








EmoticonEmoticon