Flash9:ActionScript 2.0:Import and visibility

From virtualMV2015wiki
Jump to: navigation, search
 Home  Flash11 (CS5) | Flash10 (CS4) | Flash9 (CS3) <
Icon Objective.png


By the end of this exercises you should be able to:

  • Importing an image
  • Changing to a Movie clip and giving a name
  • Adding a button
  • Using the if statement and the visible command to make the image disappear and appear.

Create a new Flash movie and add assets

1. Create a new Flash File (ActionScript 2.0)

2. Copy the following files as Btn1.png and Btn2.png to the folder you are creating the movie in (right click and save the images)

Btn1.png Btn2.png

3. Use File > Import > Import to Library, and import both Btn1.png and Btn2.png. In the Library will be placed both the image (png) and a Symbol

Add assets to stage

4. Drag both symbols to the stage, and change the type from Graphic to Movie Clip. This will allow you to name the instances.

5. Under the Movie Clip text box Name the instances as mcBtn1 and mcBtn2

FlashCS3 AS2 images01.png

6. Add a button from the Window > Common Libraries > Buttons selection and drag onto the stage (I have used Buttons tube double | tube double blue)

Add Action Script

7. Finally add this ActionScript to the button, (Click on the button and press F9, then in ActionScript 1.0 & 2.0 > Movie Clip Control > on ...)

on (release) {
	if ( mcBtn1._visible == true ) {
	  mcBtn1._visible = false;
	  mcBtn2._visible = true; 
        else {
	  mcBtn2._visible = false;
	  mcBtn1._visible = true;

Trouble shooting

  1. Make sure the if and else are LOWERCASE
  2. Check that the images are Movie clips
  3. check that the names given to the instances are exactly the same as those in the code (includes upper and lower case letters)

Making an object Visible

Keeping track of the number of times an event occurs, and then when the required number of actions have taken place, making an object visible adds another dimension. For example, we have a series of aircraft components, wings, wheels etc that need to be dragged and dropped on to the correct plane. Once the correct fuselage, wheels and wings are all lined up, the aircraft changes into a coloured version of its former monotone self.

Here's the code:

One frame one of the movie, you will need to set the initial visibility state of the coloured object, plus a variable for keeping count. In this example there are six different aircraft requiring construction:

//initialize the counting
var CamelLocked:Number = 0;
var Dr1Locked:Number = 0;
var SpitfireLocked:Number = 0;
var Me109Locked:Number = 0;
var F22Locked:Number = 0;
var MiG29Locked:Number = 0;
//set the initial visibility of all coloured aircraft
DR1_Color._visible = false;
Camel_Color._visible = false;
Spitfire_Color._visible = false;
Me109_Color._visible = false;
F22_Color._visible = false;
MiG29_Color._visible = false;

For each aircraft wing and wheels that need to be dragged and dropped and locked into place, use the following code, changing the instance naming as required:

on (press) 
	if (!this.isLocked)
on (release) {
if(this._droptarget =="/MiG29_Wheels_Target")
	this._x = _root.MiG29_Wheels_Target._x;
	this._y = _root.MiG29_Wheels_Target._y;
	_root.MiG29Locked = _root.MiG29Locked +1;
	var isLocked = true;
	if(_root.MiG29Locked == 2)
	_root.MiG29_Color._visible = true;
	_root.MiG29_Wheels_MC._visible = false;
	_root.MiG29_Wings_MC._visible = false;
	_root.MiG29_Fuselage_MC._visible = false;

Ok, so what we have here is:

  • Actionscript to drag and drop
  • if statement to check to condition of the dropped component and if it matches the target zone
  • Increment the counter
  • If the counter == 2 (ie two pieces dropped and locked) the coloured object will now become visible, and all others visibility set to false.

Copy and paste the Actionscript onto each draggable object, change the naming of the object and targets to match.

Sd hottipicon.png HOT TIP! Dont forget to double check all the naming for case or missing bits - usually the cause of any problems with this sort of exercise and watch things like forgetting the underscore when referring to root objects and visibility (Very Important!)

Icon References.png References

Flash9:ActionScript 2.0:Import and visibility. (2017). In virtualMV's ( Michael Verhaart ) wiki. Retrieved December 12, 2017, from http://www.virtualmv.com/wiki/index.php?title=Flash9%3AActionScript_2.0%3AImport_and_visibility    (zotero)