TasostheGreat
TasostheGreat

Reputation: 432

load image on movieclip registration point problem

to load an image onto a movieclip, the mc's registration point must be top left. Is there a way of loading the image on the whole movieclip, when it's reg point is not top left. In other words to set where to start putting the image in reference to the movieclip

Upvotes: 0

Views: 334

Answers (2)

hardik
hardik

Reputation: 9319

yes you can.

its really simple thing that after adding objects into MovieClip you can position it every where by using its x and y properties.

like

var mc:MovieClip = new MovieClip();
addChild(mc);

var mc_child:MovieClip = new MovieClip();
mc.addChild(mc_child);

mc_child.x = //whatever you like.
mc_child.y = //whatever you like.

Upvotes: 1

Jacksonkr
Jacksonkr

Reputation: 32207

Sure thing.

Side note: Any child added to a DisplayObject places it's own registration point at the clip's registration point unless otherwise specified (sounds like you know this well). It's a bit tricky to "override" this behavior, but there's some work-arounds:

import flash.display.Sprite;

// assume parentClip already has a single image inside of it at -100,-100

var newClip:Sprite = new Sprite();

// add a peachy-red box to the child (to give us something to look at)
newClip.graphics.beginFill(0xff9999);
newClip.graphics.drawRect(0, 0, 200, 200);
newClip.graphics.endFill();

// Essentially, tell the newClip to show up wherever the pre-existing child is
var curChild:DisplayObject parentClip.getChildAt(0)
newClip.x = curChild.x;
newClip.y = curChild.y;

parentClip.addChild(newClip);

There's a lot you could do with this too. If you're wanting to get real fancy you could make a custom class that extends a DisplayObjectContainer that would override the method addChild the check the wanted parent for existing children and get X Y coords of the child that is the furthest most top|left.

A custom class would look something like:

package {
    import flash.display.DisplayObject;
    import flash.display.Sprite;

    public class MySuperContainer extends Sprite {
        public function MySuperContainer() {
            super();
        }

        override public function addChild(child:DisplayObject):DisplayObject {
            var to_x:Number = 0;
            var to_y:Number = 0;

            for(var i:int = 0; i < this.numChildren; ++i) {
                var c:DisplayObject = this.getChildAt(i);
                if(c.x < to_x) to_x = c.x;
                if(c.y < to_y) to_y = c.y;
            }

            child.x = to_x;
            child.y = to_y;

            return super.addChild(child);
        }
    }
}

Granted the above example a bit more "advanced", but extending classes and polymorphism is very valuable OOP info for coders if you're not familiar with them already. Also, my class example extends a Sprite, but you can extend any item that is a DisplayObjectContainer eg. MovieClip etc. Good luck!

Upvotes: 2

Related Questions