Actionscript 3 TextField renders excessive line breaks

Hi all,

I was working on a project for musicnation.com, their EPK. I had to render text in a textfield that was coming from an XML feed, text that is entered by users. It can be filled with different types of line feeds, notable the:

\n = linux
\r = mac (prior to macos being based on freebsd I guess)
\r\n = the damned pc linefeed.

The TextField object in Flash seems to render both \r and \n as linefeeds. Therefore I was getting way too many linefeeds in my rendered text.
Continue reading

Passing parameters to a Flash 9 ActionScript 3 SWF file

Hi everyone, I found this blog entry which shed light on how to pass variables to your SWF this way :

[code]
light.swf?artist=yolanda&page=12
[/code]

Here is some usefull function you can use. Put it in your Document Class. For those who don’t know what that is, just put it on the stage on Frame 1 then.

1
2
3
4
5
6
7
8
9
10
protected function getSwfParam(name:String, defaultValue:String):String
{
	var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters;
 
	if(paramObj[name] != null && paramObj[name] != "")
		return paramObj[name];
 
	else
		return defaultValue;
}

So it’s quite simple to use it:

1
var artist:String = getSwfParam("artist", "defaultArtist");

Voilà!

Inspiration

I used these articles as inspiration. Make sure you READ the FIRST COMMENT of the post since it outlines an important omission from the author.

link to Theo’s Blog entry

Cheers!

bug in flash player 9 only on Mac OS X and Linux

Hi..

i’ve run into a bug on Mac OS X and Linux. Don’t ask me WHAT is the source, I don’t understand it. Basically I have a scrollable list (all my own components) of tracks & videos in a box, and it works fine on Windows XP but doesn’t work at all under Linux or Mac OS X. The box contains a status bar which appears on all platforms but the scrollbar & content don’t appear on Mac OS X and Linux.

Anybody has clues? I updated my version of flash player to 9.0.48.0 and still the bug doesn’t happen on Windows. I had version 9.0.32.0 on Windows before, while my other clients have version 9.0.45.0 on Mac OS X and 9.0.32.0 on Linux (I think).

How am I supposed to diagnose the cause of such a problem?

HELP!

Cashback the movie

Hi everyone,

last night I watched the movie Cashback from Sean Ellis. It is a beautiful movie about beauty, sensitivity, the women body. It’s a movie that I would suggest to all sensitive men out ther who are in a respectful awe of the women body in all it’s different ways of expressing itself.

It brought me back to my teenage years, to remembering my first relationships, to memories of childhood also.

Here is the cover :

cashback covert

I give it a solid 4.5 stars.

Cheers!

Martin

je retourne à l’université!

ça y est, j’ai fait mon choix de cours pour l’automne :

ENGR 233 – Applied Advanced Calculus (yark!)
ENGR 301 – Engineering Management Principles and Economics (yark!)
BIOL 208 – Environmental Biology (ah ça risque d’être intéressant!)
COMP 472 – Artificial Intelligence (ahhhh oui!)
COMP 473 – Pattern Recognition (ahhh oui!)
GERM 201 – Introduction to German (viva la germania!).

bref, un peu chargé pour le moment.. ça fait 19 crédits. Je risque de laisser tomber GERM 201 puisque ça peut pas compter dans mon bach.

Toutefois à ma première session j’ai accumulé 19 crédits (maudit malade!).

Martin

Making a DisplayObject, Sprite or Movieclip (or any other subclasses of DisplayObject) be always on top in ActionScript 3

Hi. I’m making a little rating visual control for a client and had to ensure this control would always stay on top of everything else. This basically means that whenever another control is added to the display list of it’s parent, i need to ensure my control stays last in the list.

You need to listen to the flash.events.Event.ADDED event on the container like so :

1
2
3
4
5
6
7
// don't forget to do this at the top of your file
import flash.events.Event;
 
// object code goes here.. and inside of some initialization function you add this line
// (be sure the object has been added to some parent before calling this line!)
 
parent.addEventListener(Event.ADDED, alwaysOnTop);

Now, to code the alwaysOnTop function, what we need to do is find out where our object is in the display list; if it’s not last we need to swap it to the last position like so

1
2
3
4
5
6
 
protected function alwaysOnTop(evt:Event):void
{
   if(parent.numChildren-1 > parent.getChildIndex(this))
      parent.swapChildrenAt(parent.numChildren-1, parent.getChildIndex(this));
}

