Archive for the ‘Flash’ Category

ScrollPane doesn’t clip loaded content

April 17, 2008

When a ScrollPane is used to load media (Flash 8 IDE), sometimes the loaded content didn’t get clipped (it either sits on top of the scrollpane or goes underneath it).

Cause: An absolute URL is used (such as “myScrollPane.contentPath = ‘http://www.mydomain.com/img/image.jpg’”) for the loaded content.

Solution: Change the absolute URL to relative URL (”img/image.jpg”). Of course you should be able to put the loading swf file and the loaded contents on the same domain.

Full-screen mode in Flash 8

February 19, 2008

How to create true fullscreen movies with Flash

Exploring full-screen mode in Flash Player 9

Quick facts:

  1. Only supported with Flash Player 9.0.28  or above;
  2. Can be implemented in Flash 8 IDE by modifying the Stage class;
  3. Can only be initiated with  user inputs such as mouse click or keypres;
  4. An overlay dialog box will appear when the movie enters full-screen mode, instructing the user how to exit and return to normal mode.

Timeline, MovieClip and AS 3 101

October 8, 2007

1. A MovieClip subclass instance starts playing as soon as it’s been instantiated:


var myMC:MyMC = new MyMC();

AddChild(myMC); or removeChild(myMC) doesn’t affect the playhead in myMC, meaning if there is no framescript, myMC will continue playing regardless of whether it’s been added to the playlist or not.

2. To use the versatile/artistic/rétrospectif/timeline-script style, one thing to keep in mind is if any children MovieClip symbols are physically placed inside the timeline of your MovieClip subclass, you can either have Flash make those variable declarations automatically (and thus you should not declaring them as variables in your timeline script), or associate your movieClip symbol with an external class file definition and remove those variable declaration in the class. (in such a case, you will still have Flash declare those children instances variables for you but you will have to make them public in your class.)

Link: Flash CS3: Automatic Timeline Declarations

3. To use an external class definition for a MovieClip symbol and associate timeline script with it, use addFrameScript(frameObj1, frameHandler1, frameObj2, frameHandler2, *rest);. Note that frameObject is zero based: meaning if you want to use frame number, frame one would need a value of 0.

4. To determine if a displayObject is on the playlist or not, check to see if either of the following revolves to true or false:

displayObject.stage

displayObjectParentsOrAncestor.contains(displayObject)

5. Since the timeline script is synchronized by “Event.ENTER_FRAME“, it might be it’s delayed because of other script took longer to execute. As an animator, I found myself, especially in my earlier days of using Flash, put a stop() on some frame (say that frame is labeled “paused”) of a MC symbol and in my client code, I need to gotoAndPlay("paused") to resume from that specific point. It would sometimes get confusing since gotoAndPlay("paused") won’t always resume the playhead, instead it seems to freeze at that certain frame; that is because the frame script stop() is executed AFTER gotoAndPlay("paused") is executed . A quick-n-dirty workaround is to simply do the following:

gotoAndStop("paused");
gotoAndPlay(currentFrame+1);

or

gotoAndPlay(pausedFrameNumber+1);

6. Render Event

7. Flash 9: Timeline navigation and code execution

useHandCursor, buttonMode and mouseChildren

October 7, 2007

To display the hand cursor for a displayObject with a dynamic textfield inside it (Suppose the textfield is on top of a shape):

mySprite.buttonMode = true;
//By default useHandCursor is true;
//mySprite.useHandCursor = true;
mySprite.mouseChildren = false;

Publishing files for local deployment in Flash 8

August 22, 2007

In order to deploy an swf file that is published to Flash 8 and is wrapped in an HTML file on a CD/DVD or thumbdrive, the following set-ups are required:

1.  “Local playback security” needs to be set to “Access Local files only” if the swf needs to load other local swf files. If set to “Access network only”, the swf will fail to load/access other local swf files. “Local files” means the files that reside in local FileSystem or in movable media that are accessable to the FileSystem. They are usually accessed via something like “file:///C:/folder/fileName.swf“, etc, or a relative path like “./folder/fileName.swf”. Network files are those that are accessed via an iNternet protocal such as “http://sub.domainname/fileName.swf“.

2.  If “getURL” is used to access files on the Internet or call pseudo JavaScript functions within Flash Player, “allowScriptAccess” needs to be set to “always”. Otherwise, both IE and Firefox will fail those getURL calls.  If not wrapped in HTML, request to access an HTML page via “getURL” will be successful, provided the file exits, but the browser window will be obscured by the projector or the swf file and will not get auto focus as if the call is made via a web browser.

The above statements are true when tested on FP 8 and FP 9.

MVC/UMC revisit

August 7, 2007

I was doing a simple audio clip playlist a couple weeks ago. Such a small thing got to the level at which I almost couldn’t handle it when I started to try to put some buttons to indicate and prompt users to toggle “repeat all” on and off. It works like this:

If no clip is playing and “repeat all” is off, the player displays “toggle off” icon while displaying a “play all clips” button when mouse rolls over it. If a clip is playing and “repeat all” is off, the player instead displays “turn on autoplay” when mouse-over. When “repeat all” is on, the player displays “repeat all on” icon on “mouse-up” state and “turn off autoplay” on “mouse-over” state.

I got into interwinded “if-else” conditions and was almost lost. I spent almost a whole day and still couldn’t get the buttons work together nicely. I think to myself hey if such a small thing takes me so long to figure out, what else could I possibly do? I have been doing ActionScript full-time for about 3 years now; although most of my stuff is not stellar in terms of the coding level, I’ve gained quite a bit of confidence in coding UI and animation. So I decided to give myself a little break and went to the library and Borders. After reading and thinking, I finally think I not only got the stuff done, but also showed a bit of logic in my spaghetti code. At the end I list the books/articles that I have been reading for the past couple of days. Not that I feel I have been elevated to another level, but here are just some thoughts:

Basically Control is like a hub. It knows about both View and Model and connects View and Model. It contains the logic of the UI. The responsibility of Control basically is about coordinating all the events and actions that affect both UI and Model. Many of them (but not all) are about how to react to the requests from UI and updates it; if there is anything it cannot decide by itself, it relays them to the Model. The rule of thumb here is that if an interaction won’t affect other UI components and doesn’t request anything from Model, Control will handle it directly (like most of the roll-overs) without consulting Model.

If the UI is requesting anything from Model (like asking to play another song or movie), Control would not change View directly. Instead it sends the request to Model and Model makes changes accordingly and then signals Control about the updates. Control/View receives the information about the changes and View is then updated (like dehighlighting the currently highlighted button and highlighting the button that is associated to the clip that is requested, assuming the clip starts playing.)

The change of UI doesn’t necessarily initiated by a request from UI. It could be a system event, like a song completes playing, or a change from Model, like a new song has been added to the pool of all the available songs on the server, etc.. Control should have routines to handle all these changes (such as “handleClipComplete()”, etc..)

It’s just a matter of your coding style and about the complexity of the entities you are dealing with when it comes to whether to use classes (each of which present only a segment of MVC), or whether to have one UI component per View, or just to stuff everything in one big chunk of code. It’s the thought behind it that matters.

It helps me a lot since I’ve started to learn to draw diagrams of the states (statechart) and then to figure out all the classes and operations/attributes from it. I am using paper and pall pen instead of any of the UML tools. It helps me to concentrate on the thinking.

Readings:

Embedding fonts in Flash CS3/AS3

July 29, 2007
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.Font;
import flash.utils.getDefinitionByName;

    public class SimpleTextPaneTestDrive01 extends Sprite
{
private static var embeddedFont:Font = null;

        //CONSTRUCTOR
public function SimpleTextPaneTestDrive01()
{
var embeddedFontClass:Class = getDefinitionByName(”Font1″) as Class;
Font.registerFont(embeddedFontClass);
var embeddedFontsArray:Array = Font.enumerateFonts(false);
embeddedFont = embeddedFontsArray[0];
var fmt:TextFormat = new TextFormat();
//fmt.bold = true;
fmt.color =0xffffff;
fmt.size = 16;//Old day fashion: If you embed it in a textfield on stage
//fmt.font = “Helvetica”;
fmt.font = embeddedFont.fontName;

            var tf:TextField = new TextField();
tf.text = “hello world!”;

tf.setTextFormat(fmt);

}

}
}

