Posts Tagged ‘ActionScript’

How to Use SWFSpriteSheetComponent (PBE)

March 12th, 2010

JD Conley from Hive7 has posted a tutorial how to use his SWFSpriteSheetComponent for the PushButton Engine. Check it out at his blog, it’s definately worth a look!

In short, using this component, you can combine, what normally is hard to achieve with Flash: animation with “thousands of buildings on the screen at once“.

Simulate Double-Click with Actionscript

February 2nd, 2010

Using the Flash built in solution to detect double clicks with MouseEvent.DOUBLE_CLICK can turn out to be a pain, especially if you’re developing more than a simple website (no offence to those who do! ;) ). For my current project (Level Master 2000), I urgently needed such behaviour, and I didn’t want to turn on sprite.doubleClickEnabled = true for all selectable objects. So I simply implemented my own version, for which I can even adjust the click interval, such that a double click gets recognized.
It’s as simple as:

private const DOUBLE_CLICK_INTERVAL:int = 400;
private var clickIntervalTimer:int;

Somewhere, add your event listener to a normal mouse down event:

%something%.addEventListener(MouseEvent.MOUSE_DOWN, OnClick);

And then, in the OnClick, check if it was a double click:

if((getTimer() - clickIntervalTimer) < DOUBLE_CLICK_INTERVAL) 
{
    //A DOUBLE CLICK HAPPENED, DO SOMETHING!
}
 
//Don't forget to update your clickIntervalTimer afterwards:
clickIntervalTimer = getTimer();

Note: the getTimer function can be found in import flash.utils.getTimer;. Everything else should be straight forward!

Get class and super class

January 27th, 2010

For Level Master 2000, I needed two functions:

  1. getClass, which I stuff in any kind of object, and it returns me its class.
  2. getSuperClass, which I stuff in it’s class name as string, and it returns me the class name of its parent as string.

Note that first returns the class itself, and the second one works with strings. If you need to e.g. let the second one work with classes also, just adapt it like in the first function.

All in all, you need to import these functions:

import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;

And here are the two functions:

public static function getClass(obj:Object):Class
{
	return Class(getDefinitionByName(getQualifiedClassName(obj)));
}
static public function getSuperClass(className:String):String
{
	return getQualifiedSuperclassName(getDefinitionByName(className));
}

Note that getSuperClass only returns its direct parent. If you need to go further up the inheritance tree, you need to call it recursively. Highest it can get is “Object”.

PBE Series: using SchemaGenerator

January 22nd, 2010

Previously, in PBE Series: dynamically get list of public members, properties and methods of object or class, I noted that

SchemaGenerator.instance.generateSchema();

“dumps all classes and public info to a xml file”.
We’ll, I’ve used it now, and I must admit that was a lie ;)
So here’s how you use it, and what it results in!

First, you need to establish a local connection, and then call generateSchema:

conn = new LocalConnection();
conn.client = this;
conn.allowDomain('*');
try {
	conn.connect("_SchemaConnection");
} catch (error:ArgumentError) {
	trace("Can't connect to _SchemaConnection");
}
 
SchemaGenerator.instance.generateSchema();

SchemaGenerator.instance.generateSchema will then call OnSchemaReceived in your class, so you need to add that. It takes two string arguments: type and data. Type is either “START”, “END”, “ERROR” or “TYPE”. Data is an XML description of the current processed class (internally it uses flash.utils.describeType), which you then can further interact with.
Here’s an example of how OnSchemaReceived could look like:

public function OnSchemaReceived(type:String, data:String):void
{
	switch( type )
	{
		case "START":
		break;
 
		case "END":
		break;
 
		case "ERROR":
		break;
 
		case "TYPE":
		{
			var myXML:XML = new XML(data);
			myXML.ignoreWhite = true;
 
			var xmlList:XMLList = myXML.child("factory");
 
			//Check accessors:
			for each (var acc:XML in xmlList.accessor)
			{
				//DO SOMETHING.
			}
 
			//Check public variables:
			for each (var pVar:XML in xmlList.variable)
			{
				//DO SOMETHING.
			}
		}
		break;
	}
}

