Creating Display Objects with Action Instructions

This section provides a tutorial example showing you how to create a display object like TextField with action instructions. Flash Player supports a stack machine to perform calculations and function calls.

Flash Player also supports action instructions to create and modify display objects using function calls and stack operations. Let's learn more through another ActionScript code example:

#- ping_pong_with_caption.sc
#- Copyright (c) 2014 by Dr. Herong Yang, herongyang.com
#
.flash bbox=300x100 background=gray fps=6
.box ball color=black fill=yellow width=10 height=10

.frame 1
   .put ball x=0 y=0
   .action:
       this.n = 0;
       createTextField("caption", 0, 10, 75, 100, 20);
   .end

.frame 2
   .del ball
   .put ball x=10 y=45
   .action:
       _root.n = this.n + 1; // _root = this
       caption.text = "Loop: "+this.n;
   .end

.frame 3
   .del ball
   .put ball x=145 y=20

.frame 4
   .del ball
   .put ball x=280 y=45

.frame 5
   .action:
      gotoFrame(1); // index 1 = #2
      play();
   .end

.end

Comparing with ping_pong.sc, I have added ActionScript code in frame 1 and 2:

If we compile the code and dump the SWF file, we will see:

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

C:\herong>\local\SWFTools\swfdump --full ping_pong_with_caption.swf
[HEADER]   File version: 6
[HEADER]   File is zlib compressed. Ratio: 75%
[HEADER]   File size: 373
[HEADER]   Frame rate: 6.000000
[HEADER]   Frame count: 5
[HEADER]   Movie width: 300.00
[HEADER]   Movie height: 100.00
[009]    3 SETBACKGROUNDCOLOR (be/be/be)
[020]   33 DEFINESHAPE3 defines id 0001
            | fillstyles(01)        linestyles(01)
            | 1 ) SOLID ffff00ff    1 ) 0.00 000000ff
            |
            | fill: 01/00 line:01 - moveTo 0.00 0.00
            | fill: 01/00 line:01 - lineTo 10.00 0.00
            | fill: 01/00 line:01 - lineTo 10.00 10.00
            | fill: 01/00 line:01 - lineTo 0.00 10.00
            | fill: 01/00 line:01 - lineTo 0.00 0.00
            |
[028]    7 NAMECHARACTER adds information to id 0001 "ball"
[038]    9 EXPORTASSETS
           exports 0001 as "ball"
[01a]   10 PLACEOBJECT2 places id 0001 at depth 0001 name "ball"
[00c]   93 DOACTION
            (33 bytes) action: Constantpool(4 entries) 
           String:"this" String:"n" String:"caption" 
           String:"createTextField"
            ( 2 bytes) action: Push Lookup:0 ("this")
            ( 0 bytes) action: GetVariable
            ( 7 bytes) action: Push Lookup:1 ("n") int:0
            ( 0 bytes) action: SetMember
            (34 bytes) action: Push int:20 int:100 int:75 int:10 
           int:0 Lookup:2 ("caption") int:6 
           Lookup:3 ("createTextField")
            ( 0 bytes) action: CallFunction
            ( 0 bytes) action: Pop
            ( 0 bytes) action: End
[001]    0 SHOWFRAME 1 (00:00:00,000)
[01c]    2 REMOVEOBJECT2 removes object from depth 0001
[01a]   14 PLACEOBJECT2 places id 0001 at depth 0002 name "ball"
            | Matrix
            | 1.000 0.000  10.00
            | 0.000 1.000  45.00
[00c]   93 DOACTION
            (35 bytes) action: Constantpool(6 entries) 
           String:"_root" String:"n" String:"this" String:"caption" 
           String:"text" String:"Loop: "
            ( 2 bytes) action: Push Lookup:0 ("_root")
            ( 0 bytes) action: GetVariable
            ( 4 bytes) action: Push Lookup:1 ("n") Lookup:2 ("this")
            ( 0 bytes) action: GetVariable
            ( 2 bytes) action: Push Lookup:1 ("n")
            ( 0 bytes) action: GetMember
            ( 5 bytes) action: Push int:1
            ( 0 bytes) action: Add2
            ( 0 bytes) action: SetMember
            ( 2 bytes) action: Push Lookup:3 ("caption")
            ( 0 bytes) action: GetVariable
            ( 6 bytes) action: Push Lookup:4 ("text") 
           Lookup:5 ("Loop: ") Lookup:2 ("this")
            ( 0 bytes) action: GetVariable
            ( 2 bytes) action: Push Lookup:1 ("n")
            ( 0 bytes) action: GetMember
            ( 0 bytes) action: Add2
            ( 0 bytes) action: SetMember
            ( 0 bytes) action: End
[001]    0 SHOWFRAME 2 (00:00:00,167)
[01c]    2 REMOVEOBJECT2 removes object from depth 0002
[01a]   15 PLACEOBJECT2 places id 0001 at depth 0003 name "ball"
            | Matrix
            | 1.000 0.000 145.00
            | 0.000 1.000  20.00
[001]    0 SHOWFRAME 3 (00:00:00,333)
[01c]    2 REMOVEOBJECT2 removes object from depth 0003
[01a]   15 PLACEOBJECT2 places id 0001 at depth 0004 name "ball"
            | Matrix
            | 1.000 0.000 280.00
            | 0.000 1.000  45.00
[001]    0 SHOWFRAME 4 (00:00:00,500)
[00c]    7 DOACTION
            ( 2 bytes) action: GotoFrame 1
            ( 0 bytes) action: Play
            ( 0 bytes) action: End
[001]    0 SHOWFRAME 5 (00:00:00,667)
[000]    0 END

Okay. I see lots of more action instructions in the SWF dump output that a stack machine:

Play ping_pong_with_caption.swf. It looks better:

C:\herong>\local\bin\flashplayer_13_sa ping_pong_with_caption.swf

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

 AS3Compile - ActionScript 3 Compiler

 Adobe Flex SDK 4

 SWF File Structure and Tags

SWF File Processing Rules

 Flash Player Lifecycle - Timeline and Frame Rate

 SWF Tag Process Flow - SHOWFRAME Synchronization

 SWF Player Lifecycle Example

 Showing SWF Tag Details - "swfdump --full"

 Flash Player Supports Dynamic Actions

 Action Instruction Example - "GotoFrame"

Creating Display Objects with Action Instructions

 More Action Instructions and Less SWF Tags

 SWF Files for Video and Audio Streams

 Outdated Tutorials

 References

 Full Version in PDF/EPUB