Archive for the 'Flash' Category

16
Feb

Word Count

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Returns the number of words in a string.

function wordcount(string:String):Number {
   var wrdArray:Array = string.split(" ");
   for (var i = wrdArray.length; i>0; i--) {
      if (wrdArray[i] == "") {
         wrdArray.splice(i,1);
      }
   }
   return wrdArray.length;
}
trace(wordcount("This will count the number of words"));

Found this useful? How about buying me a coffee - simply click here.

15
Feb

FileReference onComplete is not fired on Mac OS / 404 error / IO error

A While ago we had an interesting problem with file flash uploaders on Macs. It turns out it doesn’t work. But we have found the way. So here is how we solved the undocumented feature
1. Firstly ensure that you explicitly load the cross domain policy file.

2. Make sure you escape all the characters, Macs hate spaces.

3. And the most important you need to send empty response from upload server-side script.

In php, you would just do
echo (”");

after file-upload code. In ASP, you can do
Response.Write (”").

In Java servlet, you can do:
response.getWriter().println(”");

response.getWriter().flush();

That’s all, now onComplete event should trigger on Mac’s Adobe Flash Player 8.

But if sending “” dosent work try “ “ or “1” – we found that sending 1 was the best option.

Hope this shines some light on a frustrating problem

Found this useful? How about buying me a coffee - simply click here.

11
Feb

Mouse Event - up

In AS3 mouse event a handled differently than in AS2. Here is the general procedure for adding a mouse up event to a object.

Mouse Event - up

// you will need a movieclip on the stage with an instance name of cir_mc
// or just download the file above (www.flashcs.org)
cir_mc.addEventListener(MouseEvent.MOUSE_UP, clickListner);
function clickListner(ev:MouseEvent):void{
trace(”Mouse Up”);
}

Found this useful? How about buying me a coffee - simply click here.

08
Feb

Introduction to Flash’s Drawing API

Using Actionscript you are able to draw lines in a movie. This is achieved by a bunch of drawing methods attached to the Movie Clip Class.

What we are going to do today:
This is the first installment of a series of flash drawing tutorials. Today we are just going to have a brief introduction and overview to the drawing methods in the Movie Clip Class. We will create a basic drawing app that will let you draw on the stage at run time.

In future tutorials we will explore into colors, gradients, and much more

Introduction to the methods that we will use today:

lineStyle()
Before drawing any lines in a movieclip you must set a linestyle for that movieclip

Sintax

my_mc.lineStyle(thickness, color, alpha)

moveTo()
All movieclips hold a drawing position/coordinate where the line will start. When a movieclip is instance is created on stage the drawing position is set to 0,0 but moveTo() can change the drawing position.

Syntax

my_mc.moveTo(100,100)

lineTo()
This is the one that will do most of the work for us today. This class draws a line from the moviesclips drawing position, in the lineStyle to the position in the method

Syntax

my_mc.lineTo(200,200)

Lets Create the App.

In this app when you click and drag on the stage a line will be drawn, just line in Photoshop or any drawing application. In future tuts we will add color, brushes, and fill shapes, clear stages and event attempt to save our image to a jpg. But today we will just get the basics sorted.

Step 1
First thing first we need to detect when the mouse is down/pressed. So open a new flash doc and add the following code to the first frame of the main timeline

stop();
var isDown:Boolean = false;

onMouseDown = function(){
isDown = true
}

onMouseUp = function(){
isDown = false
}

This code will allow us to tell is the mouse is down or up. As if it is down we will be drawing on the stage

Step 2
We will create a canvas for us to draw on which is just a movieclip. You also draw straight onto the root of the stage but I prefer the movieclip way.

Add the code in bold

stop();

var isDown:Boolean = false;
var mcCanvas:MovieClip = this.createEmptyMovieClip(”canvas_mc”, this.getNextHighestDepth())

onMouseDown = function(){
isDown = true
}

onMouseUp = function(){
isDown = false
}

Now we have a moveclip called canvas_mc that we can draw on.

Step 3
Now that we have the canvas to draw on we need to adjusts its drawing cords to the mouse position every time the mouse is pressed

Add the code in bold

stop();

var isDown:Boolean = false;
var mcCanvas:MovieClip = this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth())

onMouseDown = function(){
isDown = true
mcCanvas.moveTo(_xmouse,_ymouse)
}

onMouseUp = function(){
isDown = false
}

Step 4
Now to the part that will actually make this app draw. We are going to use onMouseMove to draw on the canvas, you could also use onEnterFrame.

Add the code in bold

stop();

var isDown:Boolean = false;
var mcCanvas:MovieClip = this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth())

onMouseDown = function(){
isDown = true
mcCanvas.moveTo(_xmouse,_ymouse)
}

onMouseUp = function(){
isDown = false
}

onMouseMove = function(){
if(isDown){
mcCanvas.lineStyle(2,0×000000,100)
mcCanvas.lineTo(_xmouse,_ymouse)
}
}

When the mouse moves we test to see if the mouse is down and if it is we when set the line style and draw the line.

Test the move and have a play

That’s it… simple

Have a play with colors and brush sizes. Also see if you can create a button that will clear all the lines drawn…
Tip you don’t need to remove or remake the canvas. Have a look in the help

Found this useful? How about buying me a coffee - simply click here.

06
Feb

Convert degrees to radians & radians to degrees

These are two useful formulas that you will use more often than not.

degrees to radians

var degrees:Number =  radians * 180 / Math.PI

radians to degrees

var radians:Number = degrees * Math.PI / 180

Found this useful? How about buying me a coffee - simply click here.

05
Feb

removeMovieClip() dosent work

So it would seem that when you publish your movies in flash, removeMovieClip() doesn’t seem to work properly or at all.

This is because you are more than likely using getNextHighestDepth() which is supposably perfectly fine. But in certain cases such as using v2.0 components or buttons this will cause problems. The reason is that when you use components the Flash DepthManager class automatically reserves the highest upper and lower depths available to Flash (-16383,1048575). So if you have a component in your fla and you call getNextHighestDepth() it will return 1048576 – this is now an out of range depth for removeMovieClip() making it unable to remove the clip. Also the call will fail silently (making it hard to debug)

 

Work Around Provide by macromedia

Use unLoadMovie() instead ofremoveMovieClip(). – I still have problems, but it might work for you.

Swap the depth of the movie clip to a level at or below 1048575 before doing removeMovieClip() – With a complicated movie can get interesting to sort sometimes

 

The easiest way I have found is to manage the depths manually, this approach is extremely easy and doesn’t cause any issues

var nDepth:Number = 100
var createEmptyMovieClip(“myClip_mc”, nDepth++);

Hope this shines some light on what can be an extremely frustrating problem.

 

Read some more about it here
http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_19435

Found this useful? How about buying me a coffee - simply click here.

04
Feb

Should I use scenes

I got asked the other day why don’t we use scenes?

Before getting into using scenes read below, this is a hotly contested area but is suggested best practice by adobe to steer away from scenes. Read below for adobes official thoughts and my own on scenes in flash…

Using Scenes
Using scenes is similar to using several SWF files together to create a larger presentation. Each scene has a timeline. When the playhead reaches the final frame of a scene, the playhead progresses to the next scene. When you publish a SWF file, the timeline of each scene combines into a single timeline in the SWF file. After the SWF file compiles, it behaves as if you created the FLA file using one scene. Because of this behavior, avoid using scenes for the following reasons:

  • Scenes can make documents confusing to edit, particularly in multiauthor environments. Anyone using the FLA document might have to search several scenes within a FLA file to locate code and assets. Consider loading content or using movie clips instead.
  • Scenes often result in large SWF files. Using scenes encourages you to place more content in a single FLA file and hence, larger documents to work with and larger SWF files.
  • Scenes force users to progressively download the entire SWF file, even if they do not plan or want to watch all of it. Your user progressively downloads the entire file, instead of loading the assets they actually want to see or use. If you avoid scenes, the user can control what content they download as they progress through your SWF file. This means that the user has more control over how much content they download, which is better for bandwidth management. One drawback is the requirement for managing a greater number of FLA documents.
  • Scenes combined with ActionScript might produce unexpected results. Because each scene timeline is compressed onto a single timeline, you might encounter errors involving your ActionScript and scenes, which typically requires extra, complicated debugging.

There are some situations where few of these disadvantages apply, such as when you create lengthy animations. If you create lengthy animations, you might find it adventageous to use scenes. If disadvantages apply to your document, consider using multiple FLA files, movie clips, or screens to build an animation instead of using scenes. For more information on using screens, see Working with Screens in Flash Help or the Flash 8 LiveDocs (Using Flash > Working with Screens).

Basically I think scenes are the devils spawn

Instead I would suggest that you break your file into manageable flash files (chucks) e.g make separate swf files. This will make it easier to create, edit, code and in end manage.

Hope this helps clear some things up

Found this useful? How about buying me a coffee - simply click here.




Close
E-mail It