TypeError: Error #1009: Cannot access a property or method of a null object reference – UIMovieClip

So I ran into this error again today. I first ran into it a few years ago. It happens to me when I have an asset that is created in Flash Pro and used in a Flex application. It happens when that component has focus and the whole application then loses focus and then receives focus again. The UIMovieClip class has some issues and so if you are getting this error try one of the following solutions. For either of these solutions you’ll need to put an asset on the root level of your application that extends UIMovieClip, DisplayObject or there are a few others. It has to be an object that the FocusManager instance of the main app can set the focus to. I like to use a logo or some other visual element that I can create in Flash and that will be on every view of your application (root level). What you want to do is before removing any UIMovieclip object from the display list, have the application set the focus to the logo or whatever you’re using. The first way of doing this is by adding the deactivate listener to your flex application. Then have the handler function for that set the focus. This works most of the time, but I’ve found some instances where it doesn’t. If you have text fields that have keyboard listener it doesn’t work. The way I prefer is to dispatch an event from each of my UIMovieClip objects before they are removed (I like to put it in a destroy function). That event bubbles all the way up to the app and then the app calls the function to change the focus. Here is an example:

this.focusManager.setFocus(logo);

That’s it. I hope this might help some of you from pulling your hair out. You want to keep all of the hair that you have, believe me I know.

Recommended MP3 Settings for Flash Player

I recently ran into another MP3 issue while working on a Flash Project. I found this one while working on an iPad app in Flash Builder. I meant to write about this issue a long time ago when some mp3 files that I was using on a website sounded really bad in the Flash Player. So here is my recommended MP3 Settings for mp3 files used in ActionScript applications:

  • Sample Rate – 44100 Hz (this one is the most important, if it is not set to this, it can sound bad, slow, fast, garbled)
  • Channels – Stereo or Mono work fine (for most sound effects I use mono)
  • Bit Depth – 16
  • Sample Rate – 128 Kbps (you can use higher or lower, I haven’t noticed too much improvements going higher, but it’s definitely noticeable as you start going lower)

This is probably common knowledge to most people working with Flash applications and ActionScript, but if not, I hope this helps.

SoundChannel position doesn’t = Sound length

So I recently made an AIR app at work that we use for finding cue points in an mp3 file. It loads an mp3 file, creates the sound wave spectrum for said file and then allows the user to target different locations in the file and output them as cue points. While working on this project I noticed that I was getting weird results on the the location of the play head in the audio file. It was really noticeable at the end of the file. The position property of the SoundChannel object at the end of the file didn’t equal the length property of the Sound object. It was a variable amount of milliseconds behind it. After a beating my head for a while and doing some searching online I realized what the problem was. It was all due to the bit rate at which the mp3 was saved. Anything below 128 was causing it to be off and the amount that it was off varied depending on the bit rate. If it was saved above 128 it was fine, but below caused problems. So if you are playing mp3 files in the Flash Player the ideal settings are 44100 Hz with a bit rate of 128 Kbps or higher. Also just as a side note if you don’t sample it at 44100 Hz the audio actually sounds bad as well. Hope this helps. Flash Player is still the best way to play audio files on the web.

Flash Player Garbage Collection.

