A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Wormhole effect

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1

    Wormhole effect

    hello everyone,
    I am barely getting into flash and I've ran into a problem trying to make a classic wormhole effect.
    What I want to portray are squares getting larger and larger toward me as if I'm traveling inwards.

    what i have done so far is constructed a small square turned it into a symbol and put it in the center of the page. I then double clicked the symbol and used shape tween to expand it outward. This motion is exactly what i need, but now i need this shape tween to repeat every X amount seconds.

    I know i have to go back to the scene and make an action script but i need some guidance in basic loops. I've looked around at different tutorials and have learned things but nothing to really pinpoint my issue.

    can anyone guide or assist me in the right direction?

    Thank you very much

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Lots of ways you can spin this but given your setup, probably simplest to just add a new copy of the clip to the stage at a regular interval.
    First, in your library, right click on the movie clip containing the shape tween and choose properties. Check the box for "Export for ActionScript". The text that appears in the Class: input field will be the name we use to add a copy to the stage. Give it name like 'GrowBox' (no quotes) Click OK. Ignore the class warning that comes up and just hit OK.

    Clear your main stage and main timeline, the library symbol will still be in tact.
    On frame 1 of your timeline, open the actionscript panel and insert this code.

    This will create a virtual timer that goes off every 300 milliseconds (there are 1000 milliseconds in one second)
    Everytime it goes off a function called 'onTick' is called by the timer.
    onTick() will create a new copy of GrowBox, position it at the center of the stage and then place it on the stage.
    If the timeline of GrowBox has no stop()'s on it, it will just play. You can play with the 300 milliseconds number until you get something you like.
    Code:
    // frame 1
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.MovieClip;
    
    var box:MovieClip;
    var timer:Timer = new Timer(300);
    timer.addEventListener(TimerEvent.TIMER, onTick);
    timer.start();
    function onTick(event:TimerEvent):void{
    	box = new GrowBox();
    	box.x = stage.stageWidth / 2;
    	box.y = stage.stageHeight / 2;
    	addChild(box);
    }
    So that's a quick solution but there's some hidden problems. One is that the stage will continue to get new copies forever, each one playing repeatedly. Over time this will slow down your playback and possibly overlap so much that it just becomes one big solid square.

    You can help this by clearing each box as it finishes. Flash won't know that it's done unless you manually tell it by dispatching an event. It sounds complicated but it basically works like one thing making a cell phone call to another thing with a particular message. The recipient of that call will have a list of messages it is supposed to respond to and a specific response to each message. (that's what the addEventListener() command does in the above code) Event dispatching is really the heart of as3 so getting a grip on it is extremely important.

    So two things need to happen, first, as each box is created, we need to tell flash to "listen" for an "event" coming from that box. We'll call it "BOX_GROW_COMPLETE" but you could call it anything, as long as the exact same text gets sent out.

    Second, our box movieclip symbol needs to make this phone call (dispatch an event) on its last frame.
    Add a layer to its timeline and create a keyframe on the last frame (the same frame as the end of the shape tween) by right clicking and choosing Insert Blank Keyframe.
    Select that frame an in your actionscript panel insert this:
    dispatchEvent(new Event("BOX_GROW_COMPLETE"));

    Now we need to tell flash to listen for that message and how to react to it. Since flash keeps a list of those messages, we need to stay clean and clear that listener when we don't need it anymore. Below is the updated code with comments explaining what and why.

    Code:
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.MovieClip;
    import flash.events.Event;
    
    var box:MovieClip;
    var timer:Timer = new Timer(300);
    timer.addEventListener(TimerEvent.TIMER, onTick);
    timer.start();
    function onTick(event:TimerEvent):void{
    	box = new GrowBox();
    	// here we tell flash to listen for a message coming from each box
    	// when the message "BOX_GROW_COMPLETE" comes in, react to it by
    	// calling a function called onGrowComplete
    	box.addEventListener("BOX_GROW_COMPLETE", onGrowComplete);
    	box.x = stage.stageWidth / 2;
    	box.y = stage.stageHeight / 2;
    	addChild(box);
    }
    
    // onGrowComplete does a few things.
    // first it determines which box just called in, then it tells flash to
    // stop listening for messages from it, then it removes the box from the stage
    function onGrowComplete(event:Event):void{
    	// this function got passed an Event object named 'event'
    	// event holds some information about the event including who sent it which called 'target'.
    	// since event's target property could be anything we are telling flash to
    	// treat it like a MovieClip;
    	var box:MovieClip = MovieClip(event.target);
    	// Now that we have the sender, we can tell flash to stop listening to it
    	box.removeEventListener("BOX_GROW_COMPLETE", onGrowComplete);
    	// Now that we've cleaned up after ourselves, we can remove it from the stage
    	// NOTE: This does not delete the box, it still exists in memory but Flash has an
    	// internal mechanism called the Garbage Collector or GC. The gc will regularly check
    	// for orphaned objects and delete them from memory. Since our box has no one listening
    	// to it and is not on the stage, it will get garbage collected automatically
    	// If we failed to remove the listener, it would stick around
    	removeChild(box);
    	
    }
    Last edited by jAQUAN; 04-09-2013 at 07:27 AM.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center