there is one case, if another object swaps itself to your position, where the display list is changed. The event Event.ADDED is triggered by the functions “addChild” and “addChildAt”. There is no event (in my humble knowledge) which tells you when the display list order has changed. Therefore it would be a good idea to have a centralized timer which would ensure all controls which must stay on top do actually stay on top by calling their “alwaysOnTop” functions at a regular interval.

Comments and questions are welcome!

:)

Martin

j’ai hâte d’avoir mon piano!

à ma prochaine paye, je me fais un gros plaisir: je fais déménager mon piano chez moi.

et ce que je peux avoir hâte. ça va peut-être décourager mes colocs de m’entendre jouer tout le temps!! Mais .. comment dire, c’est si important pour moi.

Si je peux suggérer un bon film: Reign Over Me avec Adam Sandler (entre autre). Vraiment puissant et intéressant.

Reign Over Me
5 stars

Depuis quelques jours j’écoute James Blunt – Fall At Your Feet, à répétition. Je trouve que la chanson a une belle passion, celle de se jeter aux pieds d’une femme.
James Blunt - Chasing time

Ces derniers temps, j’arrive pas à m’ouvrir en amour. Ma dernière relation amoureuse est viré au vinaigre au mois de janvier, alors qu’on était en inde. Ce fût un beau voyage, très mouvementé, mais Mina aurait eu besoin d’avoir la fessé plus souvent quand elle était enfant; qu’on lui mette un peu plus de plomb dans la tête. Ou peut-être qu’elle avait juste besoin de vivre seule sans chum assez longtemps pour se grounder à nouveau. J’en sais rien. Je viens de lui écrire un long email.. après plus d’un moins sans nouvelles; peut-être elle me répondra.

Demain je rencontre Yves Poiré pour discuter de notre projet Made Authentic. J’aime pas vraiment le design, et le truc me saoule un peu. Surtout parce que c’est du HTML / Javascript, surtout parce que c’est pas fiable ces bébelles là. J’ai une aversion aigue pour le HTML / Javascript; surtout après avoir travaillé en Flash 9 (as3).

Euh à part ça, je me suis acheté plein de jouets cette semaine. Une nouvelle suspension avant pour mon Trek Fuel 98 en carbone; la suspension c’est une Fox Float RLC 130mm, usagée bien sûr. Puis un support pour mon écran qu’on visse au mur, un dérailleur XTR (usagé) pour mon autre vélo, et des écouteurs Sony Fontopia (les meilleurs selon moi) EX71 quelque chose. Tout ça sur Ebay et en pigeant dans ma marge de crédit étudiante même si je suis plus étudiant depuis 2 ans.. d’ailleur faudrait faire le ménage dans mes finances, c’est le bordel là dedans.

Je vais commencer le montage vidéo de mes épisodes en Inde cet automne je pense. Je vais les visionner bientôt, pour la première fois depuis mon retour de voyage, mais le montage il sera pour quand il fera froid dehors et que j’aurai moins envie d’y être.

ah, et c’est vraiment bizarre d’être à montréal pendant les vacances de la constructions. Tu as un sentiment agréable qu’il manque de monde partout, que la moitié de la population est partie. Tu le sens sur les rues (St-Denis, Beaubien) et encore plus dans les parcs comme le parc lafontaine. Tantôt il y a eu les feux d’artifices ici, et je dois avouer qu’il y avait quand même pas mal de monde pour les voir..

bon moi dodo.
:)

Tutorial – Creating the reflection of an image dynamically in AS3

You’ve seen it everywhere, even in the new iTunes. Basically the idea is to create a reflection of an image, as if the surface it is standing on was mirror-like.

sample reflection

I’ve managed to hack away some code using the BitmapData, Bitmap and Sprite classes. This is probably not an efficient way to do things, especially the changeAlpha function which adapts the negative color values (why do I get that?) for each pixel.

_art is a Sprite containing the image we want to create the reflection of; in my example _art has been scaled by fixing it’s width and height dynamically, therefore we need to take that into consideration when drawing it’s content into a BitmapData object

_artReflection is a Sprite which will contain the reflection

Here we go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;
 
