RePost: center movie clips dynamically, as3.0 ?

I have a question that was somewhat answered but I now have some problems with, as far as implementing the solution is concerned.  I posted in August and I have revisited the file only now to see if I can create the changes necessary to center my mc's dynamically as they are drawn on the stage at runtime.  They need to center to themselves rather than having their registration points in the top left corner, per display object.
Here is the thread:
http://forums.adobe.com/message/5760947#5760947
Thanks in advance for any responses.
-markerline

I attempted to reply yesterday but the forums became under maintenance (as we all re-logged in after).
What I tried to post before the system went down was to give a single example as you had mentioned. To remove the complexity of the rest of the app so this can be understood alone and then implemented into your larger system.
Centering via container in code is very simple as long as you can grab a hold of the shape in code as well.
Here's a complete AS3 example of centering a single object inside a container. I just want you to paste this into a new AS3 doc so you can tell me you understand how it works. After that, the more complex multi-object container in a container approach comes in:
e.g. 2 squares rotating:
import flash.display.Shape;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
// start rotation loop using a Timer to turn all objects
var moveTimer:Timer = new Timer(10,0);
// function to rotate objects
moveTimer.addEventListener(TimerEvent.TIMER, handleTimer);
// draw a rect shape
var redrect:Shape = new Shape();
redrect.graphics.lineStyle(3,0x0,1,true);
redrect.graphics.beginFill(0xFF0000);
redrect.graphics.drawRect(0,0,100,100);
redrect.graphics.endFill();
addChild(redrect);
// position at x:100 / y:100 on stage
redrect.x = 150;
redrect.y = 200;
// now draw a blue square but with a container while centering the shape
// container
var blueContainer:Sprite = new Sprite();
addChild(blueContainer);
// draw a rect shape
var bluerect:Shape = new Shape();
bluerect.graphics.lineStyle(3,0x0,1,true);
bluerect.graphics.beginFill(0x0000FF);
bluerect.graphics.drawRect(0,0,100,100);
bluerect.graphics.endFill();
blueContainer.addChild(bluerect);
// position in center of container (subtract half width/height)
//-------------centering code----------------
bluerect.x -= bluerect.width / 2;
bluerect.y -= bluerect.height / 2;
// position container
blueContainer.x = 400;
blueContainer.y = 200;
// start timer which invokes function below
moveTimer.start();
// rotate the red rect (upper left reg) and blue (objects centered);
function handleTimer(e:TimerEvent):void
          // just rotate both to see the registration
          redrect.rotation += 2;
          blueContainer.rotation += 2;
Now do understand I know I can draw my bluerect with negative coordinates to achieve the same thing inside the shape (e.g. -50,-50,100,100) but the point here is containing potentially a complex object into a single object so the entire outer contents can be measured and rotated from a single center point. That comes after this simple code is understood.

