Friday 13 February 2015

BOOLEAN MEANING ?





BOOLEAN MEANING ?
In most computer programming languages, a Boolean data type is a data type with only two possible values: true or false.

Related to this, Boolean (named after George Boole)
May also refer to: Boolean algebra, a logical calculus of truth values or set membership.
--------------------------------------------------------------------------------------------------------------------------
HOW BOOLEAN USE IN FLASH
What Is More Listener One Function?
https://wilsonas3tutorial.wordpress.com/simple-card-matching-game-tutorial/simple-card-matching-game-tutorial-4/
-------------------------------------------------------------------------------------------------------------------------


Example event target code:

mc1.addEventListener(MouseEvent.CLICK, clickedMC);
mc2.addEventListener(MouseEvent.CLICK, clickedMC);
mc3.addEventListener(MouseEvent.CLICK, clickedMC);

function clickedMC(e:MouseEvent):void {

e.target.x += 1;

}

----------------------------------------------------------------------------------------------------------------
Simple Card Matching Game Tutorial 5
https://wilsonas3tutorial.wordpress.com/simple-card-matching-game-tutorial/simple-card-matching-game-tutorial-5/
-------------------------------------------------------------------------------------------------------------------



Hi, again ! i know it’s wordy at tutorial 4 but no choice, i cannot apply it if you all don’t know how to use it. So let get started.

Simple Card Matching Game Tutorial 5

Create 3 variables, 2 variables that store the index of the array and 3rd variable will be checking if it has clicked the 2nd time.

var indexNum:Number;
var indexNum2:Number;
var clicked1Time:Boolean = false;

A formula will be needed. This make me think for quite long because it’s hard to turn the logic into coding which need to think for quite a long time but this is well known for game. > o <

The formula i use, i will explain. First get their index number then i divide by 2. If it has decimal, i will round down the number.

The concept behind the formula. Let say that index number of the first object clicked is 6 and i divided by 2 which will make 3, no decimal point, don’t have to round down. You got your first object number.

He clicked the 2nd time. I get the 2nd object number which is 7 and i divided by 2 which make 3.5, I round down the decimal point, so it will be 3. Those 2 object number are now correctly link. That make 1 pair, if they are not link, i will reject it and make him try again. Simple concept.

The formula:

indexNum = Math.floor(bCards.indexOf(e.target)/2);

We need to get his 2nd click so that we can pair it together or reject it. You will need boolean to do this.

I will state false at start because he has not clicked the 1st time. If he clicked the first time, i will turn it to true. The next false will be his 2nd click and restart the whole process again. Computer will know him clicking the 2nd time and store the first object index number to my 2nd storage. I can change the 1st storage again.

The coding:

if(clicked1Time == false)
{

clicked1Time = true;
indexNum2 = indexNum;

}
else
{

clicked1Time = false;

}

But now we just storing it, haven’t even change the state of the object. So we need to check if the 1st object index number match with the 2nd object index number. I will make it disappear. If not i will show my card back view again. To do this, i will use my if statement to create another if statement inside it. This is called nesting.

We need another formula to get the cards REAL index number because the first formula that we give is to make a FAKE index number. Using the FAKE index number, we must find back our REAL index number. That the concept behind the formula.

(Don’t ask me how i get all those crazy formula because this is not easy to explain to you, as i am not a pro in explaining and teaching)

The coding:

else
{

clicked1Time = false;

if(indexNum == indexNum2)
{

fCards[Math.floor(indexNum*2)].visible = false;
fCards[Math.floor(indexNum*2) + 1].visible = false;

}
else
{

bCards[Math.floor(indexNum*2)].visible = true;
bCards[Math.floor(indexNum*2) + 1].visible = true;
bCards[Math.floor(indexNum2*2)].visible = true;
bCards[Math.floor(indexNum2*2) + 1].visible = true;

}

}

We need a timer because you can play but you can’t see the 2nd card as it was too quick, that you can bearing see. A timer will delay the time for viewers to see. I think 0.5 seconds is enough. As it’s for young people to play. If it’s for old man to play, i will put at least 3 seconds for them to see. The question of the delay is: “WHAT IS YOUR TARGET AUDIENCE?”

The coding:

