Keyboard Issues – Left Arrow + Up Arrow + SPACE

In the few games that I tried to create ( yes, many left in development because of time and mood ) I rarely used key combinations. The ones I am talking about right now refers to arrows and space keys. Some combination of three of these give a lot of troubles and to a lot of people seems they are not working.

I found out that it’s not a Flash issue but more a hardware issue on some keyboards and the solution to that is not using arrow keys as your controls.  WASD seems to be the best choice.

Advertisements

[ AS3] Masking Dynamic Content TextField and cacheAsBitmap

Ok, this is a quick post.

In a little project I am working right now I had to do some effects involving some textfield masking.  The problem I had is that the text was never masked and it always was visible on the display list. To solve this problem I had to use the cacheAsBitmap propriety of display objects. Once I set that to true everything went smooth.

Waving Text Effect in Actionscript 3.0

var text:String       = "This text is a waving text";
var format:TextFormat = new TextFormat("Arial", 12);
var radius:int = 5;
var speed:Number = Math.PI/15;
var angle:Number =0
var letters:Array = null;


function Init():void
{
	letters = [];
	
	for (var a = 0, x = 0; a<text.length; a++)
	{
		letters[a] = new Sprite();
		
		var txt:TextField =  new TextField();
		txt.selectable = false;
		txt.autoSize = "left";
		txt.text = text.charAt(a);
		txt.setTextFormat(format);
		
		letters[a].addChild(txt);
		letters[a].x = a * 15;
		addChild(letters[a])
	}
	
	addEventListener(Event.ENTERFRAME, Animate, false, 0, true); 

}

function Animate(e:Event):void
{
	for (var a = 0; a<letters.length; a++) 
	{
		letters[a].y = Math.sin(angle+a)*radius;
	}
	
	angle += speed;
}

Init();

Random Generated Displacement Map

So,  I was following this tutorial from activetuts+ to create the same effect,  a static distortion effect using the displacement map filter,  in one of my game projects and it was cool until I realized that my project is getting fatter and fatter by adding images of different sizes to use for different sized objects.

Ofcourse, this was not acceptable so I thought I could create a random generated image by code and reuse it than to create different images in Photoshop.

Anyway, here’s the code which is generating a random image to be used with the effect in the tutorial.

function rndImage(w:Number, h:Number, wl:int = 0, bl:int =0):BitmapData
{
var bitmapdata:BitmapData = new BitmapData(w,h,false, 0xDDDDDD);
var randY:int = 0;
var randX:int = 0;
var randW:int = 0;
var thick:Number = 0;
var a:int = 0;
var minWidth:int = 0;
var line:Shape = new Shape();

for ( a = 0; a< wl; a++)
{
thick = Math.random()* 1.3 + 0.2;
randY = Math.random() * h;
minWidth = (40 * w)/100;
randW = Math.random() * (w-minWidth) + minWidth;
randX = Math.random() * ((w-randW));
line.graphics.lineStyle(thick, 0xFFFFFF);
line.graphics.moveTo(randX,randY);
line.graphics.lineTo(randW, randY);
}

for ( a = 0; a< bl; a++)
{
thick = Math.random()* 1.3 + 0.2;
randY = Math.random() * h;
minWidth = (40 * w)/100;
randW = Math.random() * (w-minWidth) + minWidth;
randX = Math.random() * ((w-randW));
line.graphics.lineStyle(thick, 0x000000);
line.graphics.moveTo(randX,randY);
line.graphics.lineTo(randW+randX, randY);
}

bitmapdata.draw(line);

return bitmapdata;
}

And this is how you generate the image

rndImage(width,height, nr_white_lines,nr_dark_lines);

Detect Mouse Inactivity in Actionscript 3.0

Before we start you need to be familiar with MouseEvent and Timer classes.

var _timer:Timer = new Timer(5000);

function Start():void
{
   _timer.addEventListener(TimerEvent.TIMER, ShowMsg, false, 0, true);
   _timer.start();

   stage.addEventListener(MouseEvent.MOUSE_MOVE, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_UP, StopTimer, false, 0, true);
}

function ShowMsg(e:TimerEvent):void
{
   trace("There is no activity");
}

function StopTimer(e:MouseEvent):void
{
   _timer.stop();
   _timer.start();
}

Start();

The first thing I’ve done is to setup a Timer object to count up to 5 seconds ( you can change that to what ever numer of seconds you want ).

The timer object also has an event listener so we can know when the 5 seconds are over.

I have setup some event listeners that will help detecting any actions of the mouse on the Stage ( MOUSE_DOWN, MOUSE_MOVE and MOUSE_UP )

If during those 5 seconds nothing happens ( mouse is not moved or performs a click ) ShowMsg() function will be fired up and give you a message of inactivity.

If the mouse is moved the StopTimer() function will be activated and reset timers count.

This is a useful and easy way to detect user activity.

Tween A Number with GreenSock’s Tweening Class

I am going to give you a little example on how to tween a number. This example can be used with other tweening classes as well like Caurina’s Tweener.

Pay attention that the variable we want to tween needs to be public otherwise you’ll get an error compiling the code.

We are going define a variable as an Object. This object will hold a value. The tweener will tween this object’s value.

public var my_number :Object  =  { value: 0 };
TweenLite.to( this.my_number,  .5, { value: 100 , onUpdate:function(){ trace (this.value); } } )

 

Collision detection alternatives to hitTest and hitTestObject

Since ages ago Flash has never had a perfect solution to detect collisions.

The old  HitTest  method from Actionscript 2.0 has been replaced in Actionscript 3.0 by two separated methods hitTestObject and hitTestPoint, but they are still not perfect.

Luckly there are some good alternatives ( depending on your project ). Here’s a list.

Mike Chambers’s BitmapData.hitTest for Collision Detection

Troy Gilbert’s Pixel-Perfect Collision

Corey O’Neil’s  Collision Detection Kit