Posts Tagged ‘cs4’

Why Do Package Names Start With com. In Actionscript 3?

Posted in Actionscript 3.0 on February 16th, 2010 by helloworlder – 1 Comment

Question:

Why do package names start with com. in Actionscript 3?

By the way, I’m guessing you already know that package names map to folder names. E.g. an as3 source file in the package com.example must be under the folder example, which in turn must be under the folder com..

Answer:

Package names do not have to begin with com. However, it is the convention to do so. E.g. com.thebestcompany.thebestproduct

What is the significance of com? Well, the package name is simply a domain name reversed. E.g com.helloworlder.utils

The domain name technically does not have to exist but you should own it. Obviously never use someone else’s domain name!! That’s just rude. It is standard practice to use a domain name because it helps to guarantee that your package names don’t collide with the names of someone else’s package on the other side of the world.

If I release some AS3 code that creates popup windows, I may place the code in a package called com.helloworlder.utils.popupwindow and not simply popupwindow

Likewise, if I download some third party code to use in my own project I’d expect their code to be packaged properly. E.g. com.goodbyeworld.utils.modalwindow. Then, I can just place their goodbyeworld folder under by own com folder.

Just as importantly, putting your as3 files into packages helps you think about how to organize the files. Often people place all files (their own and 3rd party code) under one folder! The advice is - don’t.


Unit Testing AS3 Code on the Flash CS4 Platform

Posted in Actionscript 3.0 on February 15th, 2010 by helloworlder – Be the first to comment

I’m developing a project on the Flash CS4 platform and finally started unit testing my AS3 code.  Not doing unit tests seems lazy and negligent. The only reliable unit testing framework I’ve found so far is asunit, which is open source. The Flash CS4 platform has been traditionally targeted at designers and not programmers so unit testing was somewhat neglected in the past.

Installation is very simple. Just unzip the archive and place the as3 folder anywhere on your system. Then in Flash CS4 go to Edit->Preferences and click on the button “Actionscript 3.0 Settings” at the bottom of the popup.  You need to add both of the following paths under “Source path”:

as3/src/

as3/test/

IMPORTANT: Don’t just add as3/ otherwise the framework files won’t be found.

That’s all for the installation. For usage, the asunit website has enough information to get you started.

Code Your Own Lasso Tool in Actionscript 3.0

Posted in Misc on August 23rd, 2009 by helloworlder – 5 Comments

This tutorial assumes you are using Flash CS3 or Flash CS4. You should have basic programming experience.

We are going to create a lasso tool, like the one you see everyday on your desktop by dragging the cursor and selecting icons. You can apply the following technique to games as well. Specifically I have real time strategies games in mind :) It’s easy to code and it’s only about 50-60 lines in total. A screenshot of the end product:

lasso_final

Part 1 - Setup

1. Go to File->New and Create a new Flash File (Actionscript 3.0).

2. Save the file, call it anything you want.

3. Go to File->New again and this time create a new ActionScript File.  Save this file as “Lasso” without the quotes.

4. Go back to the Flash File and change the Class in the Properties panel to “Lasso”. This refers to the Actionscript file that you create and saved in step 3. Should a dialogue box pop up just click OK:

properties_panel

5. Go back to the “Lasso” Actionscript file. This is where we’re going to do everything. We will not be doing anything on the timeline.

Part 2 - Coding

Now we can begin programming. I’ll go through the code chunk by chunk. At the end of the tutorial is the full code.

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

We start off by importing the stuff we need. Specifically we are importing classes. These classes are part of the Flash API.