So the more you work with ActionScript 3 and especially when it comes to building RIA’s (Rich Internet Applications) or games, the more garbage collection will become really important. When I first started down the road of memory management and garbage collection there were a lot of things that were very confusing. This post is to share the things that I’ve learned and hopefully help others avoid much of the confusion. The first thing to know is as of right now there is no real way of triggering a garbage collection pass on demand, but there are some good practices to follow that will ensure that objects are flagged to be collected. The first thing to be aware of is that if you are not using the Premium version of Flash Builder to profile your swf file you are going to have a difficult time knowing if it’s cleaning itself up properly. There are some other tools out there to help with this, but as off the time I wrote this Flash Builder Premium is by far the best. One tool to keep in eye on though that looks very promising is called project monocle. Do a Google search for it. Covering how to profile a swf is a little beyond the scope of this post, but when you’re writing your code here are the things I’ve found that you want to look for.

  1. Remove all event listeners.
    It’s good practice to use weak referencing when adding listeners. To do this set the last parameter in the addEventListener function to true. Doing that is not a guarantee though so remember to always remove listeners.
  2. Set all complex data types and any other object that contains a reference to another object such as Arrays and vectors to null.
    Although you can’t call the garbage collector on demand, this is the best way to ensure that an object is marked for collection on the next pass(as long as listeners are remove prior).
  3. Stop and nullify all timer and loader variables.
    Timers and loaders are notorious for hanging out in memory so make sure you watch them carefully.
  4. Ensure that all display objects are off of the display list before trying to destroy them.
    It’s rare, but there have been times when I’ve had to manually remove children from a parent before destroying the parent.
  5. Start a habit of good coding practices that works for you.
    So after you know what types of things to look for in your code that can lead to memory leaks, start making some habits to help you avoid those things. One thing that I’ve found helpful is to create a “destroy” or “cleanup” function for every class. Another thing that I’ve started doing that is very helpful is to make “create” and “destroy” functions for every complex object that I instantiate. So for example say that you were making a game that had a hero class, the two methods would be “createHero” and “destroyHero”. In those functions is where the object is created/destroyed(null) and where listeners are added and removed. Then you would have one public function that when it comes time to do the overall cleanup would call all of the destroy functions for the different objects. You can download a sample AS file that illustrates how to do this.

Memory management is very important in polishing any game or project. I hope this helps.

Is Flash a Dying Technology

So no surprise I’m a big Fan of pretty much everything Flash. Because of this people will ask me things like “Flash you’re still doing that?” and “Isn’t that a dying technology?” and my response is “NO!”. I’ve blogged about this before, but let me just state again that I personally feel that the technology behind the Flash Player was, and still is ahead of its time as far as delivering rich content on the web is concerned. You create something once and you can deliver it everywhere that the Flash Player is installed. And that is pretty much everywhere. Except on iOS devices. iOS is a different story and I’ll come back to that. Leaving Flash in favor of HTML 5 is a step backwards as far as I’m concerned. Don’t get me wrong, I think that HTML 5 is great, it’s just inferior to Flash and it’s not as ready as everyone who supports it would have you think. When I say inferior I am referring to using it as a tool to create rich internet applications and games, the two areas where I feel Flash is the better choice. If you just want to make a sweet website with a cool little jQuery navigation then by all means use HTML 5. The other thing that I feel is disappointing is how everyone is deciding to leave Flash right when it is getting good. Flash player now supports processing on the GPU and multi-threading, things that I never thought I’d see. ActionScript has come a long way and in my opinion is a very powerful and robust object-oriented programming language. So with that said let me address the iOS thing. Now before you get all upset and run off telling everyone that I hate Apple, I want to make it clear that I like Apple products and own three iPods and love them. Apple makes great (expensive) stuff, and they are brilliant marketers and innovators. With that said I strongly feel that they, like any other company do what’s in the best interest of themselves, and that’s fine. The part that rubs me the wrong way is that that’s not what they say. Let’s take the now famous stance from Steve Jobs himself against the Flash Player (again don’t get me wrong, Steve Jobs did a lot of good for the world of computing). So he said that it wasn’t secure and that it didn’t perform very well both of which I think are bunk. Then he went on to say that HTML 5 was the way of the future. So here’s my question: If HTML 5 is the way of the future, why does Apple not use it on their own website to display video content instead of Quicktime? Isn’t Quicktime all of a sudden a dying technology as well? It seems a little one-sided, just saying. I recently purchased an Android tablet and wanted to go watch some movie trailers on Apple’s website, but what do you know I couldn’t because I needed Quicktime and at the time of this post I couldn’t get it for my Android device. I’m not trying to bash, I just wish those to whom a majority of people look up to in the computer world would be a little more honest and not just say things that will benefit their personal interest. Right now is a great time to be in the computer world. There are new technologies coming out all the time that are revolutionizing things. Let’s embrace those and use them, but in doing so let’s not attempt to kill others that are also great.

FPS and Memory Profiler

