Solved

AS2:  onPress not working with createEmptyMovieClip

Posted on 2010-09-15
4
895 Views
Last Modified: 2012-05-10
Okay, I know it's a pain to deal with AS2, but this is really stumping me.  I have a Debug class that essentially creates a new window, and then writes to a text field within that window.  In any case, I would like to make sure the window is draggable.  The problem is that after I create the window on the stage via the createEmptyMovieClip method, and draw a background into that window, I cannot get assign something to work with the .onPress handler.  Here is my code for the constructor and building of the debug window.  The problem is that the .onPress handler is not firing off!  Why is that and how can I ensure that it will work.  BTW, I commented out all the stuff re: the text field, because that is not important at the moment.

Thanks!
class Debug extends Object {
 
 	/* define properties */
	public var verb;
	public var _dName:String;
	public var dWindow:MovieClip;
	public var dField:TextField;
	
	/* define constructor */
	public function Debug(initParams:Object){
		
		if (initParams != undefined) {
			
			addDebugField(initParams);
			
		} else {
			
			var defaultConfig:Object = new Object();
			defaultConfig.name 		= "DefaultDebug";
			defaultConfig.width 	= Stage.width/4;
			defaultConfig.height 	= Stage.height/3;
			defaultConfig.color  	= 0xFFFFFF;
			defaultConfig.alpha		= 50;
			addDebugField(defaultConfig);
			
		}
	
	}
	
	/* define methods */
	public function addDebugField(param:Object)
	{
		
		/* create generic debug movie clip */
		dWindow = _root.createEmptyMovieClip(param.name, _root.getNextHighestDepth());
		var dElements = dWindow.createEmptyMovieClip("elements", dWindow.getNextHighestDepth());
		
		trace("Constructed a new debug window: " + dWindow);
		
		/* create background image for debug movie clip */
		drawRoundedRectangle(dElements, param.width, param.height, 8, param.color, param.alpha);
		
		/* create text field for writing debug messages */
		//dWindow.createTextField("readout", _root[param.name].getNextHighestDepth(), 0, 0, 400, 200);	
		/*
		with (dWindow.readout) {
			selectable = false;
		}
		*/
		
               // THIS DOES NOT WORK WHEN I CLICK IT THE DEBUG WINDOW
		dElements.onPress = function () { this._x = 0; trace("Begin moving") } 
		
		(verb==1) ? trace("Added the debug movieclip: " + dWindow) : '';
	}
	
	public function write(msg:String)
	{
		trace("Writing to debug field : " + _root.debug.readout);
		_root.debug.readout.text += "\n" + msg;
		
		var txt_format:TextFormat = new TextFormat();
		txt_format.color = 0x660000;
		txt_format.leftMargin = 15;
		txt_format.size = 16;
		_root.debug.readout.setTextFormat(txt_format);
		
	}
	
	private function drawRoundedRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, cornerRadius:Number, fillColor:Number, fillAlpha:Number):Void
	{
		with (target_mc) {
			beginFill(fillColor, fillAlpha);
			moveTo(cornerRadius, 0);
			lineTo(boxWidth - cornerRadius, 0);
			curveTo(boxWidth, 0, boxWidth, cornerRadius);
			lineTo(boxWidth, cornerRadius);
			lineTo(boxWidth, boxHeight - cornerRadius);
			curveTo(boxWidth, boxHeight, boxWidth - cornerRadius, boxHeight);
			lineTo(boxWidth - cornerRadius, boxHeight);
			lineTo(cornerRadius, boxHeight);
			curveTo(0, boxHeight, 0, boxHeight - cornerRadius);
			lineTo(0, boxHeight - cornerRadius);
			lineTo(0, cornerRadius);
			curveTo(0, 0, cornerRadius, 0);
			lineTo(cornerRadius, 0);
			endFill();
		}
    }

Open in new window



0
Comment
Question by:ariestav
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 33689959
if you can upload your files, will have a look. its just easier that way.
0
 
LVL 4

Accepted Solution

by:
ariestav earned 0 total points
ID: 33690964
I wish I could, but the .fla is proprietary.  I did, however, figure out a solution, although I don't know why it works.  Basically, what I did was to add a line of code *after* the assignment of the onPress and onRelease handlers.  The line was

dWindow.enabled = true;

Once I did that, the movieclip started to work.  However, what is most strange is that I didn't necessarily have to use the .enabled property.  If I placed a trace(dWindow.onPress) statement instead of the .enabled, it worked just fine.  I'm sure it's a bug in how Flash handles createEmptyMovieClip.  I don't think Adobe got it right with AS2.  In any case, here is the modified "addDebugField" function from above:


public function addDebugField(param:Object)

	{

		

		/* create generic debug movie clip */

		dWindow = _root.createEmptyMovieClip(param.name, _root.getNextHighestDepth());

		dElements = dWindow.createEmptyMovieClip("container", dWindow.getNextHighestDepth());

		

		trace("Constructed a new debug window: " + dWindow + " with container " + dElements);

		

		/* create background image for debug movie clip */

		drawRoundedRectangle(dElements, param.width, param.height, 8, param.color, param.alpha);

		

		//create a scale 9 grid for the movieclip

		var bgGrid:Rectangle = new Rectangle(10, 10, 734, 12);

		dElements.scale9Grid = bgGrid;

		

		/* create text field for writing debug messages */

		dWindow.createTextField("readout", _root[param.name].getNextHighestDepth(), 0, 0, 400, 200);	

		

		with (dWindow.readout) {

			selectable = false;

		}

		



		dWindow.onPress = function () { this.startDrag() }

		dWindow.onRelease = function () { this.stopDrag() } 

		dWindow.enabled = true;

		

		(verb==1) ? trace("Added the debug movieclip: " + dWindow) : '';

	}

Open in new window

0
 
LVL 4

Author Closing Comment

by:ariestav
ID: 33690973
Thanks for the offer, blue-genie.  Much appreciated!
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 33691390
np, glad u came right.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to how to record live broadcast.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now