Links:

Loading fonts at runtime
Using hasGlyphs() with embedded fonts

Declaring stage instances from Library

July 17, 2007

1. Uncheck “Publish settings->AS3->Automatically declare stage instances”;

2. Right click Sprite/MovieClip in Library. Check “Linkage”, assign”Class”and “Base class”. I think the base class is either “Sprite” or “MovieClip”;

3. Declare instance as a property of the Document class. The class must be declared as public in order not to get a compiler error. Like so:

public var bkgdrop:Backdrop;

or

public var bkgdrop:Sprite;

Links:

Class path / Document class

July 16, 2007

It took me like 10 minutes to figure out…

The class path, if under the directory “Classes” which is inside the same directory as the running .fla, should be:

./Classes

Not:

Classes

Nor:

/Classes

Document class (accessed by right-click on stage) is dot name-spaced.

Garbage collection in FP(AS3)

July 15, 2007

1. To remove an object (to authorize Flash Player to garbage collect it in the next GC cycle), simply remove the references to the object by doing so:

var fruit:Fruit= new Apple();
fruit = null;

or reuse the reference and stuff it with another object, like so:

fruit = new Orange();

2. Since garbage collection of a large amount of objects will be a big performance hit, rule of thumb is, you should strive to reuse rather than to delete an object for GC whenever possible.

