Hi

I'm relatively new to Actionscript coding and have a small problem in Flash.

I want to track the x property of a symbol (in this case a movie clip) and display the x property of that movie clip in a text field. However I want the text field to update the x property of the movie clip as it moves across the workspace. At the minute all the code I have does is track the starting x property and display that in the text field.

Here's my code:

var xCo = box.x; //Box is the symbol's instance name

xCo_txt.text = xCo; //xCo_txt is the text fields instance name

Thanks for any help and if any more information is required please ask and I will provide it.

Andy

Dani AI

Generated

's ENTER_FRAME suggestion and 's working snippet correctly solve the immediate problem of showing a MovieClip's changing x value in a text field. The notes below add practical refinements and common gotchas so the solution stays robust and efficient in larger projects.

A small optimization: only update the TextField when the x value actually changes (reduces string work and rendering). Also show numeric formatting with toFixed rather than raw String() conversions.

import flash.events.Event;

var lastX:Number = NaN;
addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(e:Event):void {
    var curX:Number = box.x;
    if (curX != lastX) {
        lastX = curX;
        xCo_txt.text = curX.toFixed(0); // integer display; change precision as needed
    }
}

If the MovieClip is nested, box.x is relative to its parent. To display stage/global coordinates use localToGlobal:

import flash.geom.Point;

var p:Point = box.localToGlobal(new Point(0,0));
xCo_txt.text = p.x.toFixed(1); // stage X with one decimal

Troubleshooting checklist (common causes when the value doesn't appear or update):

  • The text field must be Dynamic (not Input) and have the exact instance name xCo_txt.
  • Code must run on the same frame as the instances or after they are created; otherwise defer the listener until creation.
  • Remove listeners when no longer needed (removeEventListener) to avoid memory/performance issues.
  • For many objects or low-frequency updates, prefer a Timer or throttle updates instead of ENTER_FRAME.
  • If fonts look wrong, ensure fonts are embedded when using TextField with device fonts turned off.

These tips keep the simple ENTER_FRAME approach used by and reliable and performant as the project grows.

Recommended Answers

All 3 Replies

Hey Andy.
You might want to try adding an event listener for the ENTER_FRAME event and set up a handler function to update the text box each time the event is detected.

The ENTER_FRAME event is an event which is dispatched by Flash's event dispatcher each time a new frame is drawn. So by adding an event listener and a handler, you can update the text box each time a new frame is drawn.

Here's a simple, yet complete example of what I mean (Note: This is done in AS3/Flex not the Flash IDE...I've not used the Flash IDE for years!):

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	/**
	 * Example using Flex SDK.
	 * Creates two text fields.
	 * Uses an event listener and a handler function to update the text-boxes
	 * with the X and Y positions of the mouse cursor.
	 */
	[SWF(backgroundColor="0xffffff", width="800", height="600", frameRate="25")]
	public class UpdateText extends Sprite 
	{
		private var text1:TextField;
		private var text2:TextField;
		
		public function UpdateText()
		{
			// Add two text fields to the stage.
			text1 = new TextField;
			addChild(text1);
			text2 = new TextField;
			text2.y = 20; // place text2 directly underneath text1
			addChild(text2);
			
			// Set up an event listener to listen for the ENTER_FRAME event.
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		// This is the function we specified in our event listener.
		// This will deal with updating the text boxes each time the listener
		// detects an ENTER_FRAME event 
		private function onEnterFrame(e:Event):void
		{
			text1.text = "Mouse X = " + String(mouseX);
			text2.text = "Mouse Y = " + String(mouseY);
		}
	}
}

The above example creates two text boxes and sets up an event listener and an associated handler function for the ENTER_FRAME event. Each time ENTER_FRAME is detected, the listener calls the handler function (onEnterFrame), which updates the two text boxes with the current X and Y positions of the mouse cursor.
OK, so I'm using the mouse co-ords, but it works exactly the same way for any other objects properties!

However, the above example is targetted towards using the Flex SDK rather than the Flash IDE. But it's still AS3, so the principles behind it still apply in the IDE.
So assuming you're using the Flash IDE; for your purposes, wherever you've currently got your actionscript for setting the text for xCo_txt, you'll probably need to add something like this:

function onEnterFrame(e:event):void
{
    xCo_txt.text = String(box.x);
}

this.addEventListenter(Event.ENTER_FRAME, onEnterFrame);

That might do the trick... It might not be exactly right, but as I said I've not used the flash IDE for a loooong time. Either way you almost certainly need to add an event listener and a handler function IMHO!

Thank you so much for the detailed explanation, the example was particularly useful.

This is the code that works:

import flash.events.Event;

this.addEventListener(Event.ENTER_FRAME, EnterFrame);

function EnterFrame(e:Event):void
{
    xCo_txt.text = String(box.x);
}

Relatively identical to your's as you can see. Your knowledge of the Flash IDE is still as good as ever!

Thank you again!

No worries, glad to have helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.