Friday, December 10, 2010

Display Variables Access Scope using timeline objects.

please like and post your questions : http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436

In AS2 it was very easy to access any variables from Parents and Childs using _parent or _root .

but after the enhancement that happened to the AS3 the change the way you can access variables from different scops using the timeline Movies or even thru a classes.

****access variables using time line Movies.

to understand how you can access a parent variables from child or vise-versa you need to create new AS3 document  and then create Circle Movie and name it "shapes_mc"  then inside this Movie Create another square  and Movie and name it "square_mc". so finally create a dynamic text name it "name_txt" then converted to movie and name it "textContainer_mc" as in the picture below:

so now on the stage create new variable in farm1 :
var Scope1 = "Scope1"
in the shapes_mc create another variables :
var Scope2 = "Scope2"
in the Square Movie create variable
var Scope3 = "Scope3"

* now I will show you how you can access these variables from different Movies by creating a buttons in each scope and in when the user click will trace the other variables from button scope.


  • Stage Button Code :

stage_btn.addEventListener(MouseEvent.MOUSE_DOWN,TraceScopeHandler)
function TraceScopeHandler(e)
{
trace("Stage Variable --> "  + Scope1)
trace("Shapes Variable --> " + shapes_mc.Scope2)
trace("square Variable --> " + shapes_mc.square_mc.Scope3)
trace("Text Variable --> " + textContainer_mc.name_txt.text)
}
  • shapes Button Code :
import flash.display.MovieClip;
var Scope2 = "Scope2"
shapes_btn.addEventListener(MouseEvent.MOUSE_DOWN,TraceScopeHandler)
function TraceScopeHandler(e)
{
var oStage:MovieClip = parent as MovieClip
trace("Stage Variable --> "  + oStage.Scope1)
trace("Shapes Variable --> " + Scope2)
trace("square Variable --> " + square_mc.Scope3)
trace("Text Variable --> " + oStage.textContainer_mc.name_txt.text)
}
  • square button code:
import flash.display.MovieClip;
square_btn.addEventListener(MouseEvent.MOUSE_DOWN,TraceScopeHandler)
function TraceScopeHandler(e)
{
var oShapes:MovieClip = parent as MovieClip
var oStage:MovieClip = oShapes.parent as MovieClip
 
trace("Stage Variable --> "  + oStage.Scope1)
trace("Shapes Variable --> " + oShapes.Scope2)
trace("square Variable --> " + Scope3)
trace("Text Variable --> " + oStage.textContainer_mc.name_txt.text)
}


----------------------------------

Source Code 

11 comments:

  1. truly awesome man.!!!!
    you are a life saver..!!!
    ive been stuck in this for 2 days..
    guess learning at home is hard..
    tx a lot..

    ReplyDelete
  2. your most welcome ;)

    if you have any other question just drop it :)

    I hope my future posts will help u also.

    ReplyDelete
  3. :)
    okay.. one more..
    im in a frame and its actionscript has two tweens.
    when i play it.. it just jumps to second tween.
    even if i separate them to different frames,still it plays straight the second one...
    can you help me out??

    ReplyDelete
  4. can you post an example of what you want ??? i really didnt understand your case :(

    ReplyDelete
  5. import com.greensock.plugins.*;
    import com.greensock.*;
    import flash.display.MovieClip;
    import flash.net.URLLoader;
    import flash.net.URLRequest;





    var access:MovieClip = parent as MovieClip
    var pic = "a" + access.num;
    this[pic].x= -264;
    this[pic].y= 4;
    TweenPlugin.activate([AutoAlphaPlugin, SetSizePlugin]);
    TweenLite.to(this[pic], 1, {setSize:{width:540, height:540}});
    }


    var loader:URLLoader = new URLLoader(new URLRequest("1.txt"));
    loader.addEventListener(Event.COMPLETE, completeHandler);

    function completeHandler(event:Event):void {
    var loadedText:URLLoader = URLLoader(event.target);
    tex.text = loadedText.data;
    }

    stop();

    ReplyDelete
  6. you see this code plays both at the same time..(tween + text);

    but if i place the text loader in next frame , it plays only first..

    any way it plays full tween and THEN display text..??
    :(

    ReplyDelete
  7. var access:MovieClip = parent as MovieClip
    var pic = "a" + access.num;
    this[pic].x= -264;
    this[pic].y= 4;
    TweenPlugin.activate([AutoAlphaPlugin, SetSizePlugin]);
    TweenLite.to(this[pic], 1, {setSize:{width:540, height:540}});



    var loader:URLLoader = new URLLoader(new URLRequest(access.num+ ".txt"));
    loader.addEventListener(Event.COMPLETE, completeHandler);

    function completeHandler(event:Event):void {
    var loadedText:URLLoader = URLLoader(event.target);
    tex.text = loadedText.data;
    tex.alpha=1;
    }

    these are the tween and text..they are all right..but how to play them one after other

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. hi dude,
    sorry for the delay but your comments moved to spam folder.

    anyway, you can delay the call of the text loader until the tween is finished.

    TweenLite.to(mc, 5, {delay:3, x:300, ease:Back.easeOut, onComplete:onFinishTween, onCompleteParams:[5, mc]});
    function onFinishTween(param1:Number, param2:MovieClip):void {
    trace("The tween has finished! param1 = " + param1 + ", and param2 = " + param2);
    }

    ReplyDelete
  10. thanx man..!!!
    this works exactly like i want...:)

    ReplyDelete

Create facebook Iframe Application Using flash AS3 API ( Updated Version ).

please like and post your questions :  http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436 After I have received a lot of requ...