Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AS2:  onPress not working with createEmptyMovieClip

Posted on 2010-09-15
4
Medium Priority
?
908 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to count occurrences of each item in an array.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

722 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