if(indexNum == indexNum2)
{

var delayDisappear:Timer = new Timer(500);
delayDisappear.addEventListener(TimerEvent.TIMER, disappearTime);

delayDisappear.start();

function disappearTime(e:TimerEvent):void
{

fCards[Math.floor(indexNum*2)].visible = false;
fCards[Math.floor(indexNum*2) + 1].visible = false;

delayDisappear.stop();

}

}
else
{

var delayBack:Timer = new Timer(500);
delayBack.addEventListener(TimerEvent.TIMER, giveBack);

delayBack.start();

function giveBack(e:TimerEvent):void
{

bCards[Math.floor(indexNum*2)].visible = true;
bCards[Math.floor(indexNum*2) + 1].visible = true;
bCards[Math.floor(indexNum2*2)].visible = true;
bCards[Math.floor(indexNum2*2) + 1].visible = true;

delayBack.stop();

}

}

I was planning to put this the last part because this is a part of garbage collection, but don’t have this, it’s will have bug inside. I have no choice but to fix it now using garbage collection method.

You have done a game that is correct and you are thinking what is the bug?

It’s work perfectly well but if you click fast enough, you clicked 3 object what will happen?

Ta Ta ! You just perform a magic to destroy your game. Haha !

To solve it, use garbage collection method will do.

Garbage collection because we check every single method human will try to destroy the game. If you click the 3rd object. It’s SCREW ! and your face will be like….. So to fix it, it’s easy.

We remove the listener when the user clicked 2nd time, when he click 3rd object he cannot active the function and we add back the listener so that the user can click again. That’s simple right?

How we add event listener, how we remove it. We used a for loop to add it, We use a for loop to remove it.

Code will be:

for(var j:int = 0; j < bCards.length; j++)
{

bCards[j].removeEventListener(MouseEvent.CLICK, openCard);

}

and

for(var j:int = 0; j < bCards.length; j++)
{

bCards[j].addEventListener(MouseEvent.CLICK, openCard);

}

Place the remove loop inside the 2nd click and place the add inside the 2nd click if statement.

Placement:

function openCard(e:MouseEvent):void
{

e.target.visible = false;
indexNum = Math.floor(bCards.indexOf(e.target)/2);

if(clicked1Time == false)
{

clicked1Time = true;
indexNum2 = indexNum;

}
else
{

(Here put the remove loop)

clicked1Time = false;

if(indexNum == indexNum2)
{

var delayDisappear:Timer = new Timer(500);
delayDisappear.addEventListener(TimerEvent.TIMER, disappearTime);

delayDisappear.start();

function disappearTime(e:TimerEvent):void
{

(Here is the adding loop)

fCards[Math.floor(indexNum*2)].visible = false;
fCards[Math.floor(indexNum*2) + 1].visible = false;

delayDisappear.stop();

}

}
else
{

var delayBack:Timer = new Timer(500);
delayBack.addEventListener(TimerEvent.TIMER, giveBack);

delayBack.start();

function giveBack(e:TimerEvent):void
{

(Here is the adding loop)

bCards[Math.floor(indexNum*2)].visible = false;
bCards[Math.floor(indexNum*2) + 1].visible = false;
bCards[Math.floor(indexNum2*2)].visible = false;
bCards[Math.floor(indexNum2*2) + 1].visible = false;

delayBack.stop();

}

}

}

}

There you go. Try spamming your 3rd object, it’s now unable to come out now. You have a complete matching game now. Great ! You can play your game now without any bugs!

Overall coding we have done and COMMENT YOURSELF !

var indexNum:Number;
var indexNum2:Number;
var clicked1Time:Boolean = false;

function openCard(e:MouseEvent):void
{

indexNum = Math.floor(bCards.indexOf(e.target)/2);

if(clicked1Time == false)
{

clicked1Time = true;
indexNum2 = indexNum;

}
else
{

for(var j:int = 0l j < bCards.length; j++)
{

bCards[j].removeEventListener(MouseEvent.CLICK, openCard);

}

if(indexNum == indexNum2)
{

var delayDisappear:Timer = new Timer(500);
delayDisappear.addEventListener(TimerEvent.TIMER disappearTime);

delayDisappear.start();

function disappearTime(e:TimerEvent):void
{

for(var j:int = 0; j < bCards.length; j++)
{

bCards[j].addEventListener(MouseEvent.CLICK, openCard);

}

fCards[Math.floor(indexNum*2)].visible = false;
fCards[Math.floor(indexNum*2) + 1].visible = false;

delayDisappear.stop();

}

}
else
{

var delayBack:Timer = new Timer(500);
delayBack.addEventListener(TimerEvent.TIMER, giveBack);

delayBack.start();

function giveBack(e:TimerEvent):void
{

for(var j:int = 0; j < bCards.length; j++)
{

bCards[j].addEventListener(MouseEvent.CLICK, openCard);

}

bCards[Math.floor(indexNum*2)].visible = true;
bCards[Math.floor(indexNum*2) + 1].visible = true;
bCards[Math.floor(indexNum2*2)].visible = true;
bCards[Math.floor(indexNum2*2) + 1].visible = true;

delayBack.stop();

}

}

}

}