protected function createReflection()
{
   // you'll need to rewrite this, my _art variable is a covert art from a CD loaded dynamically, and I scale it previously
   var xscale:Number = _art.scaleX;
   var yscale:Number = _art.scaleY;
 
   // create my scaling matrix to draw the bitmap into a bitmap data, since even it it's containing Sprite is scaled, it will draw the thing in full proportions otherwise
   var scaleMatrix:flash.geom.Matrix;
   scaleMatrix = new Matrix();
   scaleMatrix.scale(xscale, yscale);         
 
   // create a bitmap data for the source bitmap, of the full dimensions of the image we are using to create the reflection from
   var sourceBmp:BitmapData;
   sourceBmp = new BitmapData(_art.width, _art.height, true, 0x00ffffff);
 
   // draw the source (covert art in my case, into the bitmap data.
   sourceBmp.draw(_art, scaleMatrix);
 
   // the height of my reflection is 1/3 of the original image
   var reflectionHeight:Number = Math.round(_art.height/3);
 
   // variable to track the row we are writing in the target bitmapdata
   var targetRow:Number;
 
   // this is our starting alpha value (0 to 255). The reflection starts at 1/4th of alpha, going gradually to 0
   var curAlpha:Number = 64;
 
   // this is the step by which the alpha will lessen on each row, calculated based on the height of our reflection
   var steps:Number = 64 / reflectionHeight;
 
   // create our target bitmap data, same width as the original image, and using the height we calculated earlier
   var targetBmp:BitmapData = new BitmapData(_art.width, reflectionHeight, true, 0x00FFFFFF);
 
   // loop through the rows of pixels, starting at the bottom of the image to be reflected
   for(var row:Number = _art.height-1; row > _art.height - reflectionHeight; row--)
   {
      // our target row is opposite the sourcerow, since we are reflecting it
      targetRow = _art.height - row - 1;
 
      // for each row, decrement the alpha
      curAlpha -= steps;
 
      // we go through columns left to right in both source and target image
      for(var col:Number = 0; col < _art.width; col++)
      {
         // we set the pixel in the target image, using col, targetrow, and changing the alpha value of the pixel we get from the source image on the way
         targetBmp.setPixel32(col, targetRow, changeAlpha(sourceBmp.getPixel32(col, row), Math.round(curAlpha)));
      }
   }
 
   // create a bitmap from the bitmapdata
   var image:Bitmap = new Bitmap(targetBmp);
 
   // add it to the sprite we use to contain the reflection
   _artReflection.addChild(image);
 
   // and eventually position _art and _artReflection so that _art is right OVER the _artReflection
}
 
// this function alters the alpha value of a 0xAARRGGBB color value and returns the new color value
protected function changeAlpha(color:uint, alpha:int):uint
{
   // decompose the 0xAARRGGBB value, each component is a byte, therefore from 0 to 255 in value.
   var blueOffset:Number = color % 256;
   var greenOffset:Number = ( color >> 8 ) % 256;
   var redOffset:Number = ( color >> 16 ) % 256;
   var alphaOffset:Number = ( color >> 24) % 256;
 
   // i'm not sure why sometimes the color values are negative, but I remember 7 years ago in C++ under windows I did this hack and it worked.
   if(greenOffset < 0)
      greenOffset += 256;
   if(redOffset < 0)
      redOffset += 256;
   if(blueOffset < 0)
      blueOffset += 256;
 
   //trace("blue:"+blueOffset+", green:"+greenOffset+", red:"+redOffset+", alpha:"+alpha);
 
   // recompose the 0xAARRGGBB value
   return (alpha<<24|redOffset<<16|greenOffset<<8|blueOffset);
}

Voilà. Post your comments or questions! Or even better, improve the efficiency of my code!

Things I would like to see done in AS3

Since AS3 offers raw sockets and enables byte-arrays, there is no reason why we couldn’t build the following:

  • MySQL Connector
  • Oracle Connector
  • SVN Connector

For example, if someone wanted to make a UML modeling program online, it would be possible to plug it directly on an SVN serveur (or CVS .. or anything else you fancy), and do reverse engineering on the classes, showing you a complete UML view of your code! Then you could do live re-factoring visually, like move functions around, create new objects, and have the program update all your code for you. Now that’s what I call something usefull!

Anybody’s up for it?

AMFPHP & PHP 5.2.2 Bug

Hi. I am hosted on dreamhost and they recently upgraded their version of PHP5 to 5.2.2. There is a bug in PHP 5.2.2 where the $_GLOBALS['HTTP_RAW_POST_DATA'] never gets set, a global variable which is used by AMFPHP to read the request from the Flash application.

To fix it, edit the file gateway.php find this line of code :

[php]
$gateway = new Gateway();
[/php]
and add these lines before it :

[php]
if (phpversion()==”5.2.2″)
$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents(“php://input”);
[/php]

Cheers!