AS3 Vector Speed Tests

Posted August 31st, 2010 in Actionscript, Nerd by Jorsh

One of my co-workers at McKinney asked me a few days ago if the gains for using the new AS3 Vector class were worth the trouble.  I linked them this article by Mike Chambers, where he does a really nice speed test of the Vector class versus the common array.  I still wanted to try my own test just to see, and the results are actually very different from what I expected.

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
package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.utils.getTimer;
    import org.flashdevelop.utils.FlashConnect;
   
    public class Main extends Sprite
    {
        protected var _vectorTest:Vector.<int>;
        protected var _arrayTest:Array;
       
        protected static const TOTAL:int = 5000000;
       
        public function Main():void
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
       
        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
           
            _vectorTest = new Vector.<int>(TOTAL, true);
            _arrayTest = [];
           
            for (var k:int = 0; k < TOTAL; k++) {
                _vectorTest[k] = k;
                _arrayTest[k] = k;
            }
           
            for (var m:int = 0; m < 20; m++) {
                runVectorTests();
                //runArrayTests();
            }
        }
       
        protected function runVectorTests():void {
            var vectorStart:int = getTimer();
            var count:int = _vectorTest.length;
            for (var m:int = 0; m < count; m++) {
                _vectorTest[m] = Math.random() * TOTAL;
            }
            var vectorFinish:int = getTimer() - vectorStart;
           
            FlashConnect.atrace(vectorFinish);
        }
       
        protected function runArrayTests():void {
            var arrayStart:int = getTimer();
            var count:int = _arrayTest.length;
            for (var n:int = 0; n < count; n++) {
                _arrayTest[n] = Math.random() * TOTAL;
            }
            var arrayFinish:int = getTimer() - arrayStart;
           
            FlashConnect.atrace(arrayFinish);
        }
       
    }
   
}

As I expected, in all of my tests the Vector class beat the crap out of the array.

Vector: 450 ms average

Array: 753 ms average

What I was curious to see was if setting the length of the vector, and setting it to fixed would lead to another performance leap.

1
_vectorTest = new Vector.<int>();

Plain Ole Vector: 455 ms average

1
_vectorTest = new Vector.<int>(TOTAL);

Vector w/ Length NOT Fixed: 454 ms average

1
_vectorTest = new Vector.<int>(TOTAL, true);

Vector w/ Length, Fixed: 452 ms average

These results are VERY disappointing!  While the performance of the Vector is truly impressive, isn’t the point of fixing a Vector’s length for more speed?  I don’t know if I can justify the effort for an average of 3 ms gain.  Keep in mind this an average, the plain vector actually beat or tied the others sometimes.

Here’s a  link to my FlashDevelop project if you want to try for yourself.

Saving a 32 bit PNG from Photoshop

Posted August 24th, 2010 in Artwork, Gaming, Nerd by Jorsh

Recently I needed to save a 32 bit PNG from Photoshop for some Android game development, and I noticed that saving for web would only output a 24 bit PNG. So to figure out what was happening, I made a test image, 32 x 32 and solid red. Despite any options I selected from the save for web screen, Photoshop would still not save out a 32 bit PNG!

My solution? Ditch the save for web option altogether and just go with “Save As”, and select PNG from the list.

When FlashDevelop just won’t trace

Posted July 23rd, 2010 in Actionscript, Flash by Jorsh

FlashDevelop is my primary IDE for Actionscript development, and oh how do I lurv some FlashDevelop. The only one huge problem it that for some reason, FlashDevelop likes to eat Actionscript trace statements. The output window won’t show them, and neither will the logger, and for some reason it sometimes doesn’t send traces to my Flashlog.txt file either.

After fighting the battle of consistent traces in FlashDevelop, I stumbled upon a native FlashDevelop class that seems to solve all of my problems. If you import org.flashdevelop.utils.FlashConnect, you can then call a number of static methods on the Flash Connect class.

I’ve been using the FlashConnect.atrace(), which acts just like the native Actionscript trace, but hasn’t failed on me yet. There is also a FlashConnect.mtrace() method, which acts just like the MTASC trace method, and finally a FlashConnect.trace() which seems to act like the AS1 and AS2 trace.

This class is included in FlashDevelop, so you dont have to manage any files, just import the class org.flashdevelop.utils.FlashConnect and you’re ready to go!

How to change your FlashDevelop syntax colors

Posted July 21st, 2010 in Actionscript, Flash, Nerd by Jorsh

If you’re on Windows 7 or Windows Vista, you might have probably tried to drop your syntax theme files into your Flash Develop install folder, but nothing happens? It’s because that’s not really where you need to put them. If you open FlashDevelop, go to Tools -> Application Files … and viola! Apparently the settings files are buried deep in the Local User folders, but if you open the Settings folder, you should be able to paste in any FlashDevelop colors that you have.

Need some fancy FlashDevelop themes? Try the FlashDevelop forums on the subject at: http://www.flashdevelop.org/community/viewtopic.php?t=2860

I myself use elhector’s very awesome high contrast theme.

Using a Level Descriptions file with PushButton Engine

Posted July 19th, 2010 in Actionscript, Flash by Jorsh

Loading .pbelevel files in Actionscript is easy enough using the PushButton Engine, but I’ve had a hell of a time finding documentation on how to load levels using external Level Description xml files.  Using a level description file makes it so you don’t have to addFileReference or addGroupReference for each of your entities and groups.  The file serves as a collection of all of those by level.

The code to load the level descriptions file is very simple:

1
2
3
4
5
6
7
public function Main():void
{  
    PBE.startup(this);
    PBE.addResources(new Resources());
    PBE.resourceManager.onlyLoadEmbeddedResources = true;
    LevelManager.instance.load("../xml/leveldescriptions.xml", 1);
}

In my code example, I’m embedding all of my xml into a ResourceBundle called Resource

1
2
3
4
5
6
7
8
public class Resources extends ResourceBundle
{
    [Embed(source = "../xml/level.pbelevel", mimeType="application/octet-stream")]
    public var Level:Class;
   
    [Embed(source = "../xml/leveldescriptions.xml", mimeType="application/octet-stream")]
    public var Description:Class;
}

I easily got this far by following the tutorials on the PushButton site, but I was missing one key portion in my leveldescriptions.xml file. First, here’s the end of my level.pbelevel file, which is my example first level:

1
2
3
4
5
  <group name="Stuff">
    <objectReference name="Scene" />
    <objectReference name="Manager" />
    <objectReference name="hero" />
  </group>

In this block I’m putting my scene, spatial manager, and character into a group like I’d normally set using addGroupReference. But to have this group called on the level load, I have to add a group xml node in my leveldescriptions xml file or it won’t load. Without it, the level will load but it won’t load the group at all. Here you can see what is needed in the leveldescriptions file:

1
2
3
4
5
6
<LevelDescriptions>
   <level index="1">
      <file filename="../xml/level.pbelevel"/>
      <group name="Stuff"/>
   </level>
</LevelDescriptions>

Here’s a link to my source, hopefully no one else will have to spend as much time on this as I did!