Thursday, 15 January 2015

BUTTON FUNCTIONS IN AS3

BUTTON FUNCTIONS IN AS3



-------------------------------------------------------------------------------------------------------
BUTTON HIDE EXAMPLES IN AS3
http://www.republicofcode.com/tutorials/flash/as3events/
-------------------------------------------------------------------------------------------------------

my_btn.addEventListener(MouseEvent.CLICK, hideMe);

function hideMe(e:MouseEvent):void{
e.currentTarget.visible=false;
}
And that will create the same exact previous result. However, we can now reuse this listener function for more than one object without fear of breaking the code because of the smart reference to our event target:
my1_btn.addEventListener(MouseEvent.CLICK, hideMe);
my2_btn.addEventListener(MouseEvent.CLICK, hideMe);


function hideMe(e:MouseEvent):void{
e.currentTarget.visible=false;
}

my1_btn.addEventListener(MouseEvent.CLICK, hideMe);
my2_btn.addEventListener(MouseEvent.CLICK, hideMe);
function hideMe(e:MouseEvent):void{
if (my1_btn.visible =false , my2_btn.visible =false) {
my3_btn.visible=false;
}}

-------------------------------------------------------------------------------------------------------
BUTTON INTERVAL EXAMPLES IN AS3
http://www.republicofcode.com/tutorials/flash/as3setinterval/
-------------------------------------------------------------------------------------------------------

unction helloFun():void {
trace("Hello!");
}
setInterval(helloFun,2000);

The function is first executed after 2000 milliseconds elapse (i.e. 2 seconds) and then again after each 2000 milliseconds forever.
You can test the code above to see the word Hello displayed in the output window once every two seconds until you stop the movie. Of course in reality you might want to be able to stop the repetitive execution of the code at some point. To do this you will need to use the clearInterval() method.

Using clearInterval()

clearInterval() is a method to be used to stop a setInterval() method. It can only be used on a setInterval() method that is stored in a variable. If a setInterval() method is not stored using any variable then there is no way to stop it. This can be done using the regular var keyword:
function helloFun():void {
trace("Hello!");
}

var myInterval:uint = setInterval (helloFun, 2000);
It is now possible to stop this interval at any time by passing the variable name of our setInterval() method to the clearInterval() method this way:
function helloFun():void {
trace("Hello!");
}
var myInterval:uint = setInterval (helloFun, 2000);
clearInterval(myInterval);

Our example above might not make sense in real life because we are clearing the interval before it even gets executed once. I'll show you a more practical example below.

Practical Example

We are going to create an example where a button will be moved along the stage using an interval. The user will be able to call the clearInterval() method to stop the animation.
Start off by creating a new Flash file in AS3 format, go through Window>Components and drag and instance of the Button component and place it on the left side of the stage. Now select this button and access the Properties Inspector, set the instance name of the button to my_btn.
AS3 SetInterval - Properties Inspector
Our graphical assets are now ready, right-click the only frame you have on your timeline and select Actions to open up the Actions panel.
We are going to start by creating the function that will move the button along the stage. We are going to name this function moveBtn, it will have the simple task of adding 10 points to thex position of the button:
function moveBtn():void {
my_btn.x += 10;
}

function moveBtn():void {
my_btn.x += 10;
}

var myInterval:uint = setInterval (moveBtn, 500);

function moveBtn():void {
my_btn.x += 10;
}

var myInterval:uint = setInterval (moveBtn, 500);

my_btn.addEventListener(MouseEvent.CLICK, stopMe);
function stopMe(e:MouseEvent):void{
clearInterval(myInterval);
}


my2_btn.addEventListener(MouseEvent.CLICK, hideMe);
function hideMe(e:MouseEvent):void{
if (MOVIE1.x = 135 , MOVIE1.y = 180) {
my1_btn.visible=false;
my2_btn.visible=false;
}}
var score:uint = 0;
addEventListener(Event.ENTER_FRAME, ScoreCounter);
function ScoreCounter(e:Event):void{ 
score += 1;
scorecounter.text = " SCORE:"+score.toString()
if (score == 50){
score = 0

MOVIE1.x = 135 ,MOVIE1.y =180 }}
-------------------------------------------------------------------------------------------------
THIS TIMER FUNCTION WORK AND STOP BUTTON FUNCTION
-----------------------------------------------------------------------------------------------


function hideMe(e:MouseEvent):void{
if (num > 10) {
my1_btn.visible=false;
my2_btn.visible=false;
}
else{
my1_btn.visible=true;
my2_btn.visible=true;
}

}
var timer:Timer = new Timer(1000);
timer.addEventListener (TimerEvent.TIMER, onTimer);
var num:uint = 1;
function onTimer (e:TimerEvent):void
{
num++;
scorecounter.text = num.toString();
if (num > 10)
{
MOVIE1.x = 135 , MOVIE1.y = 180
my2_btn.addEventListener(MouseEvent.CLICK, hideMe);
}
}
timer.start ();

---------------------------------------------------------------------------------------------------------------------
BUTTON LABEL FUNCTIONS
http://code.tutsplus.com/tutorials/quick-introduction-flash-button-and-label-components--active-5594
---------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
ON OFF BUTTON AS3
http://www.dakmm.com/?p=250
---------------------------------------------------------------------------------------------------------------------


bn1.addEventListener(MouseEvent.CLICK,fn1)
bn1.label=" Turn On" ;
function fn1(e:MouseEvent):void{
if (bn1.label==" Turn On" ){
bn1.label=" Turn Off" ;
}
else if(bn1.label==" Turn Off" ){
bn1.label=" Turn On" ;
}}


----------------------------------------------------------------------------------------
CLICK GAME WITH BUTTON WITHOUT TIMER AS3

WHEN BOTH BUTTON CLICK THEN  VISIBLE FALSE
---------------------------------------------------------------------------------------

bn1.addEventListener(MouseEvent.CLICK,fn1)
function fn1(e:MouseEvent):void{
bn1.label = " OFF "
}
bn2.addEventListener(MouseEvent.CLICK,fn2)
function fn2(e:MouseEvent):void{
bn2.label = " OFF "
}
addEventListener(Event.ENTER_FRAME, fn3);
function fn3(e:Event):void{
if (bn1.label ==  " OFF "  && bn2.label == " OFF " ) {
bn1.visible=false;
bn2.visible=false;
}
}
-----------------------------------------------------------------------------------
ARRAY PLUS METHOD IN AS3
-----------------------------------------------------------------------------------

//declare the 2 variables
var A :Array = ["A"];
var B :Array = ["B"];
button1.addEventListener(MouseEvent.CLICK, calculate_fn);
function calculate_fn(e:MouseEvent):void {
boxOne.text=String("A"+"B")
}










EmoticonEmoticon