charlescfenwick.com

February 14, 2009

Actionscript 3: Override a default public setter of an object.

This one can be pretty nifty. In my certain case I had a bunch of on screen items that I continually updating their alpha property but when there was a mouse over I didn’t want to update that property anymore and peg it right at 1 so it stood out. In this case it was a text field so here is a class with the not important parts taken out.

View CodeACTIONSCRIPT
package{
	import flash.text.TextField;
	import flash.events.MouseEvent;
	public class Tag extends TextField{
		private var alpha_locked:Boolean;
		public function Tag():void{
			this.alpha_locked = false;
			this.addEventListener(MouseEvent.MOUSE_OVER, mouse_over);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouse_out);
		}
		private function mouse_over(e:MouseEvent):void{
			this.alpha = 1;
			this.alpha_locked = true;
		}
		private function mouse_out(e:MouseEvent):void{
			this.alpha_locked = false;
		}
		override public function set alpha(a:Number):void{
			if(!alpha_locked) super.alpha = a;
		}
	}
}

The magic happens with the super.alpha, super which refers to the parent class that Tag inherits from. It checks the alpha_locked variable and if things are ok it sets it, otherwise it’s ignored. Then the alpha locked variable is controlled by the MouseOver and MouseOut events.

February 13, 2009

Actionscript 3: Rotating an object from an arbitrary point.

Filed under: Actionscript, Programing, Uncategorized, flash — Tags: , , — chuck @ 4:40 am

I’ve run into this problem a few times I want to rotate and object by an arbitrary point. First off actionscript doesn’t let you change you point from which you rotate when you change the rotation attribute of an object. By default when you rotate using the rotation attribute flash rotates the objects at it’s index point (0, 0). Simple solution move the object so it’s center point is at the index point of it’s container move clip then rotate it, then move it back it’s original position. To make things easier you can of course wrap this up in a function inside of an object or use this snippet.

View CodeATIONSCRIPT
	function rotate(item:DisplayObject, angle:Number, point:Point) {
		var matrix:Matrix = item.transform.matrix.clone();
		matrix.translate(-point.x, -point.y);
		matrix.rotate (angle*(Math.PI/180));
		matrix.translate(point.x, point.y);
		item.transform.matrix = matrix;
	}

Simple enough to use just pass the function the moveclip you want to rotate the angle just like you would with the rotation attribute and the point from the index point that you want to rotate your clip.

	rotate(thing, 90, new Point(50, 50));

Powered by WordPress