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

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.