RSS link icon

.

Make simple functions with AS3


In this Flash tutorial you will see how to build your own function with actionscript 3.0.

I know people are saying AS3 is more complicated then the pervious versions, but I bet when you get to understand the basics you will like it even more.

What we will be making here is a very simple function that is called when the movie clip starts, the only content is a movie clip with a smiley graphic.

First we will need to import our smiley, so go to file -> import -> import to stage and locate the smiley on your computer.

Now right click on the smiley and choose convert to movie clip as shown below.

simple functino in flash

Go to the properties panel and give the movie clip an instance name, I just named mine smile.

simple functino in flash

Now just click somewhere on the stage so you have nothing selected and hit F9 to get to the actionscript panel and type in the following code.

function place_smiley():void
{
    smile.x = Math.random() * 100 
    smile.y = Math.random() * 100
}
place_smiley();

The first line is declaring the function, and naming it "place_smiley"

The next two lines is telling what our function will do, in this case it will place our smiley somewhere on the stage with a random x and y axis.

The last line is calling the function when the flash movie starts.

So now try to start your movie and test if its all working.


Dave says: 2008-03-20

Ignore my comment I simply had to increase the value of 100. Pretty obvious, I should have worked that out quicker but I didnChr(34)t know how Math.random worked then

Dave says: 2008-03-20

thanks a lot, these short basic tutorials are what IChr(34)m looking for at the moment. one problem though: It only ever places my object in random places right on the left hand margin of my page

tamm says: 2008-02-29

sick bro, thanks

Admin Bob says: 2008-02-27

Hi Vince, IChr(34)m not sure what makes that error right now, but try to mail me the flash project file and I will take a look at it. support@dd-soft.co

Vince says: 2008-02-24

keep getting error message Error Scene=Scene 1, layer=Layer 1, frame=1:Line 1: A type identifier is expected after the Chr(34):Chr(34). function place_eye():void Total ActionScript Errors: 1 Reported Errors: 1 this is my code using a picture of an eye function place_eye():void { eye.x = Math.random() 100 eye.y = Math.random() 100 } place_eye();

Vince says: 2008-02-24

keep getting error message Error Scene=Scene 1, layer=Layer 1, frame=1:Line 1: A type identifier is expected after the Chr(34):Chr(34). function place_eye():void Total ActionScript Errors: 1 Reported Errors: 1 this is my code using a picture of an eye function place_eye():void { eye.x = Math.random() 100 eye.y = Math.random() 100 } place_eye();

Admin Bob says: 2008-02-20

Hi Edda, Tell me what error message flash gives you, I might be able to help you.

Edda says: 2008-02-20

It does not work

Marina says: 2008-02-02

Thanks)