The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 43 of 45 FirstFirst ... 334142434445 LastLast
Results 631 to 645 of 669

Thread: ActionScript 3 Tip of the Day

  1. #631
    Just a note. You can't seem to trigger a stage.invalidate() during a handler for Event.RENDER. Since its it the same pass it doesn't work and the event doesn't trigger.

    In a Event.RENDER handler I was adding an Event.RENDER listener to a sub movieclip and triggering a gotoAndStop() then a stage.invalidate(). What I needed to do to get it to work is add an Event.ENTER_FRAME listener then in the ENTER_FRAME listener add an Event.RENDER listener, then handle it..

    Complicated mess of sorts but if you are trying to do this in a render event listener this add an enter_frame listener, then in the enter_frame add a render event listener. I don't know if I sound like I'm making sense but thats what you do.

    Quote Originally Posted by senocular View Post
    ActionScript has long since relied on the enterFrame (onEnterFrame) event for time-based actions, especially animation and actions relating to frame playback. In comparison, Director's Lingo language has, not only an enterFrame event, but two other frame events, prepareFrame and exitFrame. Though ActionScript 3 has not aquired prepareFrame or exitFrame, it has gained a new frame event, render, or Event.RENDER (flash.events.Event.RENDER).

    The render event in AS3 is a frame event that occurs after enterFrame (flash.events.Event.ENTER_FRAME) allowing one more chance to do what you need to do before the screen updates its display.

    Unlike enterFrame, however, the render event will not be called unless the display object using it is attached to a stage (or in any display list attached to the stage). Also, render is not automatically called every frame, even if attached to the stage. In order for render to be called for the current frame, you must make a call to stage.invalidate() (flash.display.Stage.invalidate());

    Ex:
    Code:
    var sprite:Sprite = new Sprite();
    stage.addChild(sprite);
    
    sprite.addEventListener(Event.ENTER_FRAME, enterFrame);
    sprite.addEventListener(Event.RENDER, render);
    stage.addEventListener(MouseEvent.CLICK, click);
    
    function enterFrame(event:Event):void {
        trace("enter frame");
    }
    function render(event:Event):void {
        trace("render");
    }
    function click(event:MouseEvent):void {
        trace("click");
        stage.invalidate();
    }
    Code:
    Output:
    enter frame
    enter frame
    enter frame
    click
    enter frame
    render
    enter frame
    enter frame
    enter frame
    enter frame
    ...

  2. #632
    Can Any one help me in making .. a tool for audio/video recording through fms with replay option!

    looking fr d help

    Thanks
    shubhram

  3. #633
    how can is use the away 3d in flash and how can I integrate my own 3d layout with using away 3d are any other

  4. #634

    Generated ActionScript Classes from mxml

    To see the generated actionscript classes from your mxml code give -keep as additional compiler arguments.
    eg: In Flash Builder select Project->Properties->Flex Compiler and in the Additional compiler arguments will look:
    Code:
    -locale en_US -keep

  5. #635
    rimacy's Avatar
    309
    posts
    Registered Schmoozer

    OutSide Looking In

    this seems pivotal what does it grant player by passing it map in this way... what am i saying by doing this?
    var map = new Map(); var player = new Player(map);

  6. #636

    Self invoking functions in ActionScript 3

    Consider this normal example:

    Code:
    function greetings():void {
        trace("Hello World");
    }
    
    greetings();
    This will display "Hello World".

    The above example can be written in more elegant way (i.e., self invoking the function) as:
    Code:
    (function greetings():void {
        trace("Hello World");
    })();
    You get the same result. So the format is:
    Code:
    (f)();
    Another example:
    Code:
    (function greetUser(name:String):void {
        trace( "Hello " + name );
    })("Awesome");
    You pass an argument during self invocation.

    [edit]
    However one important thing you must consider is the scope.
    Code:
    (function greetUser(name:String):void {
        trace( "Hello " + name );
    })("Awesome");
    
    greetUser("Foo");  //will result in error because its not global
    So note that.
    Last edited by kadaj; October 5th, 2010 at 05:51 AM.

  7. #637

    Function Arguments in Depth

    Function Arguments in Depth

    Function which takes infinite arguments
    Code:
    function infiniteArgs(...args):void {
        trace(args);
    }
    
    infiniteArgs( 1, 2 );
    infiniteArgs( 1, 2, 3, 4, 5 );
    infiniteArgs( 1, 2, 3, 4, 5, 6, 7 );
    infiniteArgs( 'a', 'b', 'c' );
    Another method without all this hassle is to do like the below. I recommend this instead of the above method:
    Code:
    var inf:Function = function():void {
        trace(arguments);
    }
    
    inf( 1, 2, 3, 4 );
    inf( 'a', 'b' );
    Arguments array
    Code:
    function foo(a:int, b:int, c:int):void [
        trace(arguments);    //displays the arguments passed
    }
    foo( 1, 2, 3 );
    foo( 0, 10, 110 );
    It will display the arguments passed into the function.
    Now you can use arguments.length, arguments[0] etc.
    Code:
    function foo(a:int, b:int, c:int):void {
        trace( "args: " + arguments );
        trace( "len: " + arguments.length );
        trace( "arguments[2]: " + arguments[2] );
    }
    foo( 1, 2, 3 );
    foo( 1, 10, 110 );
    When using mutliple parameters the arguments are referred by the variable that you put after the 3 dots. i.e:
    Code:
    function foo(...myCoolArgs):void {
        //instead of myCoolArgs if you gave arguments you'll get undefined error.
        trace( "args: " + myCoolArgs );
        trace( "len: " + myCoolArgs.length );
    }
    Passing arguments to another function
    You have a function which received arguments. You need to send those arguments to another function as arguments itself instead of an array. For that you can use the 'apply' function.
    Code:
    //normal situation
    function fooA(...myCoolArgs):void {
        trace( "args: " + myCoolArgs );
        trace( "len: " + myCoolArgs.length );    //you get 5
        fooB( myCoolArgs );
    }
    
    function fooB(...args):void {
        trace( "args: " + args ); 
        trace( "len: " + args.length );    // you get 1 instead of 5
    }
    
    fooA( 0, 1, 2, 3, 4 );
    Its passed as an argument array. So what you do is:
    Code:
    function fooA(...myCoolArgs):void {
        trace( "args: " + myCoolArgs );
        trace( "len: " + myCoolArgs.length );    //you get 5
        fooB.apply( this, myCoolArgs );    //pass it like this
    }
    
    function fooB(...args):void {
        trace( "args: " + args ); 
        trace( "len: " + args.length );    // now you get 5
    }
    
    fooA( 0, 1, 2, 3, 4 );
    More cooler version:
    Code:
    var fooA:Function = function():void {
    	trace( "args: " + arguments );
    	trace( "len: " +  arguments.length );
    	fooB.apply( this, arguments );
    }
    
    var fooB:Function = function():void {
    	trace( "args: " + arguments ); 
    	trace( "len: " + arguments.length );
    }
    
    fooA( 0, 1, 2, 3, 4 );
    fooA( 'a', 'b', 'c' );
    Last edited by kadaj; October 5th, 2010 at 06:43 AM.

  8. #638

    Recursion using anonymous function in ActionScript 3

    You must be familiar with recursion with normal functions, i.e a function calling itself. An anonymous function is a function without a name. So to use recursion in an anonymous function you call the arguments.callee() to refer to the anonymous function.
    If its a normal function we can call that function using its name. Anonymous function don't have name, but you can refer the function using the callee method.

    How to approach recursion?
    The key idea to recursion is the terminating condition. First you need to figure out when the function should terminate. Then you think on how to generalize. Means first you look at when the factorial function say fact(n) should terminate, .i.e, when n==1.

    A normal function that computes factorial (I'm choosing factorial because its simple, and easy to understand) can be like:
    Code:
    function fact(n):int {
        if( n == 1 )
            return 1;
        else return n * fact( n-1 );
    }
    trace(fact(5));
    In terms of anonymous function:
    Code:
    var fact:int = (function(n):int {
        if( n==1 )
            return 1;
        else return n * arguments.callee( n-1 );
    })(5);
    
    trace(fact);

  9. #639
    Quote Originally Posted by senocular View Post
    Using ActionScript 3, you can dynamically change the frame rate of your movie using the Stage class.

    The Stage class (flash.display.Stage) is the class assigned to the stage object which is accessible from your main movie sprite/movie clip (or others within the same security sandbox) using the stage property. The stage object has a frameRate property which can contain any value between 0.01 and 1000 and determines the frame rate at which the Flash player plays back your movie. Changing this value lets you change the frame rate at runtime.

    Code:
    // change frame rate to 12 fps:
    stage.frameRate = 12;
    Thanks for sharing !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    _______________________________________________
    Kanyakumari | Kanyakumari Hotels | Kanyakumari Rock Memorial

  10. #640
    Declare multiple variables in one line:
    Code:
    var i:int=0, j:int=0, k:int=0;

  11. #641
    assign multiple vars with same value, ... the short way.

    Code:
    var one:int=0, two:int=0, three:int = 0;
    one = two = three = 1;
    http://blog.rackdoll.nl

  12. #642
    Hey senocular,

    This might be old news but I just tried your duplicateDisplayObject method with the BulkLoader classes and it keeps returning an empty movieclip.

    In BulkLoader I load a swf and get it as a MovieClip

    var mc:MovieClip = loader.getMovieClip("box.swf");

    then when I try,

    addChild(duplicateDisplayObject(mc)); or

    var newMc:MovieClip = duplicateDisplayObject(mc);
    addChild(newMc);

    It just return a blank movieclip with no graphics.

    I know the swf loads properly because if I add the mc to the stage I see the graphics. I just need to be able to make multiple copies of the object. I don't want to have to load a new copy for every duplicate.
    World Arcade - What! onepagelove Most Loved Award | Twitter | Facebook

  13. #643

    my question

    how to post my question in kirupa,
    please send replay

  14. #644

    questions

    what is different between AS3 and AS2?

  15. #645
    Never use indexOf when making array unique sort. Always use dictionaries or objects.

    WRONG:
    Code:
    function arrayUniqueIndex(s:Array):Array
    {
        var n:int = s.length;
        var uniq:Array = new Array();
        for(var i:int = 0; i < n; i++) if(uniq.indexOf(s[i]) < 0) uniq.push(s[i]);
        return uniq;
    }
    CORRECT:
    Code:
    function arrayUniqueObj(s:Array):Array
    {
        var n:int = s.length;
        var uniq:Array = new Array();
        var obj:Object = new Object();
        for(var i:int = 0; i < n; i++) if(!obj[s[i]]) { obj[s[i]] = true; uniq.push(s[i]); }
        return uniq;
    }
    function arrayUniqueDict(s:Array):Array
    {
        var n:int = s.length;
        var uniq:Array = new Array();
        var dict:Dictionary = new Dictionary(true);
        for(var i:int = 0; i < n; i++) if(!dict[s[i]]) { dict[s[i]] = true; uniq.push(s[i]); }
        return uniq;
    }
    Proof:
    Code:
    unique.sort benchmark | core2duo 3ghz, 4gb ddr2 ram, win 10.2.152.26 debug
    array -> 500000 elements (20 cycles)
    --------------------------------
    object     |     dict     |     indexOf
    --------------------------------
    18 ms     |     16 ms     |     316 ms
    16 ms     |     16 ms     |     328 ms
    16 ms     |     16 ms     |     317 ms
    17 ms     |     15 ms     |     314 ms
    17 ms     |     15 ms     |     315 ms
    16 ms     |     16 ms     |     315 ms
    17 ms     |     15 ms     |     314 ms
    17 ms     |     15 ms     |     314 ms
    16 ms     |     16 ms     |     315 ms
    16 ms     |     17 ms     |     314 ms
    16 ms     |     16 ms     |     314 ms
    16 ms     |     16 ms     |     315 ms
    16 ms     |     16 ms     |     314 ms
    16 ms     |     16 ms     |     316 ms
    16 ms     |     16 ms     |     317 ms
    17 ms     |     16 ms     |     314 ms
    16 ms     |     16 ms     |     315 ms
    16 ms     |     16 ms     |     314 ms
    17 ms     |     15 ms     |     314 ms
    16 ms     |     16 ms     |     318 ms
    
    --------------------------------
    avg results:
    object     |     dict     |     indexOf
    --------------------------------
    16 ms     |     15 ms     |     315 ms
    --------------------------------
    Benchmark >> http://pastebin.com/8tJbGGX1
    Last edited by jloa; March 9th, 2011 at 12:28 PM.

Page 43 of 45 FirstFirst ... 334142434445 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012