Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 912
  • Last Modified:

AS2: onPress not working with createEmptyMovieClip

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
ariestav
Asked:
ariestav
  • 2
  • 2
1 Solution
 
blue-genieCommented:
if you can upload your files, will have a look. its just easier that way.
0
 
ariestavAuthor Commented:
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
 
ariestavAuthor Commented:
Thanks for the offer, blue-genie.  Much appreciated!
0
 
blue-genieCommented:
np, glad u came right.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now