public class Lasso extends Sprite
{
	private var dragging:Boolean = false;
	private var start_drag_location:Point = new Point();

Now we’ve started to code our Lasso class, which extends the Sprite class. Then we make 2 class variables. The first, dragging is a flag telling us whether the user has the mouse button down and is dragging the mouse. The second, start_drag_location is a Point indicating where the user first began dragging the mouse. The Point class represents a coordinate with properties x and y. We’ll need these variables later.

public function Lasso()
{
     stage.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
     stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
     stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
}

This part is the class constructor. All it does here is add 3 event listeners to the stage. Now the stage will listen to these events. The Event.ENTER_FRAME event is fired at every single frame. In other words it occurs continuously and as quickly as the framerate you set in the Properties panel. This event is handeled by a function call handleEnterFrame , which we will look at next.

private function handleEnterFrame(event:Event):void
{
	if(dragging)
	{
		graphics.clear();
		graphics.lineStyle(1, 0x00ff00);

                var theWidth:Number = mouseX - start_drag_location.x; //current width so far
		var theHeight:Number = mouseY - start_drag_location.y; //current height so far

		var top:Point = new Point(start_drag_location.x, start_drag_location.y);
		var bottom:Point = new Point(start_drag_location.x, start_drag_location.y + theHeight);
		var left:Point = new Point(start_drag_location.x, start_drag_location.y);
		var right:Point = new Point(mouseX, start_drag_location.y);

		graphics.moveTo(top.x, top.y);
		graphics.lineTo(top.x + theWidth, top.y);

		graphics.moveTo(bottom.x, bottom.y);
		graphics.lineTo(bottom.x + theWidth, bottom.y);

		graphics.moveTo(left.x, left.y);
		graphics.lineTo(left.x, left.y + theHeight);

		graphics.moveTo(right.x, right.y);
		graphics.lineTo(right.x, right.y + theHeight);

	}
}

OK, let’s break it up.

var theWidth:Number = mouseX - start_drag_location.x; //current width so far
var theHeight:Number = mouseY - start_drag_location.y; //current height so far

These 2 lines determine the width and height of the lasso box constantly as long as the user is dragging the mouse.

var top:Point = new Point(start_drag_location.x, start_drag_location.y);
var bottom:Point = new Point(start_drag_location.x, start_drag_location.y + theHeight);
var left:Point = new Point(start_drag_location.x, start_drag_location.y);
var right:Point = new Point(mouseX, start_drag_location.y);

These lines of code determine the positions of the 4 lines that make up the box. We are going to draw each line separately and this will form into a box. Of course we cannot just say that a line has a position at a point x, y. Where on the line is the anchor? So for the horizontal lines, the anchor is at leftmost tip. For the vertical lines the position is at the topmost point. Also we have been assuming that the user forms the lasso by dragging from the top left to the bottom right. If the user draws the lasso in any other way then top may refer to bottom and left may refer to right etc. However, no changes need to be made to the code to handle the other situations, it’ll work in any case.

graphics.moveTo(top.x, top.y);
graphics.lineTo(top.x + theWidth, top.y);

graphics.moveTo(bottom.x, bottom.y);
graphics.lineTo(bottom.x + theWidth, bottom.y);

graphics.moveTo(left.x, left.y);
graphics.lineTo(left.x, left.y + theHeight);

graphics.moveTo(right.x, right.y);
graphics.lineTo(right.x, right.y + theHeight);

These lines of code are the ones that actually draw the box. Imagine a pen that moves to each position and draws a line from that position to the specified end point. We simply get the end point by adding the height or width of the line. Soon, a nice box is formed!

private function handleMouseDown(event:MouseEvent):void
{
	dragging = true;
	start_drag_location = new Point(mouseX, mouseY);
}

private function handleMouseUp(event:MouseEvent):void
{
	dragging = false;
	graphics.clear();
}

These 2 functions handle the mouse down and mouse up events. When the mouse is pressed we set the dragging flag to true, and record the location where the user began dragging in the start_drag_location class variable. When the mouse is released we set the set the flag to false and remove the box by calling clear() on the graphics object.

That is all. Now go back to the flash file and press Ctrl+Enter to run it.

You will now be able to form a lasso with the mouse. This tutorial didn’t cover actually selecting things with a lasso. To do that you would just need an array that stores the selected objects. When you select an area, then check which objects are within the bounds of that area and add them to the array by using the push() function on the array.

The Code in Full

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class Lasso extends Sprite
	{
		private var dragging:Boolean = false;
		private var start_drag_location:Point = new Point();

		public function Lasso()
		{
			stage.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
		}

		private function handleEnterFrame(event:Event):void
		{
			if(dragging)
			{
				graphics.clear();
				graphics.lineStyle(1, 0x00ff00);

				var theWidth:Number = mouseX - start_drag_location.x; //current width so far
				var theHeight:Number = mouseY - start_drag_location.y; //current height so far

				var top:Point = new Point(start_drag_location.x, start_drag_location.y);
				var bottom:Point = new Point(start_drag_location.x, start_drag_location.y + theHeight);
				var left:Point = new Point(start_drag_location.x, start_drag_location.y);
				var right:Point = new Point(mouseX, start_drag_location.y);

				graphics.moveTo(top.x, top.y);
				graphics.lineTo(top.x + theWidth, top.y);

				graphics.moveTo(bottom.x, bottom.y);
				graphics.lineTo(bottom.x + theWidth, bottom.y);

				graphics.moveTo(left.x, left.y);
				graphics.lineTo(left.x, left.y + theHeight);

				graphics.moveTo(right.x, right.y);
				graphics.lineTo(right.x, right.y + theHeight);
			}
		}

		private function handleMouseDown(event:MouseEvent):void
		{
			dragging = true;
			start_drag_location = new Point(mouseX, mouseY);
		}

		private function handleMouseUp(event:MouseEvent):void
		{
			dragging = false;
			graphics.clear();
		}
	}
}

Easy AdSense by Unreal