Solved

AS2:  onPress not working with createEmptyMovieClip

Posted on 2010-09-15
4
894 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

747 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

12 Experts available now in Live!

Get 1:1 Help Now