Similar Messages

  • Center movie clips dynamically, as3.0

    Hi.  I have seen this discussed both on Adobe Forums and other sites but I can't seem to make sense of the code supplied by what I have seen thus far.
    I am trying to center display objects that are added to the stage at runtime dynamically through a basic drawing application.
    the code is something like this:
    1. add an event listener for the drawing event.
    2. add the path you want to draw to a container movie clip on the stage.
    3. add an enter-frame event listener to change the scale of the path randomly .... and this is the moment i want to change the default top,left registration point to the center,center of the path.  each path has its own registration point as i can tell thus far but at this point the scale is happening from the top,left of the container rather than about the path's own self.  and if it were to happen about the path i would have to get the bounds of the path and do some averaging to center the registration point.
    unfortunately i don't know what the code looks like to achieve what i'm trying to get.
    any help is appreciated.  thanks in advance.

    Well if using the graphics.lineTo method is Bitmap data then that is what I'm using.  But each call to the shape that I'm drawing is a new Shape(); .  The second paragraph you wrote seems a bit daunting when viewing it in the abstract sense.  Your first reply was a bit more concrete but I guess the first response didn't account for everything being in one container.  Should I create a container2 Movie Clip and create that as a new box on a per-shape basis so that I have multiple container2 mc's in the following path:
    container.container2[i].shape[i] where i would be an index (such that i++) though the code wouldn't be written exactly that way. ?
    (To store localX,Y positions in an array or vector shouldn't be too hard but I would need to try it several times before I get it right).

  • Moving a Movie Clip dynamically

    Hi All,
    I want to move a movie clip dynamically on a predifined
    track. How could I do this? help me.
    Thanks in advance
    Ankur

    Ankur Arora wrote:
    > Hi All,
    > I want to move a movie clip dynamically on a predifined
    track. How could I do this? help me.
    what kind of "predefined track" is that ?
    Best Regards
    Urami
    !!!!!!! Merry Christmas !!!!!!!
    Happy New Year
    <urami>
    If you want to mail me - DO NOT LAUGH AT MY ADDRESS
    </urami>

  • How do I create a play button for movie clip using AS3

    Hello.
    Bit new to this and have been searching the adobe forums and google. I am trying to create a button that will play an imported MovieClip.
    I have a movie clip that I've imported into it's own layer (Layer1) Frame 1.  I've given the MovieClip an instance name of foundationMovie.
    I've created a button and have that on its own layer(Layer2) above layer1, it is also in Frame 1.
    I've added a
    stop (); action to layer2
    The video is not set to autoplay.
    I want the video to start playing when the viewer clicks the button.  I know I've got to put some kind of event listener using AS3 in my timeline somewhere.
    I've found some code and I've been trying to work with it.  I'm sure I've mutilated it beyond usability but this is what I've got.
    stop ();
    player.addEventListener(MouseEvent.CLICK,play);
    function play(event:MouseEvent)
    gotoAndPlay("foundationMovie");
    If someone could point me the right direction or help me with my code I would really appreciate it.  Thanks.

    First, you need to clear up what you are working with... a movieclip and a video are usually entirely different things.
    Next, your code may get you started on your way with some changes.   You shouldn't use "play" as a function name due to there already being a play() method/function in Flash.
    If the movieclip is a movieclip and has an instance name of "foundationMovie", and your button has an instance name of "player" then the following adjusted code would normally work...
    stop ();
    player.addEventListener(MouseEvent.CLICK,playMovie);
    function playMovie(event:MouseEvent)
         foundationMovie.play();

  • Putting movie clips in AS3 package into movie clip on stage

    Hi - I am a as2/flash animator and am working on an AS3
    project. The AS is in a separate file, inside a package. I am
    modifying existing code.
    The script adds movie clips in a function via addchild.
    What I am hoping to do, is have the movieclips added to a
    clip that already exists on the stage.
    I put a clip on the stage named holder, with the instance
    name holder_mc - and I tried to add the clip with
    holder_mc.addchild in the AS3 file - it seemed to work, but
    manipulating the holder_mc - it looks like the added clips are not
    affected?
    Any help is appreciated.

    Child_array is a property of the Parent class.
    It's only added to in one point, in a function that creates a
    Child and adds it to the array.
    I think I might have found out the problem with my code after
    adding a trace to the function, think I'm creating twice the number
    of clips I'm removing, and the deletion part of my code is fine!
    Turns out AS3 is maybe not so bad after all :P
    Thanks alot for taking the time to reply though kglad!

  • Moving Movie Clips dynamically

    Here's the deal. I have a function that I call to move the
    movie clips based on string that holds the movie clip instance
    name. The function as I have it now can be seen below listed under
    figure A. Figure B shows my call to that function. It takes in an
    array of strings and passes the current index to the function.
    Figure C shows my output panel text after I run the function.
    For some reason, the function is only moving one of my
    movieclips, and none of the others. I can tell this by tracing the
    instance ._x value, as shown in figure D. So, I know that my calls
    to the movieclip instances are correct, but why can I only move one
    card?? I know the movie clips are instantiated, as can be seen in
    Figure E, which show my objects list that I get from my debug menu.
    Any ideas/suggestions?

    My latest crack at it... I create multiple controllers for
    each movie clip, and I test the calling of the method by inserting
    a string literal as the call, still no luck. No matter how many
    times I call move, it will only work on the last card that I call.
    Framerate does not matter.

  • How to resize movie clip dynamically

    how to resize movie clip sccording the stage size and width

    how to resize movie clip sccording the stage size and width

  • Add movie clip in AS3

    i want to have a movie clip come up on a specific pixel
    coordinate. how would i finish this code?

    i made a new forum with everything. the only part it says is
    wrong in the compiler errors was the coordinates at the end

  • Controlling Movie Clip with as3

    I try to controll my mc. I want put one MovieClip to other.
    i use next code:
    box_mc.addChild(pic_mc);
    but its not working.
    box_mc this is the a box movie clip, in this mc i want put my pic_mc.

    In addition to what dmennenoh said,
    DisplayObject's x and y properties are preserved no matter parent. In your case, it seems, original position of pic_mc is not 0/0. So when you place it inside the box - x and y are the same but now they are RELATIVE TO BOX. So, on screen it looks like it is outside the box but it is inside except that box's x and y are added.
    For instnace, if the original coordinates are pic_mc.x = 200, pic_mc.y = 100 and box_mc.x = 50, box_mc.y = 20, when you add pic_mc as a child, it will have the came x and y but relative to box_mc (not its original container) and on the screen it will look like it is on x = 250 (box_mc.x + pic_mc.x) and y = 120 (pic_mc.y + box_mc.y).
    box_mc's dimensions also change IF pic_mc x and y plus its width and height are outside of original box_mc boundaries. After you add child box_mc.width will be pic_mc.x + pic_mc.width and box_mc.height will be pic_mc.y + pic_mc.height.
    I suspect that because there is a border in the box, you expected objects to placed within the border. Border in the box IS NOT the box but ANOTHER object (child) inside the box. In other words, box is a container. After pic_mc is added - it will contain both border and pic_mc - not border containing pic_mc. You see the difference?

  • Play animation (movie clip) randomly as3

    Hello,
    Could anyone out there tell me what as3 code i should be using to make this happen.
    I want the animation (shooting star) to play at random times.
    Many thanks indeed.
    Damien

    on the last frame of your movieclip attach:
    stop();
    this.dispatchEvent(new Event("finished"));
    right click your movieclip in the library, click linkage, tick export for actionscript and assign a class (eg, Starmove).
    you can then use:
    var minTime:uint = 1000;
    var maxTime:uint = 3000;
    var timer:Timer=new Timer(timeF(),0);
    timer.addEventListener(TimerEvent.TIMER,starF);
    timer.start();
    function starF(e:TimerEvent){
    var star:Starmove=new Starmove();
    addChild(star);
    star.x = Math.floor(Math.random()(stage.stageWidth-star.width));  // these two will probably need adjustment
    star.y = Math.floor(Math.random()(stage.stageHeight-star.height)); // ditto
    star.addEventListener("finished",removeStarF);
    timer.delay = timeF();
    function removeStarF(e:Event){
    removeChild(e.currentTarget);
    e.currentTarget.removeEventListener("finished",removeStarF);
    e.currentTarget=null;
    function timeF():uint{
    return minTime+Math.floor(Math.random()*(maxTime-minTime));

  • Dynamic  Instance for a MOVIE CLIP

    Hi Please help me!!!!!!!!!!
    I am creating dynamic Movie Clip using as3, but I don't have
    any idea about dynamic Instance name, please give me some idea that
    how I can assign Instance name for a movie clip.
    I am waiting your reply.
    Thanks
    Sushil Kumar

    You can assign a value to the name property of the MovieClip,
    but refering to the variable name of your MovieClip instance is
    preferable. This code illustrates the difference:

  • Can I change the registration point of a movie clip?

    I'm loading a movie clip dynamically and its registration
    point defaults to top-left corner. I need to change it to the
    center, is this possible? if so, how?
    Thank you

    Thank you. I tried that and it didn't work too. Please check
    this code:
    loader.loadClip(_global.image_url+this["image1"], image1);
    DynamicRegistration.initialize(image1);
    image1.setRegistration( 20, 20);
    What is wrong with it?

  • Making movie clips play in reverse in AS 3.0

    Hey all. I'm changing all my files to AS 3.0 because of some
    new elements I'd like to use in flash CS4.
    Here:
    http://www.iaw-atlanta.com/IvyLeague1-13-09.html
    I have the menu navigation at the bottom with 4 buttons. When the
    buttons are moused over they ascend and when they are moused out
    the movie plays in reverse so that they descend. Basically in the
    movie clip, I have it stopped as well as gotoAndPlay the next frame
    when the button is moused over and then have a script telling it to
    reverse when moused out. I know how to do this in AS 2.0 but how
    can I translate the script to 3.0?

    DjPhantasy5,
    > Ok thanks, I'll see what he also has to say as well.
    Actually, NedWebs pretty much nailed it. :) The approach
    you've taken
    is an interesting one, because it scales all the way back to
    Flash Player 5,
    which didn't yet support the MovieClip.onEnterFrame event.
    Without that
    event, you need something like your "controller" symbol -- a
    separate
    looping timeline -- to perform code that depends on frame
    loops.
    Available as of Flash Player 6, that event would allow you
    to
    consolidate your code a bit by putting it all in a single
    frame and avoiding
    the extra movie clip symbol. That doesn't mean your approach
    is wrong, by
    any stretch. In fact, it's good to know your options, because
    if you ever
    need to publish to Flash Player 5, you'll probably do best to
    use the setup
    you've taken.
    I'll demonstrate onEnterFrame in just a bit, in case you
    want to take a
    look at that.
    >> there's no _root reference in AS3, but you can use a
    >> MovieClip(this.parent) (which replaces what _parent
    >> used to do) to locate something outside of the
    immediate
    >> movieclip.
    The term "parent," in this context, is technically a
    property of the
    MovieClip class, which means all movie clips in AS3 have
    access to the
    "parent" feature, which points to the timeline that contains
    the movie clip
    in question. Buttons also feature a "parent" property. In
    AS2, this
    property acted exactly the same, only it was preceeded by an
    underscore.
    In fact, NedWebs' suggested code works the same in your
    existin file.
    Instead of this:
    // Your existing code ...
    if (_root.animation._currentframe>1) {
    _root.animation.prevFrame();
    this.gotoAndPlay(2)
    if (_root.animation._currentframe<=1) {
    this.gotoAndStop(1);
    ... you could be using this:
    // Alternative code ...
    if (_parent.animation._currentframe>1) {
    _parent.animation.prevFrame();
    gotoAndPlay(2)
    if (_parent.animation._currentframe<=1) {
    gotoAndStop(1);
    ... because from this scope -- that is, from this point of
    view -- the
    MovieClip._parent property points to the same timeline that
    _root does.
    That changes, of course, based on how deeply nested the
    current scope is.
    The term "this" in the above code is optional, because Flash
    understands
    what scope you're in. From the point of view of this code,
    _parent is
    understood to refer to the current scope's parent timeline,
    just like
    gotoAndPlay() is understood to refer to the current scope's
    own timeline.
    You could precede either of those by "this" or not.
    Many people will argue that _root is probably best avoided,
    only because
    it's a slippery slope. It doesn't always refer to the main
    timeline you
    think it does, depending on whether or not your SWF is loaded
    into another
    SWF at runtime. Meanwhile, _parent *always* refers to the
    immediately
    parent timeline.
    So when you look at it that way, this perfectly valid AS2:
    if (_parent.animation._currentframe>1) {
    ... isn't much different at all from its AS3 counterpart:
    if (MovieClip(parent).animation.currentFrame>1) {
    It would be nicer if you didn't need to cast the parent
    reference as a
    movie clip here, but if you don't, AS3 doesn't realize that
    the scope in
    question is a movie clip.
    >> You can string them along as...
    >> MovieClip(this.parent.parent)
    Exactly.
    >> Your _currentframe in the the controller becomes
    >> currentFrame. That's about it. The rest of what I
    >> saw will fly as is.
    Not quite. AS3 doesn't support the on() or onClipEvent()
    functions, so
    you won't be able to attach your code direction to the
    button. Instead,
    you'll have to put it in a keyframe -- which you can also do
    with AS2. It's
    a good idea, in any case, because it allows you to put all
    your code in one
    place, making it easier to find.
    Here's a quick note on direct attachment:
    http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent
    To convert your existing FLA structure to AS3, you might,
    for example,
    do this:
    animation.buttonMode = true;
    animation.addEventListener(MouseEvent.ROLL_OUT, outHandler);
    animation.addEventListener(MouseEvent.ROLL_OVER,
    overHandler);
    function outHandler(evt:MouseEvent):void {
    controller.gotoAndPlay(2);
    function overHandler(evt:MouseEvent):void {
    controller.gotoAndPlay(4);
    That takes care of the on(rollout) and on(rollover) code in
    your current
    version. The first line (buttonMode) is only necessary
    because I got rid of
    the button symbol and, instead, associated the event handlers
    directly with
    your animation clip. (Movie clips handle button-style events,
    too, so you
    don't need the button.) In AS3, event handling is very
    straightforward:
    you reference the object (here, animation), invoke its
    inherited
    addEventListener() method, then pass in two parameters: a)
    the event to
    listen for, and b) the function to perform when that event
    occurs.
    You can see that spelled out above. In AS2, there are quite
    a few ways
    to handle events, including on()/onClipEvent(), so it's
    harder to know when
    to use what approach. For buttons and movie clips, the
    princple works the
    same as I just showed, but the syntax is different.
    Let's take a quick look at reducing all this code by using
    the
    onEnterFrame event. First, check out this AS2 version:
    animation.onRollOut = outHandler;
    animation.onRollOver = overHandler;
    function outHandler():Void {
    this.onEnterFrame = reversePlay;
    function overHandler():Void {
    this.onEnterFrame = null;
    this.play();
    function reversePlay():Void {
    this.prevFrame();
    All of that code would appear in frame 1. You don't need the
    controller
    movie clip. The animation clip no longer contains an orb
    button, because
    we're using animation itself as the "button". (You can always
    see what
    functionality any object has by looking up its class. If
    you're dealing
    with a movie clip, look up the MovieClip class. See the
    Properties heading
    to find out what characteristics the object has, see Methods
    to find out
    what the object can do, and see Events to find out what the
    object can react
    to.)
    In the above code, it's all laid out pretty easily. This is
    AS2,
    remember. We have two events we're listening for:
    MovieClip.onRollOut and
    MovieClip.onRollOver. Those events are established for the
    animation movie
    clip by way of its instance name, and associated with
    corresponding custom
    functions. The outHandler() function associates yet another
    function to the
    MovieClip.onEnterFrame event of the animation clip. Very
    simply, it
    instructs animation to perform the custom reversePlay()
    function every time
    it encounters an onEnterFrame event. The overHandler()
    function kills that
    association by nulling it out, and telling animation to play,
    via the
    MovieClip.play() method. Finally, the reversePlay() function
    simply invokes
    MovieClip.prevFrame() on the animation clip.
    Here's the AS3 version:
    animation.buttonMode = true;
    animation.addEventListener(MouseEvent.ROLL_OUT, outHandler);
    animation.addEventListener(MouseEvent.ROLL_OVER,
    overHandler);
    function outHandler(evt:MouseEvent):void {
    animation.addEventListener(Event.ENTER_FRAME, reversePlay);
    function overHandler(evt:MouseEvent):void {
    animation.removeEventListener(Event.ENTER_FRAME,
    reversePlay);
    animation.play();
    function reversePlay(evt:Event):void {
    evt.target.prevFrame();
    It looks wordier, but if you look carefully, you'll see that
    it's
    essentially the same thing. The main difference is the way
    scope works in
    AS3. In AS2, there are numerous references to "this", because
    the event
    handler functions operate in the same scope as the object to
    which the
    association is made. In AS3, the references aren't to "this",
    but rather to
    the same animation clip by its *intance name*, because in
    AS3, the scope
    operates in the same timeline in which the code appears
    (namely, in this
    case, the main timeline).
    In the reversePlay() function, in order to emulate a "this"
    sort of
    setup, I'm referring to the parameter that gets passed to all
    event handler
    functions in AS3. I happened to name it "evt" (for event),
    but you can call
    it what you like. In the case of reversePlay(), the evt
    parameter refers to
    an instance of the Event class, which has a target property.
    The target
    property refers to the object that dispatched the event --
    happens to be
    animation, in this case. So evt.target, in this context,
    refers to
    animation, which is what I invoke the MovieClip.prevFrame()
    method on.
    I've uploaded a handful of FLA files to my server. I won't
    keep them
    there forever ... maybe a couple weeks, as of this post:
    http://www.quip.net/RewindTestAS3.fla
    http://www.quip.net/RewindTestAS3_new.fla
    http://www.quip.net/RewindTestAS2_new.fla
    David Stiller
    Co-author, ActionScript 3.0 Quick Reference Guide
    http://tinyurl.com/2s28a5
    "Luck is the residue of good design."

  • How do you horizontal scroll a movie clip?

    Can anyone suggest any resources for horizontal (left to right) scrolling a movieClip? I want to be able to scroll through a movie clip that has pictures in it. I can't find any good ones that explain the code. FYI, I do not want the user to use a scroll bar to scroll through. They should be able to touch the screen (on the movieClip) and scroll left or right. I looked at the ThrowPops plugin example on Greensock's website but the code is not explained very well.  So I need a basic description of how to scroll a movie clip.

    google: as3 beginner tutorial flash ios movieclip scroll

  • Transform on movie clips

    I am loading a bunch of movie clips dynamically. Right now,
    the movie clips undergo a color transform for the onRelease()
    action. Now, I want to be able to transform a group of movie clips
    on the onRelease() action of a single movieclip.
    What would be the recommendation to accomplish this task? I'm
    sure there are many ways to do it, but I don't know where to start.
    I tried to put the movie clips into an array to then access the
    movie clips. I can get the properties of the movieclips that I want
    to transform, but I cannot transform them.

    This is the code that I use:
    for (var j:Number = 0; j<quad.length; j++) {
    var bu_mc:MovieClip = quad[j];
    bu_mc.select();
    trace("xPos: "+bu_mc._x+" building: "+bu_mc._name);
    The correct _x and _name are displayed, but nothing happens
    to the movieclip.
    select() is a function that I've defined. It works when
    called by the movieclip itself, but is not working when called by a
    different movieclip.

Maybe you are looking for