SWFAction

(PHP 4 >= 4.0.5)

SWFAction -- Creates a new Action

Description

SWFAction swfaction ( string script )

警告

本函数是实验性的。本函数的行为,包括函数名称以及其它任何关于本函数的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。使用本函数风险自担。

swfaction() creates a new Action, and compiles the given script into an SWFAction object.

The script syntax is based on the C language, but with a lot taken out- the SWF bytecode machine is just too simpleminded to do a lot of things we might like. For instance, we can't implement function calls without a tremendous amount of hackery because the jump bytecode has a hardcoded offset value. No pushing your calling address to the stack and returning- every function would have to know exactly where to return to.

So what's left? The compiler recognises the following tokens:

  • break

  • for

  • continue

  • if

  • else

  • do

  • while

There is no typed data; all values in the SWF action machine are stored as strings. The following functions can be used in expressions:

time()

Returns the number of milliseconds (?) elapsed since the movie started.

random(seed)

Returns a pseudo-random number in the range 0-seed.

length(expr)

Returns the length of the given expression.

int(number)

Returns the given number rounded down to the nearest integer.

concat(expr, expr)

Returns the concatenation of the given expressions.

ord(expr)

Returns the ASCII code for the given character

chr(num)

Returns the character for the given ASCII code

substr(string, location, length)

Returns the substring of length length at location location of the given string string.

Additionally, the following commands may be used:

duplicateClip(clip, name, depth)

Duplicate the named movie clip (aka sprite). The new movie clip has name name and is at depth depth.

removeClip(expr)

Removes the named movie clip.

trace(expr)

Write the given expression to the trace log. Doubtful that the browser plugin does anything with this.

startDrag(target, lock, [left, top, right, bottom])

Start dragging the movie clip target. The lock argument indicates whether to lock the mouse (?)- use 0 (FALSE) or 1 (TRUE). Optional parameters define a bounding area for the dragging.

stopDrag()

Stop dragging my heart around. And this movie clip, too.

callFrame(expr)

Call the named frame as a function.

getURL(url, target, [method])

Load the given URL into the named target. The target argument corresponds to HTML document targets (such as "_top" or "_blank"). The optional method argument can be POST or GET if you want to submit variables back to the server.

loadMovie(url, target)

Load the given URL into the named target. The target argument can be a frame name (I think), or one of the magical values "_level0" (replaces current movie) or "_level1" (loads new movie on top of current movie).

nextFrame()

Go to the next frame.

prevFrame()

Go to the last (or, rather, previous) frame.

play()

Start playing the movie.

stop()

Stop playing the movie.

toggleQuality()

Toggle between high and low quality.

stopSounds()

Stop playing all sounds.

gotoFrame(num)

Go to frame number num. Frame numbers start at 0.

gotoFrame(name)

Go to the frame named name. Which does a lot of good, since I haven't added frame labels yet.

setTarget(expr)

Sets the context for action. Or so they say- I really have no idea what this does.

And there's one weird extra thing. The expression frameLoaded(num) can be used in if statements and while loops to check if the given frame number has been loaded yet. Well, it's supposed to, anyway, but I've never tested it and I seriously doubt it actually works. You can just use /:framesLoaded instead.

Movie clips (all together now- aka sprites) have properties. You can read all of them (or can you?), you can set some of them, and here they are:

  • x

  • y

  • xScale

  • yScale

  • currentFrame - (read-only)

  • totalFrames - (read-only)

  • alpha - transparency level

  • visible - 1=on, 0=off (?)

  • width - (read-only)

  • height - (read-only)

  • rotation

  • target - (read-only) (???)

  • framesLoaded - (read-only)

  • name

  • dropTarget - (read-only) (???)

  • url - (read-only) (???)

  • highQuality - 1=high, 0=low (?)

  • focusRect - (???)

  • soundBufTime - (???)

