Space Explorer v.3 - Space Car Bouncing

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

In Space Explorer Version 2, the space car will go out of the space boundary if you wait for a few minutes.

For Version 3,, I am trying to make space car bouncing back whenever it reaches a boundary:

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

.frame 1
   .put space_car
      _root.bbox_size = 400;
      _root.car_size = 20;
      _root.car_radius = 6;
      _root.x_acc_factor = 0.5;
      _root.y_acc_factor = 0.5;

      // car center position and speed
      _root.x_pos = 10;
      _root.y_pos = 10;
      _root.x_speed = 1.0;
      _root.y_speed = 0.5;

      // car center moving boundaries
      _root.x_min = 0.5*_root.car_size;
      _root.y_min = 0.5*_root.car_size;
      _root.x_max = _root.bbox_size - 0.5*_root.car_size;
      _root.y_max = _root.bbox_size - 0.5*_root.car_size;

      // top left corner is the anchor
      _x = _root.x_pos-_root.car_radius;
      _y = _root.y_pos-_root.car_radius;

.frame 2
      _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;

      // build bouncing walls
      if (_root.x_pos < _root.x_min) {
         _root.x_pos = 2*_root.x_min - _root.x_pos;
         _root.x_speed = - _root.x_speed;
      if (_root.y_pos < _root.y_min) {
         _root.y_pos = 2*_root.y_min - _root.y_pos;
         _root.y_speed = - _root.y_speed;
      if (_root.x_pos > _root.x_max) {
         _root.x_pos = 2*_root.x_max - _root.x_pos;
         _root.x_speed = - _root.x_speed;
      if (_root.y_pos > _root.y_max) {
         _root.y_pos = 2*_root.y_max - _root.y_pos;
         _root.y_speed = - _root.y_speed;

      _x = _root.x_pos-_root.car_radius;
      _y = _root.y_pos-_root.car_radius;
.frame 3
      gotoFrame(1); // index=0 is for frame 1


Note that Flash Player uses the top left corner as the anchor to put objects on the frame. But it is much easier to use the center of the circle to calculate bouncing walls.

Compile and generate the HTML code:

   -o space_explorer_3.swf

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

Open space_explorer_3.html in a Web browser. You will see that my space car is wondering around and bouncing off boundaries!

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


 Full Version in PDF/EPUB