Drag and select in ActionScript 3

Found this piece of code over at Kirupa that explains how to do a drag-and-select implementation in ActionScript 3.0:

import flash.geom.*;

var drawingBox:Sprite;/*resized sprite that acts as a selector*/
var stageObjects:Array = createRandomObjects(20);/*array of stage objects that might be selected*/

var mouseStart:Point;

/*drawing properties of drawingBox*/
var stroke:GraphicsStroke = new GraphicsStroke();
	stroke.thickness=1;
    stroke.fill = new GraphicsSolidFill(0x333399, .5);
var fill:GraphicsSolidFill = new GraphicsSolidFill(0x444444,.5);
var graph:Vector.<IGraphicsData>;
var path:GraphicsPath;

stage.addEventListener(MouseEvent.MOUSE_DOWN,MD,false,0,true);/*start drawing box*/
stage.addEventListener(MouseEvent.MOUSE_UP,MU,false,0,true);/*select objects*/

function createRandomObjects(n:int):Array
{
	var a:Array=new Array();
	for(var i:int=0;i<n;i++)
	{
		var s:Sprite = new Sprite;
		s.graphics.beginFill(Math.random()*0xFFFFFF);
		s.graphics.drawRect(0,0,20,20);
		s.x=Math.random()*stage.stageWidth;
		s.y=Math.random()*stage.stageHeight;
		s.alpha=.5;
		s.name = String(i);
		a.push(s);
		addChild(s);
	}
	return(a);
}

function MD(e:MouseEvent):void
/*MOUSE DOWN - start drawing square*/
{
	// removes previous one
	if(drawingBox!=null)drawingBox.parent.removeChild(drawingBox);
	drawingBox=new Sprite;
	addChild(drawingBox);
	mouseStart=new Point(mouseX,mouseY);
	addEventListener(Event.ENTER_FRAME, EF,false,0,true);

	for(var i:int=0; i<stageObjects.length; i++)
	{
		stageObjects[i].alpha=.5;
		stageObjects[i].rotation=0;
	}
}

function MU(e:MouseEvent):void
/*MOUSE UP - remove square and hit test everything under it*/
{
	removeEventListener(Event.ENTER_FRAME, EF,false);
trace('Selected objects are:');
	/*run a hit test for drawingBox against stageObjects*/
	for(var i:int=0; i<stageObjects.length; i++)
	{
		if(drawingBox.hitTestObject(stageObjects[i]))
		{
			stageObjects[i].alpha=1;
			stageObjects[i].rotation=45;
trace('Hit ' + stageObjects[i].name);
		}
	}
	drawingBox.graphics.clear();
}

function EF(e:Event):void
/*ENTER FRAME - redraws the selction box based on mouse pointer location every frame*/
{
	drawingBox.graphics.clear();
	path = RR(mouseStart.x,mouseStart.y,mouseX,mouseY);
	graph = new Vector.<IGraphicsData>();
	stroke.fill = new GraphicsSolidFill(0xFF0000,1);
	graph.push(stroke, fill, path);
	drawingBox.graphics.drawGraphicsData(graph);
}

function RR(sx:Number, sy:Number, ex:Number, ey:Number):GraphicsPath
/*RETURN RECTANGLE - returns a Graphics path for a rectangle*/
{
	var p:GraphicsPath=new GraphicsPath;
	p.moveTo(sx,sy);
	p.lineTo(ex,sy);
	p.lineTo(ex,ey);
	p.lineTo(sx,ey);
	p.lineTo(sx,sy);
	return(p);
}

Duplicate loader content in ActionScript 3

Sometimes you want to display the loaded image file twice. Here’s how to do it.

var loader:Loader = new Loader();
 
var sprite1:Sprite = new Sprite();
sprite1.addChild(getLoadedBitmap(loader.content));
 
var sprite 2:Sprite = new Sprite();
sprite2.addChild(getLoadedBitmap(loader.content));
 
function getLoadedBitmap(l:Loader):Bitmap {
  return new Bitmap(Bitmap(l.content).bitmapData);
}

Smooth movement of a TextField with Greensock tweens

Here is some code on how to get a TextField (or any other display object) to marquee/scroll across the stage smoothly. When it has scrolled out completely of the viewfield, it restarts from the right side of the stage.

If the animation is rough, try increasing the document’s FPS to get a smoother movement.

This function calls itself when it’s complete and passes itself as a variable back to the function, so it repeats continuously.

var movementSpeed:Number = 1.5;
var speed:int = 100;
var stageWidth:int = 1920;
 
private function moveMarquee(obj:DisplayObject):void {
	TweenMax.to(obj, movementSpeed, {x:obj.x - speed, ease: Linear.easeNone, onComplete:moveMarquee, onCompleteParams:[obj]});
	if(obj.x &lt; -obj.width) {
		TweenMax.killTweensOf(obj);
		obj.x = stageWidth;
		TweenMax.to(obj, movementSpeed, {x:obj.x - speed, ease: Linear.easeNone, onComplete:moveMarquee, onCompleteParams:[obj]});
	}
}

To initialize the first start, use this:

moveMarquee(marqueeText);

And to kill it, use this:

TweenMax.killTweensOf(marqueeText);

Saving a webcam image to a local shared object

Sometimes it seems impossible to find what you are looking for on Google. Here is some code on how to save a webcam captured image to a local shared object. It is very simple and not optimized with compressions and encodings, but I just wanted to to do a quick test to see if it works.

Enjoy!

package  {
 
	import flash.display.MovieClip;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.display.Sprite;
	import flash.net.SharedObject;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.geom.Rectangle;
 
	public class CameraTest extends MovieClip {
 
		private var camera:Camera;
		private var video:Video;
		private var bitmapData:BitmapData;
		private var bitmap:Bitmap;
		private var so:SharedObject;
		private var ba:ByteArray;
 
		public function CameraTest() {
			camera = Camera.getCamera(); // get webcam
			video = new Video(); // setup video
			video.attachCamera(camera); // attach camera to video
			addChild(video); // add video to stage
 
                        // setup bitmap
			bitmapData = new BitmapData(video.width, video.height);
			bitmap = new Bitmap(bitmapData);
			bitmap.x = 360;
 
                        // textfield for capture image button
			var capture_btn:TextField = new TextField();
			capture_btn.text = "CAPTURE";
 
                        // background for capture image button
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0xFF0000);
			spr.graphics.drawRoundRect(0,0,100,100,10,10);
			spr.graphics.endFill();
			spr.addChild(capture_btn);
			spr.mouseChildren = false;
			addChild(spr);
			spr.addEventListener(MouseEvent.CLICK, capture);
 
                        // the local shared object
			so = SharedObject.getLocal("pictureTest");
 
                        // retrieve button, yellow, without text
			var ret:Sprite = new Sprite();
			ret.graphics.beginFill(0xFFFF00);
			ret.graphics.drawRoundRect(100,0,100,50,10,10);
			ret.graphics.endFill();
			ret.addEventListener(MouseEvent.CLICK, retrieve);
			addChild(ret);
		}
 
		private function capture(e:MouseEvent):void {
			// remove the previous image from stage
			if(bitmap.stage) {
				removeChild(bitmap);
			}
			trace("Captured");
			// draw new image
			bitmapData.draw(video);
			// add new image data to shared object
			so.data.image = bitmapData.getPixels(bitmapData.rect);
			so.flush();
		}
 
		private function retrieve(e:MouseEvent):void {
			bitmapData.setPixels(new Rectangle(bitmap.x, bitmap.y, bitmap.width, bitmap.height), so.data.image);
			addChild(bitmap);
		}
 
	}
 
}