Space Explorer v.2 - Space Car Wondering

This section provides a tutorial example on how to use ActionScript 3 statement blocks in SWFC script file to build my Space Explorer - Version 2: the space car is wondering randomly!

In Space Explorer Version 1, the space car is running in straight line.

For Version 2, space_explorer_1.sc, I am trying to change the speed randomly to make the space car wondering around instead of running straight:

#- space_explorer_2.sc
#- Copyright (c) 2014 by Dr. Herong Yang, herongyang.com
#
.flash bbox=400x400 background=#000044 fps=12
.circle space_car r=6 line=8 color=#cccccc fill=#ff0000

.frame 1
   .put space_car
   .action:
      _root.x_acc_factor = 0.5;
      _root.y_acc_factor = 0.5;

      _root.x_pos = 6;
      _root.y_pos = 6;
      _root.x_speed = 1.0;
      _root.y_speed = 0.5;

      _x = _root.x_pos;
      _y = _root.y_pos;
   .end

.frame 2
   .action:
      _root.x_speed += _root.x_acc_factor*(Math.random()-0.50);
      _root.y_speed += _root.y_acc_factor*(Math.random()-0.50);

      _root.x_pos += _root.x_speed;
      _root.y_pos += _root.y_speed;

      _x = _root.x_pos;
      _y = _root.y_pos;
   .end
   
.frame 3
   .action:
      gotoFrame(1); // index=0 is for frame 1
      play();
   .end

.end

Note that the random function, Math.random(), returns a random number in the range of 0.0 and 1.0.

Compile space_explorer_2.sc and generate the HTML code:

C:\herong>\local\SWFTools\swfc.exe space_explorer_2.sc 
   -o space_explorer_2.swf

C:\herong>\local\SWFTools\swfdump.exe -E space_explorer_2.swf
   > space_explorer_2.html

Open space_explorer_2.html in a Web browser. You will see that my space car is wondering around in the space nicely!

If you are reading the Web version of this book, you will see the Flash running here. Otherwise, you will see a static image.

Table of Contents

 About This Book

 Introduction of Adobe Flash

 Adobe Flash Player Plugin for Firefox

 Adobe Flash Player Plugin for Chrome

 Adobe Flash Player Plugin for Safari

 Adobe Flash Player ActiveX for IE

 Using "object" Elements for Flash Files

 Using "embed" Elements for Flash Files

 "mp3player" - MP3 Music Player

 SWFObject - Hidding "object" behind JavaScript

 Flash Player Projector

 SWFTools - SWF File Manipulation Tools

 SWFC Script to Generate Flash SWF Files

ActionScript Embedded in SWFC Script

 Using ActionScript in SWFC Scripts

 Space Explorer v.1 - Space Car Running

Space Explorer v.2 - Space Car Wondering

 Space Explorer v.3 - Space Car Bouncing

 Space Explorer v.4 - Space Car Stabilized

 AS3Compile - ActionScript 3 Compiler

 Adobe Flex SDK 4

 SWF File Structure and Tags

 SWF File Processing Rules

 SWF Files for Video and Audio Streams

 Outdated Tutorials

 References

 Full Version in PDF/EPUB