Showing posts with label flash. Show all posts
Showing posts with label flash. Show all posts

Friday, February 12, 2010

Newbie tip: Declaring and Managing Linked MovieClip Instances in Parent Classes

Suppose that we have two MovieClip-based classes called MegaNode and SimpleNode. We have created both of them by drawing two vector shapes on the scene and converting them to those Movieclip-based classes. For some reason, we want to call the instance of the shape that's inside - mainShape, therefore we convert it to a MovieClip, and call the instance mainShape.

Now, since on many occasions the MovieClip class is too general for our purposes, we define a SuperNode class, which extends MC, and servers as the parent for both M
egaNode and SimpleNode. We need to be able to manipulate the mainShape instance, so we instantiate it in the SuperNode class:

public var mainShape:MovieClip;

if we simply do that, the compiler will throw the following error: "1152: A conflict exists with inherited definition SuperNode.mainShape in namespace public."
The reason for that is that by default, all instances of linked MovieClips, such as mainShape, are automatically declared by default at runtime. In order to make sure that we don't get into that error again, we need to go to the ActionScript s
ettings, and uncheck the "Automatically declare stage instances" checkbox. With that done, the above mentioned code will work, and we'll be able to manipulate the mainShape instance from the SuperNode class

NOTE: Make sure that instances like mainShape are always declared as public


Wednesday, February 10, 2010

stage.width, stage.stageWidth, and loaderInfo.width

For any new Flasher comes the time of dealing with the stage and its properties. It is probably confusing for newcomers to distinguish between stage.width and stage.stageWidth, therefore this post. (the same logic follows for stage.height and stage.stageHeight)

stage.width is defined by the distance between the smallest and the largest x coordinate that its children reach at any certain point in time. If we imagine the stage as a balloon, the further we inflate it, the bigger the distance between its walls gets. Therefore, if you have tried to determine the width/height of the stage using the stage.width/stage.height properties, before anything has been put on the stage, the output must have always been 0.

stage.stageWidth, on the other hand, defines the width of the entire Flash window.

NOTE: Normally, one would assume that if the stage's scale mode is set to StageScaleMode.NO_SCALE, the stage.stageWidth will remain constant, no matter how much we resize the Flash window. Well, no .... stage.stageWidth changes all the time, regardless of what the documentation says.

If you want to get the value of the stage width, as defined in the project settings, or the SWF metadata, you should use loaderInfo.width

Monday, June 29, 2009

Pretty Good Impressions from Flash : Movie Clips

I've just recently started using Flash IDE as a development environment. Having a programming backgroung, I've always considered the Flash IDE too "out of the box" for development work. Plus, the built-in ActionScript editor, which I still do not like and try to avoid using always put out my efforts to deal with it. That's why my first experiments with ActionScript turned out to be using Flex. Flex is great, there is no doubt in that, but every tool has its particular purpose. When one wants to setup a WIMP web or desktop app, Flex is really the right one. For rapid prototyping and game development, though, it is not providing results fast enough. This is where Flash comes in.

I've always had difficulties figuring out what the movie clip was and how it worked. Well, most Flash books tend to explain it in the wrong way. Pretty much, a movie clip is the graphical representation of a class. In Flash, a movie clip is what a class is in programming - an abstract description. Just like in programming, a movie clip can be instantialized and changes to the class apply to all instances. Movie clips can be composed of instances of other movie clips, and every movie clip has its own timeline - every movie clip could have its own states and animations. I am coming to the greatest part for every developer - every movie clip can be linked to an ActionScript class. The only requirement for the class is to extend the MovieClip class. Isn't that great? As an experiment, I setup a simple project in which I created a gameCharacter class with simple logic that my game character should implement (methods like jump(), and accelerate()). Then I linked it to a Charater movie clip, and it worked like magic. Simply great.