A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: pass arguments into movie clip?

  1. #1
    Member
    Join Date
    Jun 2003
    Location
    San Antonio
    Posts
    43

    pass arguments into movie clip?

    Hello all,

    I have been given the requirments to modify a website intro. Basically it has names fly in and out one at a time. Each name that flys in has some text effect on it. They had each name set up as it's own movie clip.

    I want to make this more dynamic by putting the list of names into an XML file.
    Then after reading the XML file, pass each name one at a time into a movie clip. I thought I could set up several different movie clips for the different text effect and then randomly choose one.

    I know how to read the XML file, but that's about it.

    I need to know how to pass info into a movie clip.
    I need to know how to call random movie clips.
    And it would be nice to know how to make it loop through the names and have the effect appear on screen.

    I know, I know...I'm a total noob. I do have 15 years of programming experience, I just don't do flash that much and I'm trying to help some people out.

    Thanks so much for all your help.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your movie clips can have functions defined which take parameters. In your case, probably a String parameter for the name. That function would take the String, put it into a TextField, and place that textfield on stage if it is not already.

    To get a random number between 1 and N, you can do this:
    Code:
    Math.floor(Math.random()*N);
    You should keep your collection of possible clips in an array so that you can access it with the random index you've just got.

    To loop through, you could simply use a for-each loop on the XML nodes, but you'll probably want to actually give the animations time to complete. Use a timer for static timing, or dispatch an event from the last frame of the animation, or if you're using some sort of tween you can use a callback or listen for a complete event. To loop, just keep track of which one you showed last, and if it's the last in the list, show the first one in the list next time.

  3. #3
    Member
    Join Date
    Jun 2003
    Location
    San Antonio
    Posts
    43
    Thanks.
    Are there any samples out there I can look at?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't happen to know of any, but each part of your task is fairly straighforward.

    Let's say each of your effect movie clips has a textfield with the instancename "content", and a function called setText which takes a String and sets the textfield text. setText could look like this:

    Code:
    function setText(s:String):void{
      content.text = s;
    }
    That function would have to go in each effect clip. Or you could put it in a base class and have each effect clip extend that. I wouldn't worry about that yet.

    Let's say your effect clips are effect1, effect2, effect3. Here's an array with those contents, and a way to select a random one.

    Code:
    var effects:Array = [effect1, effect2, effect3];
    var randomEffect:MovieClip = effects[Math.floor(Math.random()*effects.length)];
    randomEffect.setText("someText");
    And let's say you've parsed your xml into an array of Strings
    Code:
    var names:Array = ["Alice", "Bob", "Carol"];
    var nameIndex:int = 0;
    var effects:Array = [effect1, effect2, effect3];
    
    function nextName(event:Event = null):void{
      var randomEffect:MovieClip = effects[Math.floor(Math.random()*effects.length)];
      randomEffect.setText(names[nameIndex]);
      randomEffect.goToAndPlay(0);
      nameIndex++;
      if (nameIndex >= names.length){
        nameIndex = 0;
      }
    }
    
    addEventListener("done", nextName);
    
    nextName();
    In the last frame of each of your effect/animation clips, you'd have to dispatch the "done" event.
    Code:
    dispatchEvent(new Event("done"));

  5. #5
    Member
    Join Date
    Jun 2003
    Location
    San Antonio
    Posts
    43
    Sweet,
    thanks 5TonsOfFlax. I see you are right up the street from where I am at.
    I will give this a try.

    Thanks.

  6. #6
    Member
    Join Date
    Jun 2003
    Location
    San Antonio
    Posts
    43
    Got another question for you if you got the time.

    I have a motion tween that adds effect to a Graphic Symbol. Is there a way for me to change that graphic dynamically to something I read out of my xml file? So every time the tween runs it uses a different graphic. Very much the same as above, except with a graphic instead of with text.

    Thanks,
    Michael

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I have no idea how your tweens work. If you're creating them through code, then you can almost certainly substitute a derived object for the one that's currently in there.

  8. #8
    Member
    Join Date
    Jun 2003
    Location
    San Antonio
    Posts
    43
    At this time they are not through code. I am modifying an old splash screen. So it has a bunch of tweens all copies of each other. Each one shows a different Graphic symbol. I am trying to get rid of all the copies and just have one. I'll make that "one" into it's own movie clip. I want to pass into it the url of my new jpg. And have that jpg become the graphic in my symbol.


    As I stated in my first post, I'm not really a flash programmer so I just trying to figure out the best way for me to do this. Sorry if I seem very noobish.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I've never used that sort of tween, and don't really know how it interacts with code. What I'd probably do (and this is not necessarily what you should do), is scrap all the existing tween stuff, get a library like TweenLite, and reproduce your tweens in code. Then you can very easily change the target when you create a new tween.

Tags for this Thread

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