With Flash shifting more of it’s focus to games it is becoming more and more important to optimize and ensure good performance. A great way to gauge performance is by the frame rate that your game can maintain on the desired device. I recently made a simply little tool that can be used to test the frame rate of your game or app. It is really easy to use, it only requires one line of code. Here is what it looks like:

as3 fps profiler

Here is all the code that is needed:

FpsProfiler.profile(this.stage);

I hope this can be useful to some of you looking to optimize your Flash games. Please feel free to give me some feedback. To download the source files click on the link in the bottom right hand corner. Enjoy!

 

Download Source Files

The Future of Flash

So I recently read an article that I like to call The Future of Flex and a couple of days ago I read The Future of Flash. We’ve known for a while now that Adobe is shifting their focus for Flash to online gaming and high end video online. In this article there are some new insights however the biggest in my opinion being ActionScript 4.0. They don’t come right out and say that, but an overhaul of ActionScript = new version in my mind. It’s a great article worth reading. Go check it out here: http://www.adobe.com/devnet/flashplatform/whitepapers/roadmap.html

Joe Billman = ACE (Adobe Certified Expert)

Recently I did something that I’ve been meaning to do for a long time. I took the exam to become an Adobe Certified Expert in Flash Professional. Those who know me know that I love Flash and have been using it for a long time. It feels good finally have something other than my experience to verify my abilities in Flash. Okay that’s enough of my 5 minutes of Flash fame.

adobe certified expert in flash

View Certificate

How do I change the retry time in SmartFTP

So if you do much with ftp you have probably heard of SmartFTP. If not I would highly recommend them. I have used a variety of ftp programs and theirs is by far the best that I have used up to now. It has a lot of great features and is reasonably priced. If you have used it then you have also most likely ran into the issue that arrives if you attempt to upload a file and it fails causing it to state that it will retry at (time stamp) future. The default is 30 seconds from the time that it attempted to upload the file. Well for me the problem is 30 seconds is much more than I care to wait. I do a lot in Adobe Flash and I have SmartFTP set to monitor the .swf file for the .fla file that I am working on. This way every time I publish the file it will automatically upload it for me. The problem is that SmartFTP recognizes the file having been changed right as Flash begins to compile the swf not when it is actually finished. This means that every time I would have to wait 30 seconds. Well there is a solution and here it is. To change the retry default time value go to Favorites > Edit Favorites. In the Favorites window, go to Tools > Edit Default Favorite. In the Properties window, select the “Connection” menu, and there you will see a setting “Retry Delay” it is set to 30 seconds by default. There you can type in whatever time you want. For most small swf files 1 second has worked for me.

How to embed fonts using actionscript 3

This was causing my to bang my head for a little while. There are different ways depending on  whether you are using Flash or Flash Builder (previously Flex Builder). If you are using Flash you can go here for a good example/tutorial: http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/. Basically you add a font to the library by clicking the pop-up menu (in the upper-right corner of the Library panel), export it for actionscript and then give it a class name. After that your code looks something like this:

var myFont:Font = new Font1();
var myFormat:TextFormat = new TextFormat();
myFormat.font = myFont.fontName;

Embedding fonts in AS3 seems to be a big problem for many people out there especially if you are using Flash Builder/Flex Builder. I know it was driving me crazy! I was following the examples, but still seemed to keep getting the same error: “An Embed variable must not have an existing value.”. Okay maybe it’s just me but that one was a little cryptic. Finally after a long time, but before having to do a Google search for straight jackets I figured out what I was missing. In all of the examples the embed line was right above a Class variable, but mine was not, so I added one and  cha ching it worked. Here is a sample:

[Embed(source="assets/MONACO.TTF", fontName="monaco",
mimeType="application/x-font-truetype")]
private var MonacoFontEmbed:Class;

It may just be me but this was not obvious I must confess. You don’t use that variable at least I didn’t, you simple set the .font property to the fontName value. It looks something like this:

myFormat.font = "monaco";

monaco is what I named it. You can name that whatever you want along with the variable. I named it MonacoFontEmbed, but you can name it whatever. I hoped this helped. If you have any questions feel free to post them. Embedding fonts is very useful especially if you are planning to manipulate a text field. Manipulations like rotation will just cause your text field to disappear if created dynamically and without embedding the font.