Tuesday, July 7, 2009

Transparent SWF

I was creating a .swf file player for my website. The player is used to prevent the annoying "autoplay" that tend to be difficult to stop. Some flash games are setup so there is no easy way to prevent this, hence your page opens up and plays the game soundtrack right away. Sorry I need to control that, even setting javascript control for the flash to param play="false" does not always work.

Anyway, the .swf file I created that plays .swf files when you click on the play button. The problem I had was that I set the player to
param wmode="transparent"
this caused games controlled by the arrow keys to scroll the browser at the same time. That makes it impossible to play the game.

Moral = If you load a swf into another swf that is transparent you may have issues with browser scrolling

points and pixels

points = pixels * 72 / 96

75 = 100 * 72 / 96

pixels = points * 96 / 72

100 = 75 * 96 / 72

Vista and air

When you try to direct download an .air file via adobe air and ie7 or 8 on your vista os it will think that the air file is a zip file. The solution is simple, change the extension to .air

You can also do it the right way and set up an air badge to properly install the air program.

Object stuff

Just adding some of my daily epiphanies.

The ability of As3 to just assign description strings "I call them", to objects is as handy as a four armed butler. If you need some kind of switch in your program you don't necessarily need to declare a public variable to use as a switch. It is very simple to just add a string to an object. My object is Player. just add dot syntax and:

player.state = "off";

For the newbies you can add anything you want to your object, real time, like this:

player.happynessiocity = "groovy";

Makes for an easy conditional to check a condition, like this:

if(player.state == "off")
{
}


This is a bit of a hack and it throws a swf error but runs. Maybe will work in a pinch but here is a clever solution for a switch in a case where event flow won't allow you to access a listener somewhere. Just change the name of the button.

player.name = "off";

You are effectively negating listeners of parent display object containers elsewhere until you return the name to normal:

player.name = "playbutton";

Concerning functions, it is very handy to be able to send a function an object. You can then mass produce object modifications, like alpha, scale, with dot syntax. Just pass the function the object and then change it:
changeTheSize(myObject);

function changeTheSize(obj:myObject):void
{
obj.scaleX = 1.5;
obj.scaleY = 1.5;
}


You can create factories this way and just send in anything you want to make bigger, or whatever.

Math.round decimal places

Here is some rounding help. This converts miliseconds to seconds with two decimal places. Note the "/10) * 10" part, that rounds it to 2 decimal places, "/100) * 100" would round it to one decimal place.

_secondsRound = Math.round(_theSound.length / 10) * 10 / 1000;

Set Focus

Just sharing some info about focus in As3.

Opon experimenting with text input boxes I discovered that setFocus(), when uses on a hidden text input box was a great way to hide a cursor after ENTER event.

drawFocus() also comes in handy to bring a field back into perspective.

Top of display list

Example of setChildIndex to bring your display object to the top.

import flash.display.Sprite;
import flash.events.MouseEvent;

var container:Sprite = new Sprite();
addChild(container);

var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFF0000);
circle1.graphics.drawCircle(40, 40, 40);
circle1.addEventListener(MouseEvent.CLICK, clicked);

var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00FF00);
circle2.graphics.drawCircle(100, 40, 40);
circle2.addEventListener(MouseEvent.CLICK, clicked);

var circle3:Sprite = new Sprite();
circle3.graphics.beginFill(0x0000FF);
circle3.graphics.drawCircle(70, 80, 40);
circle3.addEventListener(MouseEvent.CLICK, clicked);

container.addChild(circle1);
container.addChild(circle2);
container.addChild(circle3);
addChild(container);

function clicked(event:MouseEvent):void {
var circle:Sprite = Sprite(event.target);
var topPosition:uint = container.numChildren - 1;
container.setChildIndex(circle, topPosition);
}