So, setting a sprite's x position is as simple as /box.x = 100;. Why the slash in front of the box, though? That's how flash keeps track of the sprites in the movie, just like a Unix filesystem- here it shows that box is at the top level. If the sprite named box had another sprite named biff inside of it, you'd set its x position with /box/biff.x = 100;. At least, I think so; correct me if I'm wrong here.

This simple example will move the red square across the window.

例子 1. swfaction() example

<?php
  $s
= new SWFShape();
  
$f = $s->addFill(0xff, 0, 0);
  
$s->setRightFill($f);

  
$s->movePenTo(-500, -500);
  
$s->drawLineTo(500, -500);
  
$s->drawLineTo(500, 500);
  
$s->drawLineTo(-500, 500);
  
$s->drawLineTo(-500, -500);

  
$p = new SWFSprite();
  
$i = $p->add($s);
  
$i->setDepth(1);
  
$p->nextFrame();

  for (
$n=0; $n<5; ++$n) {
    
$i->rotate(-15);
    
$p->nextFrame();
  }

  
$m = new SWFMovie();
  
$m->setBackground(0xff, 0xff, 0xff);
  
$m->setDimension(6000, 4000);

  
$i = $m->add($p);
  
$i->setDepth(1);
  
$i->moveTo(-500,2000);
  
$i->setName("box");

  
$m->add(new SWFAction("/box.x += 3;"));
  
$m->nextFrame();
  
$m->add(new SWFAction("gotoFrame(0); play();"));
  
$m->nextFrame();

  
header('Content-type: application/x-shockwave-flash');
  
$m->output();
?>

This simple example tracks down your mouse on the screen.

例子 2. swfaction() example

<?php

  $m
= new SWFMovie();
  
$m->setRate(36.0);
  
$m->setDimension(1200, 800);
  
$m->setBackground(0, 0, 0);

  
/* mouse tracking sprite - empty, but follows mouse so we can
     get its x and y coordinates */

  
$i = $m->add(new SWFSprite());
  
$i->setName('mouse');

  
$m->add(new SWFAction("
    startDrag('/mouse', 1); /* '1' means lock sprite to the mouse */
  "
));

  
/* might as well turn off antialiasing, since these are just squares. */

  
$m->add(new SWFAction("
    this.quality = 0;
  "
));

  
/* morphing box */
  
$r = new SWFMorph();
  
$s = $r->getShape1();

  
/* Note this is backwards from normal shapes.  No idea why. */
  
$s->setLeftFill($s->addFill(0xff, 0xff, 0xff));
  
$s->movePenTo(-40, -40);
  
$s->drawLine(80, 0);
  
$s->drawLine(0, 80);
  
$s->drawLine(-80, 0);
  
$s->drawLine(0, -80);

  
$s = $r->getShape2();

  
$s->setLeftFill($s->addFill(0x00, 0x00, 0x00));
  
$s->movePenTo(-1, -1);
  
$s->drawLine(2, 0);
  
$s->drawLine(0, 2);
  
$s->drawLine(-2, 0);
  
$s->drawLine(0, -2);

  
/* sprite container for morphing box -
     this is just a timeline w/ the box morphing */

  
$box = new SWFSprite();
  
$box->add(new SWFAction("
    stop();
  "
));
  
$i = $box->add($r);

  for (
$n=0; $n<=20; ++$n) {
    
$i->setRatio($n/20);
    
$box->nextFrame();
  }

  
/* this container sprite allows us to use the same action code many times */

  
$cell = new SWFSprite();
  
$i = $cell->add($box);
  
$i->setName('box');

  
$cell->add(new SWFAction("

    setTarget('box');

    /* ...x means the x coordinate of the parent, i.e. (..).x */
    dx = (/mouse.x + random(6)-3 - ...x)/5;
    dy = (/mouse.y + random(6)-3 - ...y)/5;
    gotoFrame(int(dx*dx + dy*dy));

  "
));

  
$cell->nextFrame();
  
