Archive for the 'Flash CS' Category



09
Feb

removeChild and removeChildAt memory fun

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

removeChild() does not always remove the object from memory. removeChild() and removeChildAt() actually only remove the object from their parents DisplayObjectContainer. If the removed object is referenced by a variable or some such other thing the object will still persist in memory until the variables are cleared or set to null.

for example we draw a circle and attach it using the following

var cir:Shape = new Shape();
cir.graphics.lineStyle(1);
cir.graphics.beginFill(0xFF0000);
cir.graphics.drawCircle(100,100,50);
parent.addChild(cir);

now if we use the removeChild() to delete the shape

parent.removeChild(cir);

It is now longer viewable in the movie, but because it is referenced by the variable cir, the object will actually still persist in memory.

so you can re-add the shape by using

parent.addChild(cir);

If you wanted to totally remove the shape from memory you would need to change or assign a null to every var that references the shape, in our case you would do this

parent.removeChild(cir)
cir = null;

Even then it doesn’t remove the shape from memory it is now just eligible for garbage collection. (more about grabage collection another day). But for all intsive purposes it is gone.

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,0x000000,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

Drag in AS3

You can place this code in an AS3 document or a custom class, your choice. Thanks to Matt for the snippet Full code after the jump.

AS3 click and drag example
function initDragger(mc:MovieClip):void

{

 mc.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void

 {

 	e.currentTarget.startDrag();

 });

 mc.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void

 {

 	e.currentTarget.stopDrag();

 });}

// Set up drag
initDragger(cir_mc);

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




Close
E-mail It