Flash Tutorials - Herong's Tutorial Examples - v3.03, by Dr. Herong Yang
Space Explorer v.4 - Space Car Stabilized
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 between walls with a stable speed!
If in run Space Explorer Version 3 long enough, you will notice that the space car is running faster and faster slowly. I guess that this is caused by the Math.random() function not generating true random numbers between 0.0 and 1.0. It actually generates more numbers in the 0.5 and 1.0 range causing car speed higher and higher.
For Version 4, space_explorer_4.sc, I am trying to put a speed limit to the space car:
#- space_explorer_4.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.bbox_size = 400; _root.car_size = 20; _root.car_radius = 6; _root.speed_limit = 3.0; _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; .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); // build speed limits if (_root.x_speed > _root.speed_limit) { _root.x_speed = _root.speed_limit; _root.x_acc_factor = - _root.x_acc_factor; } if (_root.y_speed > _root.speed_limit) { _root.y_speed = _root.speed_limit; _root.y_acc_factor = - _root.y_acc_factor; } if (_root.x_speed < - _root.speed_limit) { _root.x_speed = - _root.speed_limit; _root.x_acc_factor = - _root.x_acc_factor; } if (_root.y_speed < - _root.speed_limit) { _root.y_speed = - _root.speed_limit; _root.y_acc_factor = - _root.y_acc_factor; } _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; .end .frame 3 .action: gotoFrame(1); // index=0 is for frame 1 play(); .end .end
Note that the acceleration factor changes sign when speed limit is reached. This change makes the acceleration to be applied in the reverse direction on the next fame. I hope this will help to stabilize the speed.
Compile space_explorer_4.sc and generate the HTML code:
C:\herong>\local\SWFTools\swfc.exe space_explorer_4.sc -o space_explorer_4.swf C:\herong>\local\SWFTools\swfdump.exe -E space_explorer_4.swf > space_explorer_4.html
Open space_explorer_4.html in a Web browser. You will see that my space car is between boundaries with a stable speed!
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
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
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