$cell->add(new SWFAction("

    gotoFrame(0);
    play();

  "
));

  
$cell->nextFrame();

  
/* finally, add a bunch of the cells to the movie */

  
for ($x=0; $x<12; ++$x) {
    for (
$y=0; $y<8; ++$y) {
      
$i = $m->add($cell);
      
$i->moveTo(100*$x+50, 100*$y+50);
    }
  }

  
$m->nextFrame();

  
$m->add(new SWFAction("

    gotoFrame(1);
    play();

  "
));

  
header('Content-type: application/x-shockwave-flash');
  
$m->output();
?>

Same as above, but with nice colored balls...

例子 3. swfaction() example

<?php

  $m
= new SWFMovie();
  
$m->setDimension(11000, 8000);
  
$m->setBackground(0x00, 0x00, 0x00);

  
$m->add(new SWFAction("

this.quality = 0;
/frames.visible = 0;
startDrag('/mouse', 1);

  "
));

  
// mouse tracking sprite
  
$t = new SWFSprite();
  
$i = $m->add($t);
  
$i->setName('mouse');

  
$g = new SWFGradient();
  
$g->addEntry(0, 0xff, 0xff, 0xff, 0xff);
  
$g->addEntry(0.1, 0xff, 0xff, 0xff, 0xff);
  
$g->addEntry(0.5, 0xff, 0xff, 0xff, 0x5f);
  
$g->addEntry(1.0, 0xff, 0xff, 0xff, 0);

  
// gradient shape thing
  
$s = new SWFShape();
  
$f = $s->addFill($g, SWFFILL_RADIAL_GRADIENT);
  
$f->scaleTo(0.03);
  
$s->setRightFill($f);
  
$s->movePenTo(-600, -600);
  
$s->drawLine(1200, 0);
  
$s->drawLine(0, 1200);
  
$s->drawLine(-1200, 0);
  
$s->drawLine(0, -1200);

  
// need to make this a sprite so we can multColor it
  
$p = new SWFSprite();
  
$p->add($s);
  
$p->nextFrame();

  
// put the shape in here, each frame a different color
  
$q = new SWFSprite();
  
$q->add(new SWFAction("gotoFrame(random(7)+1); stop();"));
  
$i = $q->add($p);

  
$i->multColor(1.0, 1.0, 1.0);
  
$q->nextFrame();
  
$i->multColor(1.0, 0.5, 0.5);
  
$q->nextFrame();
  
$i->multColor(1.0, 0.75, 0.5);
  
$q->nextFrame();
  
$i->multColor(1.0, 1.0, 0.5);
  
$q->nextFrame();
  
$i->multColor(0.5, 1.0, 0.5);
  
$q->nextFrame();
  
$i->multColor(0.5, 0.5, 1.0);
  
$q->nextFrame();
  
$i->multColor(1.0, 0.5, 1.0);
  
$q->nextFrame();

  
// finally, this one contains the action code
  
$p = new SWFSprite();
  
$i = $p->add($q);
  
$i->setName('frames');
  
$p->add(new SWFAction("

dx = (/:mousex-/:lastx)/3 + random(10)-5;
dy = (/:mousey-/:lasty)/3;
x = /:mousex;
y = /:mousey;
alpha = 100;

  "
));
  
$p->nextFrame();

  
$p->add(new SWFAction("

this.x = x;
this.y = y;
this.alpha = alpha;
x += dx;
y += dy;
dy += 3;
alpha -= 8;

  "
));
  
$p->nextFrame();

  
$p->add(new SWFAction("prevFrame(); play();"));
  
$p->nextFrame();

  
$i = $m->add($p);
  
$i->setName('frames');
  
$m->nextFrame();

  
$m->add(new SWFAction("

lastx = mousex;
lasty = mousey;
mousex = /mouse.x;
mousey = /mouse.y;

++num;

if (num == 11)
  num = 1;

removeClip('char' & num);
duplicateClip(/frames, 'char' & num, num);

  "
));

  
$m->nextFrame();
  