There you have your mini game done by your own. Congrats, if it yours 1st game, you ever made in your life. ^^V Next tutorial, I will teach you make screen. You will have a start menu, game content and restart menu. Which will make it look like a game.

------------------------------------------------------------------------------------------------------------------------
http://help.adobe.com/en_US/as2/reference/flashlite/WS5b3ccc516d4fbf351e63e3d118cd9b5f6e-7a5e.html
http://help.adobe.com/en_US/as2/reference/flashlite/WS5b3ccc516d4fbf351e63e3d118ccf9c47f-7f22.html

Boolean function

Boolean(expression:Object) : Boolean
Converts the parameter expression to a Boolean value and returns a value as described in the following list:
  • If expression is a Boolean value, the return value is expression.
  • If expression is a number, the return value is true if the number is not zero; otherwise the return value is false.
If expression is a string, the return value is as follows:
  • In files published for Flash Player 6 or earlier, the string is first converted to a number; the value is true if the number is not zero, false otherwise.
  • In files published for Flash Player 7 or later, the result is true if the string has a length greater than zero; the value is false for an empty string.
If expression is a string, the result is true if the string has a length greater than zero; the value is false for an empty string.
  • If expression is undefined or NaN (not a number), the return value is false.
  • If expression is a movie clip or an object, the return value is true.
Note: Unlike the Boolean class constructor, the Boolean() function does not use the keyword new. Moreover, the Boolean class constructor initializes a Boolean object to false if no parameter is specified, while the Boolean() function returns undefined if no parameter is specified.

Availability

Flash Lite 2.0

Parameters

expression:Object - An expression to convert to a Boolean value.

Returns

Boolean - A Boolean value.

Example

trace(Boolean(-1)); // output: true 
trace(Boolean(0)); // output: false 
trace(Boolean(1)); // output: true 
 
 
trace(Boolean(true)); // output: true 
trace(Boolean(false)); // output: false 
 
 
trace(Boolean("true")); // output: true 
trace(Boolean("false")); // output: true 
 
trace(Boolean("Craiggers")); // output: true 
trace(Boolean("")); // output: false
If files are published for Flash Player 6 and earlier, the results differ for three of the preceding examples:
trace(Boolean("true")); // output: false 
trace(Boolean("false")); // output: false 
trace(Boolean("Craiggers")); // output: false
This example shows a significant difference between use of the Boolean() function and the Boolean class. The Boolean() function creates a Boolean value, and the Boolean class creates a Boolean object. Boolean values are compared by value, and Boolean objects are compared by reference.
// Variables representing Boolean values are compared by value 
var a:Boolean = Boolean("a"); // a is true 
var b:Boolean = Boolean(1); // b is true 
trace(a==b); // true 
 
// Variables representing Boolean objects are compared by reference 
var a:Boolean = new Boolean("a"); // a is true 
var b:Boolean = new Boolean(1); // b is true 
trace(a == b); // false 

See also


&& logical AND operator

expression1 && expression2
Performs a Boolean operation on the values of one or both of the expressions. Evaluates expression1 (the expression on the left side of the operator) and returns false if the expression evaluates tofalse. If expression1 evaluates to trueexpression2 (the expression on the right side of the operator) is evaluated. If expression2 evaluates to true, the final result is true; otherwise, it is false. The expression true&&true evaluates to true, true&&false evaluates to false, false&&false evaluates to false, and false&&true evaluates to false

Availability

Flash Lite 1.0

Operands

expression1 : Number - A Boolean value or an expression that converts to a Boolean value.
expression2 : Number - A Boolean value or an expression that converts to a Boolean value.

Returns

Boolean - A Boolean result of the logical operation.

Example

The following example uses the logical AND (&&) operator to perform a test to determine if a player has won the game. The turns variable and the score variable are updated when a player takes a turn or scores points during the game. The script shows "You Win the Game!" in the Output panel when the player's score reaches 75 or higher in 3 turns or less.
var turns:Number = 2;  
var score:Number = 77;  
if ((turns <= 3) && (score >= 75)) {  
    trace("You Win the Game!");  
} else {  
    trace("Try Again!");  
}  
// output: You Win the Game! 



EmoticonEmoticon