Accessibility

Table of Contents

Converting Flash Content to Flash Lite 1.1

Creating content with ActionScript 2.0

Let's create a project (see Figure 2) with the three elements I mentioned previously: Make the shooting-bullet part as a frame action and the remaining two elements—devil and bullets—as classes (see Figure 3).

Flash authoring environment showing the project

Figure 2. Flash authoring environment showing the project

Project components

Figure 3. Project components

Shooting-bullet frame action

For the frame action, declare the position and direction of six bullets as an array. Using the shot() function that is registered in setInterval, generate a movie clip called bulletArea to fire six bullets every 1.5 seconds:

////////////////////////////////////////////////////////////////////////////////
// bullet data
////////////////////////////////////////////////////////////////////////////////
bulletData = new Array();
bulletData.push({x:20, 	y:0, xmov:1.5, 	ymov:4});
bulletData.push({x:-20, y:0, xmov:-1.5, ymov:4});
bulletData.push({x:30, 	y:0, xmov:2, 	ymov:3.5});
bulletData.push({x:-30, y:0, xmov:-2, 	ymov:3.5});
bulletData.push({x:10, 	y:0, xmov:0, 	ymov:4.5});
bulletData.push({x:-10, y:0, xmov:0, 	ymov:4.5});
// the overall speed of the bullet goes up
speedRat = 2;
// generate a bullet area movie clip
this.createEmptyMovieClip("bulletArea", 1000);
////////////////////////////////////////////////////////////////////////////////
// shoot a bullet
////////////////////////////////////////////////////////////////////////////////
function shot() {
   for (var prop in bulletData) {
      var depth = bulletArea.getNextHighestDepth();
      var m = bulletArea.attachMovie("Bullet", "bullet"+depth, depth);
      m._x = devil._x+bulletData[prop].x;
      m._y = devil._y+bulletData[prop].y+30;
      m.setMove(bulletData[prop].xmov*speedRat, bulletData[prop].ymov*speedRat);
   }
}
// shoot every 1.5 seconds
setInterval(shot, 1500);

Because the devil resides on the Stage, but the bullets don't, you need to generate them using an attachMovie method. Receive a returned object as a local variable, m, and then initialize its position and run the setMove method to initialize the position. When setMove is executed, each bullet moves toward its set direction and fades off the Stage using a removeMovieClip.

For more information about this, see the section "Bullet Class" next.

Devil class (Devil.as)

Create the Devil class (below) and set its linkage properties (see Figure 4):

////////////////////////////////////////////////////////////////////////////////
// Devil class
////////////////////////////////////////////////////////////////////////////////
class Devil extends MovieClip {
   var endPosX:Number;
   var endPosY:Number;
   // constructor function
   function Devil() {
      init();
      move();
   }
   // handle movement
   function move() {
      this.onEnterFrame = function() {
         var xdis = endPosX-this._x;
         var ydis = endPosY-this._y;
         var dis = Math.sqrt(xdis*xdis+ydis*ydis);
         var xmov = xdis*0.05;
         var ymov = ydis*0.05;
         this._x += xmov;
         this._y += ymov;
         if (dis<5) {
            init();
         }
      };
   }
   // specify position
   function init() {
      endPosX = random(160)+40;
      endPosY = random(100)+40;
   }
}

Specifying a Devil movie clip class

Figure 4. Specifying a Devil movie clip class

Creating the Devil class is so simple, you don't even have to create it as a class. It would be much easier instead to create a movie clip and then put onEnterFrame() within it.

Bullet class (Bullet.as)

Now create the Bullet class (below) and set its linkage properties (see Figure 5):

////////////////////////////////////////////////////////////////////////////////
// Bullet class
////////////////////////////////////////////////////////////////////////////////
class Bullet extends MovieClip
{
   var xmov:Number;
   var ymov:Number;
   // constructor function
   function Bullet ()
   {
   }
   // handle movement
   function setMove (x:Number, y:Number)
   {
      xmov = x;
      ymov = y;
      this.onEnterFrame = function ()
      {
         _x += xmov;
         _y += ymov;
         // Remove in the case of veering off the screen
         if(_x<0 || _x>Stage.width || _y<0 || _y>Stage.height)
         {
            this.removeMovieClip();
         }
      };
   }
}

Specifying a Bullet movie clip class

Figure 5. Specifying a Bullet movie clip class

Now you can view the results (see Figure 6).

Content created with ActionScript 2.0

Figure 6. Content created with ActionScript 2.0

Next I'll show you how to create this same content using Flash Lite 1.1.