$m->add(new SWFAction("prevFrame(); play();"));

  
header('Content-type: application/x-shockwave-flash');
  
$m->output();
?>


add a note add a note User Contributed Notes
samrerb at gmail dot com
13-Aug-2006 12:03
for creating a play/pause button I used this script:
<?php
//pause button
 
$b = new SWFButton();
 
$b->addShape(rect(0, 0xff, 0), SWFBUTTON_HIT | SWFBUTTON_UP | SWFBUTTON_DOWN | SWFBUTTON_OVER);
 
$b->addAction(new SWFAction("stop();"),SWFBUTTON_MOUSEDOWN);
 
$i = $movie->add($b);

//play button
 
$b = new SWFButton();
 
$b->addShape(rect_two(0, 0xff, 0), SWFBUTTON_HIT | SWFBUTTON_UP | SWFBUTTON_DOWN | SWFBUTTON_OVER);
 
$b->addAction(new SWFAction("play();"),SWFBUTTON_MOUSEDOWN);
 
$i = $movie->add($b);
?>
it has to be run during every frame for the buttons to be in every frame... hope that helps somebody....
julien/*AT*/theoconcept.com
27-Jan-2006 02:41
fscommand, the proper way to call a javascript function from a flash animation seems not to work in Ming at the moment, here is a commented example on how to do that :

http://blog.theoconcept.com/flashlink.php
ifrost at uos dot de
25-Oct-2004 09:50
If you want to open an URL in a new window, define a shape ($ashape) and use this code:

<?php
$b
= new SWFButton();
$b->addShape($ashape, SWFBUTTON_HIT | SWFBUTTON_UP | SWFBUTTON_DOWN | SWFBUTTON_OVER);
$b->addAction(
   new
SWFAction(
  
'getURL("http://www.php.net","_blank");' // _blank is the target like in html
  
), SWFBUTTON_MOUSEDOWN
);
?>

But if you want it in the same window use this addAction():

<?php
$b
->addAction(
   new
SWFAction(
  
'this.getURL("http://www.php.net");'
  
), SWFBUTTON_MOUSEDOWN
);
?>
jerryscript at aol dot com
05-Jan-2004 10:28
Ming 0.3 (current cvs) can use most MX actionscript, just set the swf version to 6

ming_useswfversion(6);
Anze
04-Nov-2003 04:45
fscommand() doesn't work (at least in Ming 0.2a), but there is a workaround.
Instead of:
 fscommand("do","something");
use:
 getURL("fscommand:do","something");
jamesNOSPAMbarros at hotNOSPAMmail dot com
14-Sep-2002 02:35
Printing Flash Movies

When a browser tries to print flash, the autoscaling can make it look ugly. users have to right click on the flash and select "print" to get it to print properly. (so that thier flash player is handling the printing, not the browser) If you dont want to require this of your users, you can create a print button with the following action:

getURL('print:', '/');

by default, this prints ALL frames. to avoid this, just put:
 
$m->labelFrame("#p");
before:
$m->nextFrame();

where $m is your SWFMovie. the #p label denotes a printable frame. (this also allows you to build your movie, then throw the print button into the next frame and not have it show up when you print. )
diem at writeme dot com
06-Feb-2002 01:11
Sorry Guys ....
the /box.x syntax is for fash version 4 ... and _root.box._x is used for flash version 5 ....
Ming >= 0.2 assumes version 5 by default .... to use version 4 syntax, you must use ming_useswfversion before ...
diem at writeme dot com
01-Feb-2002 07:40
... hummm ... just /box._x won't work ... sorry .... .. I used SWFAction('setTarget("box"); _x += 3;');
Same when using _rotation, _y , and so on ...

[]'s
diem at writeme dot com
01-Feb-2002 07:16
Warning .....
To use Flash 5 properties like x,y and rotation ... you can't use /box.x as described above .. use box._x <note for underscore>. Same happens with y and rotation ...
Hope this help someone ..... :o)