That should help you getting started!

PBE Series: creating spatial objects via ActionScript

December 19th, 2009

Prerequisites:

Today I want to cover how to create spatial positioned objects with PushButton Engine via ActionScript.
With the current build, you got two possibilities:

  1. create a SimpleSpatialComponent or
  2. create a Box2DSpatialComponent.

Remark: if you want to display graphics only (meaning without a spatial component), it’s also possible to position them directly. See PBE Series: quickly displaying sprites via ActionScript for more information about that!

So what’s the difference between those two? In short, Simple-SpatialComponent is not driven by a physics engine. Box2D-SpatialComponent is, as the name might let one guess, bound to Box2D. If your SpatialManager is a Box2D-ManagerComponent (again, see PBE Series: initializing your scene via ActionScript how to setup your scene), you can easily work with both types of objects in your scene, but remember that a Simple-SpatialComponent cannot collide with a Box2D-SpatialComponent. So for everything which needs physical interaction, you want to use Box2D-SpatialComponent.

Anyway, not every game needs physics, so let’s start with the SimpleSpatialComponent:

//Create a new entity:
var myEntity:IEntity = PBE.allocateEntity();
myEntity.initialize("MyEntity");
 
//Create our spatial component
var spatialComp:SimpleSpatialComponent = new SimpleSpatialComponent();
spatialComp.spatialManager = PBE.getSpatialManager();
spatialComp.objectMask = new ObjectType("Renderable");
spatialComp.position = new Point(0, 0);
spatialComp.size = new Point(256,64);         
 
//Add the component to the entity:
myEntity.addComponent( spatialComp, "Spatial" ); 			
 
//Since we want to visualize the entity, we need a simple sprite renderer:
var renderComp:SpriteRenderer = new SpriteRenderer(); 
 
renderComp.scene = PBE.getScene();
renderComp.fileName = "../assets/Images/platform.png";
 
//Bind render component position and rotation to spatial component:
renderComp.positionProperty  = new PropertyReference("@Spatial.position");
renderComp.rotationProperty = new PropertyReference("@Spatial.rotation");
 
//Add the component to the entity:
myEntity.addComponent( renderComp, "Render" );

The most important thing to note, is that the render component’s position and rotation properties are bound to the spatial component.

Now to the Box2DSpatialComponent:

//Create a new entity:
var myEntity:IEntity = PBE.allocateEntity();
myEntity.initialize("MyEntity");
 
//Create our spatial component / body:
var spatialComp:Box2DSpatialComponent = new Box2DSpatialComponent();
spatialComp.manager = (PBE.getSpatialManager() as Box2DManagerComponent);
spatialComp.position = new Point(0, 0);
spatialComp.size = new Point(50, 50);
spatialComp.collisionType = new ObjectType("Something");
spatialComp.collidesWithTypes = new ObjectType("Something", "SomethingDifferent");
 
//We also need a collision shape, which collides with the world (meaning other shapes):
var shape:CircleCollisionShape = new CircleCollisionShape();
shape.radius = 1.0;
shape.density = 20;
 
//Now we add the shape to the spatial component / body:
spatialComp.collisionShapes = new Array();
spatialComp.collisionShapes.push(shape);
spatialComp.buildCollisionShapes(); //don't forget to build the collision shapes!
 
//Add the component to the entity:
myEntity.addComponent( spatialComp, "Spatial" ); 			
 
//Since we want to visualize the entity, we need a simple sprite renderer:
var renderComp:SpriteRenderer = new SpriteRenderer(); 
 
renderComp.scene = PBE.getScene();
renderComp.fileName = "../assets/Images/platform.png";
renderComp.positionProperty  = new PropertyReference("@Spatial.position");
renderComp.rotationProperty = new PropertyReference("@Spatial.rotation");
 
//Add the component to the entity:
myEntity.addComponent( renderComp, "Render" );

I think the code is pretty self-explanatory. The basic work flow is to first create a body (spatial component), then some collision shapes, add those shapes to your body and then build the collision shapes. This is just a start, feel free to play around with all the properties and see how they affect the body’s behaviour!