RSS link icon

.

Create and position shapes with AS3


In this Flash actionscript tutorial I will show you how to use loops to load in and position objects on the stage, a very neat method and much better then just dragging all your movie clip objects from the library and try to place them in the right position.

 

This is what we want to accomplice, making 4 columns with 3 rows of objects, in all 12 green ghosts.

 

 

 

First we need to make the ghost, I will not show you how to do this, this is an actionscript tutorial not drawing tutorial.

 

But when you have your ghost in place in the library panel, the first and most important thing is to use linkage to tell flash that this object should be available through the actionscript code.

 

So open the library panel and right click our ghost object, then choose linkage and set it like I did in the image below, if you get any error messages just hit okay, its just flash telling you it will make a class for the object.

 

 

actionscript loops

 

 

actionscript loops

 

Now we are ready to go to the actionscript panel and do some dirty work.

 

I have typed in the description within the code starting with the comment tag // in flash so you can copy and paste it right into your flash project.

 

 

// First we define all the variables at the top of the actionscript, so we know where to change them, therefore its also important to give them good names to remember.

 

// first variable holds the value of how many objects we want.

var shapesNum:Number = 12;

 

// shapeSize holds the value of the size in pixels we want our object to be.

var shapeSize:Number = 55;

 

// Holds how many columns we want to create.

var columnsNum:Number = 4;

 

// This is the distance between the objects we create, its the space between them, we will need it later to do some simple math.

var distance:Number = 20;

 

// Here is the function wrapper, we name the function createShapes and void tells it to return no value.

function createShapes():void {

 

// some internal variables to hold the position of the objects, this is set to 0 now, but as we loop through all 12 objects, it will be changes.

var X:int = 0;

var Y:int = 0;

 

// this is the loop it basically says i = 0 and adds on each time, as long as its less then our variable shapesNum (which is 12). Meaning it will loop 12 times.

for (var i:int = 0; i < shapesNum; i++) {

 

// Now we create an instance of our shape, here we just name it shape and calls the linkage from before which we called myShape.

var shape:myShape = new myShape();

 

// This might seem a bit confusing, but I will try to explain, it says if not i (in the loop) is the same as i divided by columnsNum AND i is not equals to zero.

// Meaning in english that now we have reached the end of a column and needs to reset the X position and create a new column. and the Y position is brought down.

if (!(i%columnsNum) && i != 0) {

X = 0;

Y += shapeSize + distance;

}

 

// Here we just set the x and y position of the ghost object, saying distance (which was 20) + the shapeSize * X (X is how many shapes we have placed) + distance* X, now this gave me just a bit of scale number, but I experimented with it and found that if I added 15 to the number it fit.

// So this is a number you might want to experiment with yourself to find something that fits.

shape.x = distance+shapeSize*X+distance*X + 15

shape.y = distance+Y + 25

 

// Now we set the shape size equal to shapeSize

shape.width = shapeSize

shape.height = shapeSize

 

// Finally we add the shape to the stage using the addChild method.

addChild(shape);

 

// incrementing the X value so its ready for the next object in the loop.

X++;

}

}

 

// Last but most important thing is to make sure we run the function when flash starts by calling the function createShapes();

createShapes();

 

I hope you understood what I did in this tutorial, it might seem a bit confusing but when you learn it, its a great function and I have used it making a couple of games in the past.

 

 


babboor says: 2008-09-30

Thank you for the step by step explaation. It is tutorials (comments) like this that makes it easier for beginners like us to understand. I look forward to reading more of your detailed tutorials.