Category: ActionScript

Simulated 3D Scaling With ActionScript

3 March, 2008 | ActionScript | No comments

The current version of Flash (8 at the time of writing) isn’t capable of real 3D graphics, so to create a simple fly by effect you need to use the _xscale and _yscale properties of a movie clip.

To do this create a movie clip and attach the following ActionScript code to the movie clip on the stage.

onClipEvent(load){
 x = 525;
 y = 25;
}
 
onClipEvent(enterFrame){
 this._xscale++;
 this._yscale++;
 this._x -= 5;
 this._y += 5;
}

When you run the movie you will see your shape move towards the bottom left hand corner and also appear to get bigger. This effect is best used on non-abstract shapes as it can just look like your cube gets bigger. Adding a vector image to your movie clip will give the best results.

Create A Simple Flash Presentation

28 February, 2008 | ActionScript | No comments

One common thing that can be done with Flash is to create presentations. These presentations can then be exported to a swf file and viewed by just about everybody.

To make a presentation create a new flash document and create a new layer. This layer will be used to keep the next and previous buttons on it.

On the first frame add the following code to stop the animation running at the first frame.
stop();

Next, create a button that you can use for moving forward and backward through the presentation. This can be done by drawing an object on the stage, right clicking on it and selecting Convert to Symbol…. You can then add these buttons to the stage and create events for them.

For the previous button a function exists in ActionScript called prevFrame(). So all you need to do is attach that function call to the release event (ie. after a user has clicked on it) on that button.
on(release){
 prevFrame();
}

The next button is much the same, except that it uses the function nextFrame().

on(release){
 nextFrame();
}

It is also possible to create a menu slide that will click through to some of the main frames in the presentation. To do this you can use the same button object that you have used before and use the following code to fast forward to a particular slide.

on(release){
 gotoAndStop(25);
}

This works if you don’t add any more frames before that frame as it will change the number of the slide. A better way is to use frame labels, which can be set in the properties section for every key frame.

on(release){
 gotoAndStop("chapter3");
}

This will always move to the frame labelled chapter3, even if you add more frames before it.

The most recent version of Flash comes with the option of creating a presentation when you create a new file. This creates a project with some basic functionality. For example, pressing the left and right arrows on the keyboard will move the presentation through the slides.

ActionScript Bouncing Ball

27 February, 2008 | ActionScript | No comments

To create a ball that bounces around the edges of the window you need to use the enterFrame event. You will need to create a movie clip on the stage in exactly the same way as the ActionScript follow mouse example. Remove the code that is present in the movie clip object and put this code in its place.

onClipEvent(load){
 speedX = 5;
 speedY = 5;
}
 
onClipEvent(enterFrame){
  this._x += speedX;
  this._y += speedY;
 
  if(this._x >= 550){
   speedX = -speedX;
 }else if(this._x <= 0){
   speedX = -speedX;
 }
 
  if(this._y >= 400){
   speedY = -speedY;
 }else if(this._y <= 0){
   speedY = -speedY;
 }
}

The first part of the script sets up two variables (speedY and speedX) when the flash program loads. The rest of the script keeps the ball from going outside the confines of the frame.

ActionScript Follow Mouse

26 February, 2008 | ActionScript | 1 comment

Creating an Movie Clip that follows the mouse around the screen is the initial step in making your own cursors in Flash. First off, create an empty flash file and draw an object on the stage. Right click on this object and convert it into a Movie Clip by clicking on the item Convert to Symbol… as in the following screenshot.

Convert object to a Movie Clip by right clicking on the object and selecting Convert to Symbol...

You will then see the following dialog box on screen.

The Convert to Symbol dialog box

Make sure Movie Clip is selected and click ok. Additionally, you can also select the registration mark to be different from the default of top left.

Click on the object on the stage again and you enter the following code in the ActionScript window. This keeps the object in the same geographical coordinates as the mouse pointer.

onClipEvent(enterFrame){
 this._x = _root._xmouse;
 this._y = _root._ymouse;
}

You should now see something that looks like the following screenshot.

Entering the onClipEvent enter frame event.

Go to Control->Test Movie or press Ctrl+Enter to test the movie. When you move your mouse pointer over the movie window the object you created should follow it. It won’t follow the mouse once it has left the edge of the window because the event we created only looks at the stage area.