Solved

AS2:  onPress not working with createEmptyMovieClip

Posted on 2010-09-15
4
904 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

627 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