3. When the programmer authorizes FP to garbage collect an object, he should always remember the followings:

  • Remove all the event listeners of the target before removing it;
  • Remove all the intervals, timers (Timer/timeout) that are associated with the object;
  • Remove all the timeline functions within the instances of DisplayObject that are associated with the object;
  • Remove all the tween instances and onEnterFrame handlers that are associated with it.
  • URLLoader is also on the blacklist as some people noticed.

Example:

Before refactoring:
public function set tweenPosX(v:Number):void
{
//var duration:Number = UNIT_DIST_DURATION*Math.abs(v-posY);
var duration:Number = .5;
//if(duration==0)return;
var tween:Tween = new Tween(this, “posX”, Regular.easeInOut, posX, v, duration, true);
tween.addEventListener(TweenEvent.MOTION_FINISH, tweenPosXFinish);
}

The code above is problematic since tween is a local variable and it registers function tweenPosXFinish() within the setter function. When the setter function finishes execution, tween is disposed of for GC while its TweenEvent listener is not. The listener object will take up more and more memory as consecutive calls to the setter function until the next mark/sweep GC process occurs. both tween and its listener function remain in memory; all the instances get stacked with sequential tweening until the next Mark/Sweep process occurs. The app takes up around 6mb (”6,569,984″) memory in standalone player and continues to take up more memory with sequential calls to the setter/tweening function until it reaches around 8mb(”7,909,376″, etc.); In a fresh web browser (without other tabs/windows open), it starts taking around 2mb (”2,093,056″) with similar situation until the memory usage reaches around 3mb (”3,133,440″). GC doesn’t seem to be able to get rid of the local references to a tween instance. I came up with this conclusion because using weak references to the listeners of the tween didn’t solve the issue; so it presumably was not listener issue.

After refactoring: tween_posX becomes instance variable and GC is reused.

private var tween_posX:fl.transitions.Tween;
public function set tweenPosX(v:Number):void
{
//var duration:Number = UNIT_DIST_DURATION*Math.abs(v-posY);
var duration:Number = .5;
tween_posX = new Tween(this, “posX”, Regular.easeInOut, posX, v, duration, true);
tween_posX.addEventListener(TweenEvent.MOTION_FINISH, tweenPosXFinish);
}

Links:

Kirupa: ActionScript 3 Tip of the day Page 9

gskinner: Weakly referenced listeners

[Added Sept 03, 07]

Someone on FlashMedia maillist had this one:
Never Ever Use Tween or URLLoader or Loader or Timer or…