views:

804

answers:

2

i have a loaded swf fil, in this file, a movieclip named ball on it, this movieclip move when i press a button, at the moment, i want to add a text on this movieclip, as movieclip moving, text move with it as if they are the same object

A: 

Umm. I'm not sure what exactly you wish to do, but If I understand you correctly, there are a few ways of implementing this.

OPTION 1:

button.addEventListener(MouseEvent.CLICK, followFunc);

private function followFunc(event:MouseEvent):void {
    removeChild(text);
    movieClip.addChild(text);
}


OPTION 2:

button.addEventListener(MouseEvent.CLICK, followFunc);

private function followFunc(event:MouseEvent):void {
    stage.addEventListener(Event.ENTER_FRAME, updateFunc);
}

private function updateFunc(event:Event):void {
    text.x = movieClip.x; //possibly + or - some offset
    text.y = movieClip.y;
}
PiPeep
By the way, why are you using MovieClips? Sprites are faster if you don't need the frames.
PiPeep
thanks, my english is very poor,so i can not express myself very collectly. let me try to explain it again. i want to attach a text to a ball run as a movieclip when i click a button,when the button clicked, the ball move from bottom to top, the text will right on the ball and move with it.
i use the option 1 you point out,but the text did not appear on the ball but in other place, when the ball move, the text still there
i did not use sprite because my ball is on a time frame
A: 

Since the ball is on a timeline, the simplest approach may be to put a textfield in the same MovieClip that the ball is in. Something like the ball asset is on Layer 1, and the textfield is on Layer 2.

Position the textfield as you want it to be, on the ball.

You would need to name the textfield -- something like 'ballTextField', maybe.

Name your ball MovieClip, when you add it to the stage (either in the Flash authoring environment, or programatically). Call it something like 'ball'.

Then:

button.addEventListener(MouseEvent.CLICK, buttonClickListener);

private function buttonClickListener(e:MouseEvent):void
{
    ball.ballTextField.text = "Hi.  This text displays on the ball";
}

Essentially, this makes the text and the ball the same object (or two parts of the same object, really).

Ross Henderson
thank you,i will try it