Posts Tagged ‘Tutorials’

Configuring Notepad++ for pbelevel format

March 23rd, 2010

I use Notepad++ for viewing my *.pbelevel files, and always had to do the extra clicking “Language >> XML” to configure it, to display the file with XML highlighting. I now found out, how one can configure Notepad++ to do this automatically: locate the langs.xml in your Notepad++ install directory and add the following line:

<Language name="xml"  ext="pbelevel"></Language>

And tada – you’re off to go!

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!

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: clickable component

January 2nd, 2010

Prerequisites:

Today I will show you, how to create a component for PushButton Engine, that registers, if an entity got clicked on or not.

In general, you got two possibilities how to handle this:

  1. per object level
  2. on a global level

Per object level: since it’s flash, you can simply add an event listener to your graphics component, which listens to click events.

I personally don’t like that too much though, so let’s consider variant two: global level.

It’s a component which exists once in the scene, and not per entity. It listens to click events which happen on the main stage, and on each click, it uses the built in functions PBE.scene.getRenderersUnderPoint and/or PBE.spatialManager.getObjectsUnderPoint to determine, which objects got clicked on.

This version only uses the getRenderersUnderPoint, the main stuff is going on in the OnClick method:

package
{
   import com.pblabs.engine.PBE;
   import com.pblabs.engine.debug.Logger;
   import com.pblabs.engine.entity.EntityComponent;
   import com.pblabs.rendering2D.DisplayObjectRenderer;
 
   import flash.events.MouseEvent;
   import flash.geom.Point;
 
	public class ClickComponent extends EntityComponent
	{
		protected override function onAdd():void
		{
			PBE.mainStage.addEventListener(MouseEvent.CLICK, OnClick );
		}
 
		private function OnClick(e:MouseEvent):void
		{
			var results:Array = new Array();
			var point:Point = new Point( PBE.mainStage.mouseX, PBE.mainStage.mouseY );
			var foundRenderers:Boolean = PBE.scene.getRenderersUnderPoint( point, results );
 
			for each( var obj:DisplayObjectRenderer in  results )
			{
				Logger.print( this, obj.name );
			}
		}	  
 
		protected override function onRemove():void
		{
			PBE.mainStage.removeEventListener(MouseEvent.CLICK, OnClick );
		}
	}
}

Now if you want to get the spatial objects, simply use this:

var spatials:Array = new Array();
PBE.spatialManager.getObjectsUnderPoint( PBE.scene.transformScreenToWorld(point), spatials, new ObjectType("Renderable"));
 
for each( var spatialObj:ISpatialObject2D in spatials )
{
	Logger.print( this, spatialObj.position.toString() );
}

The main difference is that getRenderersUnderPoint results in DisplayObjectRenderers, and getObjectsUnderPoint results in ISpatialObject2Ds.

Also, see that you can supply an object mask. Leaving it null will take all objects into consideration.

All in all you can see, how easy this is (for such a complex sounding task)!