Joe's Blog

line

A blog by Joe Billman about programming, and other stuff

line

Archive for the ‘Flex’ Category

AS3 Singleton

Posted on: February 7th, 2014 by Joe

line No Comments

Singleton classes are very useful. So I wanted to show a simple sample of a Singleton class. I like to use these as models to store data in one location that multiple parts of your application can access. Singleton classes in ActionScript 3 for the most part are just like they are in other OOP languages. There is one minor difference. Since you can’t make a constructor private you have to add a few lines of code in the constructor. Here is a sample:

public class Singleton
{

	private static var _instance:Singleton = new Singleton();

	public function Singleton()
	{
		if(_instance != null)
		{
			throw new Error("Instantiation failed");
		}
	}

	public static function getInstance():Singleton
	{
		return _instance;
	}

}

That is my preferred way to do it for models that you know are going to be instantiated. Then you just add public variables and you have a model. Generally when I’m using models I always plan to instantiate them, but if you are not sure if you are going to instantiate the class you may want to do it like this:

public class Singleton
{

	private static var _instance:Singleton;

	public function Singleton()
	{
		if(_instance != null)
		{
			throw new Error("Instantiation failed");
		}
	}

	public static function getInstance():Singleton
	{
		if(_instance == null)
		{
			_instance = new Singleton();
		}
		return _instance;
	}

}

 

Get Spark Image width or height

Posted on: January 9th, 2014 by Joe

line No Comments

So one of the most annoying things about working with display items in the Flex framework is the difference in how things are rendered. This means that after you have created an object in AS3 code you can’t get the properties of that object until after a certain event in the creation process. Generally you need to listen to FlexEvent.CREATION_COMPLETE. After that fires you can usually get width and height etc. the Spark image is a little different. The best way that I’ve found is to listen to Event.COMPLETE and then get sourceWidth or sourceHeight instead of width or height. This one drove me nuts for longer than it should have so hopefully if you have this problem you can find this post before it drives you nuts.

TypeError: Error #1007: Instantiation attempted on a non-constructor

Posted on: November 11th, 2013 by Joe

line No Comments

So I know that many people are jumping ship when it comes to Flash and Flex when it comes to web applications but if you are one of the ones like me that is still doing this type of development you might run in to this sometime. The app that I am working on is a Flex app that uses the 4.6 sdk. The is a central module that loads other modules. The problem I ran into was on one of the modules that was using an AdvancedDataGrid. I kept getting the Error #1007: Instantiation attempted on a non-constructor. The solution ending up being something very simple. The main application just needs to have an instance of the AdvancedDataGrid. I simply added AdvancedDataGrid; that along with the import and you should be good to go.

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

Posted on: June 18th, 2013 by Joe

line No Comments

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.

TypeError: Error #2007: Parameter antiAliasType must be non-null

Posted on: June 14th, 2013 by Joe

line No Comments

I got this error when I was trying to create a mx DateChooser via code instead of mxml. To make a long story short the solution was to move the call to the function that was attempting it to create it. It was being called from the constructor at first and I moved it to be called after the parent object was added to the stage and that did it. If you are getting this error, try and call whatever code is causing it later in time. I hope this helps.

Starling iOS app rotates after splash screen

Posted on: April 5th, 2013 by Joe

line No Comments

So I’ve been using Flash Builder and the Starling framework to create an iOS app of the Card Match game that I made a while back. I must say I’m pretty happy with the results, but there have been a few quirks. One of them was driving me nuts. I’m not sure how many of you will run into this but if you do hopefully this post will help. Here’s what it was doing: When the app would launch it would show the splash screen like it should and right after the splash screen was finished my whole app would rotate so that it was now upside down. I tried changing and then changing again the full screen and auto rotate properties in the app xml and nothing happened. So then I decided that I would just recreate the project in Flash Builder, so I made a new project and imported my classes, built it, and everything worked fine. Again I’m not sure why or if it was just a fluke, but if this happens to you, try making a new project.

I was recently working on a Flex Mobile Project and kept having this happen. After a lot of time and frustration I finally figured it out. By just adding -swf-version=29 to the compiler options that solved the problem. I think that it somehow wasn’t using the last AIR SDK somehow. I used the latest Apache SDK and it worked so I knew that it had to have something with the version. My guess is that without having that in there to force it to use the latest one it’s just missing something.

How to embed fonts using actionscript 3

Posted on: May 27th, 2010 by Joe

line No Comments

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.


Warning: Use of undefined constant Y - assumed 'Y' (this will throw an Error in a future version of PHP) in /homepages/16/d145171736/htdocs/php/footer.php on line 1