PHP Velho Oeste 2024

SWFAction クラス

(PHP 5 < 5.3.0, PECL ming SVN)

はじめに

SWFAction.

クラス概要

SWFAction {
/* メソッド */
__construct ( string $script )
}

説明

スクリプトの文法は C 言語をもとにしていますが、多くの機能が 省略されています - SWF バイトコードマシンは、より単純であることを 志向しています。例えば、相当手の込んだ細工をしなければ、関数のコールを 実装することもできません。なぜなら、バイトコードの jump 命令は ハードコードされたオフセット値を使用しているからです。 呼び出し元のアドレスをスタックに格納したりはしません - すべての関数は、戻ってくる場所を正確に知っている必要があるのです。

結局、どんな機能が残っているのでしょう? コンパイラが理解できるトークンは 以下のとおりです。

  • break
  • for
  • continue
  • if
  • else
  • do
  • while

データ型は存在しません。SWF アクションマシンにおいては、すべての値は 文字列として扱われます。以下の関数が使用可能です。

time()
ムービーが開始してからの経過時間を、ミリ秒 (?) で返します。
random(seed)
0 から seed までの範囲の擬似乱数を返します。
length(expr)
指定した式の長さを返します。
int(number)
指定した数値を、一番近い整数に切り下げた値を返します。
concat(expr, expr)
指定した式を連結して返します。
ord(expr)
指定した文字の ASCII コードを返します。
chr(num)
指定した ASCII コードに対応する文字を返します。
substr(string, location, length)
指定した文字列 string の、 位置 location から始まる 長さ length の部分文字列を返します。

さらに、以下のコマンドも使用できるでしょう。

duplicateClip(clip, name, depth)
指定したムービークリップ (またの名をスプライト) clip を複製します。 新しいムービークリップの名前は name で、 深度は depth となります。
removeClip(expr)
指定したムービークリップを削除します。
trace(expr)
指定した式をトレースログに書き込みます。ブラウザのプラグインが これをきちんと扱ってくれるかは疑わしいものです。
startDrag(target, lock, [left, top, right, bottom])
ムービークリップ target のドラッグを開始します。 引数 lock で、マウスをロックするかどうか (?) を指定します - 0 (FALSE) あるいは 1 (TRUE) を指定します。 オプションのパラメータでは、ドラッグする範囲のを指定します。
stopDrag()
つかれきった心を落ち着かせます。そしてムービークリップの ドラッグも修了させます。
callFrame(expr)
指定したフレームを関数としてコールします。
getURL(url, target, [method])
指定した URL を読み込みます。引数 target は、 (たとえば "_top" や "_blank" のような) HTML ドキュメントの target に対応します。オプションの引数 method は、 サーバーに変数を返したい場合に POST あるいは GET を指定します。
loadMovie(url, target)
指定した URL を読み込みます。引数 target は、 (おそらく) フレームの名前か あるいは特別な値 "_level0" (現在のムービーを置き換える) 、 "_level1" (現在のムービーの前面に新しいムービーを表示する) のうちのいずれかです。
nextFrame()
次のフレームに移動します。
prevFrame()
直前の (あるいは一つ前の) フレームに移動します。
play()
ムービーの再生を開始します。
stop()
ムービーの再生を停止します。
toggleQuality()
高品質/低品質を切り替えます。
stopSounds()
音声の再生を停止します。
gotoFrame(num)
フレーム番号 num に移動します。 フレーム番号は 0 からはじまります。
gotoFrame(name)
name という名前のフレームに移動します。 これは便利です。というのもまだフレームのラベルを追加していないからです。
setTarget(expr)
アクションのコンテキストといわれるものを設定します。 これが何をするものなのかは実際のところよくわかりません。
そしてもうひとつ変なものがあります。if 文や while ループの中で、 指定したフレーム番号が読み込まれているかどうかを調べるために、 式 frameLoaded(num) が使用可能です。ええ。そのようにいわれています。 しかし私は実際にこれをテストしたことがなく、実際に動作するのか 疑問に思っています。かわりに /:framesLoaded を使用するとよいでしょう。

ムービークリップ (さぁみなさんご一緒に - またの名をスプライト) はプロパティをひじしています。すべてのプロパティが読み込み可能で、 そのうちのいくつかには値を設定することも可能です。プロパティの 一覧は以下のとおりです。

  • x
  • y
  • xScale
  • yScale
  • currentFrame - (読み込み専用)
  • totalFrames - (読み込み専用)
  • alpha - 透明度
  • visible - 1=on, 0=off (?)
  • width - (読み込み専用)
  • height - (読み込み専用)
  • rotation
  • target - (読み込み専用) (???)
  • framesLoaded - (読み込み専用)
  • name
  • dropTarget - (読み込み専用) (???)
  • url - (読み込み専用) (???)
  • highQuality - 1=high, 0=low (?)
  • focusRect - (???)
  • soundBufTime - (???)
ということで、スプライトの x 位置を指定するには単に /box.x = 100; とすればよいわけです。なぜ box の前のスラッシュがあるのでしょう? これは、flash がムービー内のスプライトを Unix ファイルシステム風に 管理しているからです - つまり、この場合 box がトップレベルに存在する ことになります。box という名前のスプライトがその中に biff という名前の スプライトを保持している場合、その x 位置を指定するには /box/biff.x = 100; とします。すくなくとも私はそう思っています。 もし間違っていたら指摘してください。

目次

add a note add a note

User Contributed Notes 12 notes

up
1
jon at nux dot co dot uk
15 years ago
A little story that may help some others - part 2

and this didn't work... why not.. the trace gave me a clue:

Warning: createEmptyMovieClip is not a function

After a bit of digging around I found that this was because the flash movie was not running in the right version. There is a ming option that is mentioned in other posts but does not seem to be well documented and that is:

ming_useswfversion(setversion)

This makes the world of a difference as it sets what version of flash ming outputs its movies as.

So the monent I added:

ming_useswfversion(6)

To the top of my php I not only did the createEmptyMovieClip function work but so did the loading and the variable was accessible. Huruh!

So my final code now looks like this:

<?php
    ming_useswfversion
(6);
   
   
$m = new SWFMovie();
   
$m->setRate(30.000000);
   
$m->setDimension(480, 400);
   
$m->setBackground(0xff, 0xff, 0xff);

   
$m->add(new SWFAction('
        myvar = "variable to pass to flash";
        this.createEmptyMovieClip("mc", 99999);
        mc.loadMovie ("/flash_file_created_by_hand.swf");
    '
);
   
   
header('Content-type: application/x-shockwave-flash');
   
$m->output();
?>

instead of this:

<?php
    $m
= new SWFMovie();
   
$m->setRate(30.000000);
   
$m->setDimension(480, 400);
   
$m->setBackground(0xff, 0xff, 0xff);

   
$m->add(new SWFAction('
        myvar = "variable to pass to flash";
        LoadMovie("/flash_file_created_by_hand.swf", "mc");
    '
);

   
/* -- make movie clip 'mc' that we will load flash_file_created_by_hand.swf into -- */
   
$s1 = new SWFSprite();  /* (1 frames) */
   
$s1->nextFrame();  /* (end of sprite frame 0) */
   
$i1 = $m->add($s1);
   
$i1->setName('mc');
   
$m->nextFrame();  /* (end of frame 0) */

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

I dont pretend to be a flash guru.. but i know it took me a while to figure this all out.. so I thought that this post might one day be of help to someone.
up
2
diem at writeme dot com
22 years ago
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 ...
up
1
jon at nux dot co dot uk
15 years ago
A little story that may help some others - part 1

Upgrading from:

I have used ming for several years now for injecting variables into flash, usually for menus. In a recent move to a new server all the ming/php stopped working. Ming was working and properly installed but the variables just weren't apparent within the flash movie.

The old server is running:
ming-0.2a_1         LGPL'ed Flash 4/5 movie output library with many languages
php4-ming-4.4.1_1   The ming shared extension for php

The new one is running:
ming-0.3.0_3        LGPL'ed Flash 4/5 movie output library with many languages
php5-ming-5.2.6_1   The ming shared extension for php

So the way I have always done this is to put all my data together in php, and then put it into an array/variable in Flash. I then create a movie clip and load my movie.swf (made by hand in flash) into the movieclip.

I know there are now cleaner ways of getting data loaded into Flash (even directly from Flash without ming) but as I have many many movies out there using ming it is far easier to change a few lines of php to get them working again than change every flash movie.

So anyhow, here is the code:

<?php
    $m
= new SWFMovie();
   
$m->setRate(30.000000);
   
$m->setDimension(480, 400);
   
$m->setBackground(0xff, 0xff, 0xff);

   
$m->add(new SWFAction('
        myvar = "variable to pass to flash";
        LoadMovie("/flash_file_created_by_hand.swf", "mc");
    '
);

   
/* -- make movie clip 'mc' that we will load flash_file_created_by_hand.swf into -- */
   
$s1 = new SWFSprite();  /* (1 frames) */
   
$s1->nextFrame();  /* (end of sprite frame 0) */
   
$i1 = $m->add($s1);
   
$i1->setName('mc');
   
$m->nextFrame();  /* (end of frame 0) */

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

So what stopped working. Well this took me a while to work out, and even know I am not 100% sure but I can tell you what does work and how I got there.

Firstly I needed some more debugging, and so installing the flash debug player http://www.adobe.com/support/flashplayer/downloads.html and 'Flash Tracer' https://addons.mozilla.org/en-US/firefox/addon/3469 enabled me to see trace output of the ming movie and my loaded movie (flash_file_created_by_hand.swf).  If you don't already know then this is worth knowing about. I had to play around a bit to get the debug player to write its log file, but it is worth it.
Make sure you have the write settings when you publish your movie otherwise you will not be able to see the trace output.

So now I can see trace... well I used to this run a few checks to see for instance if the movie clip was there :

$s1->add(new SWFAction('
    trace ("location of movie clip is: " + this._target);
');   

and so I played around.. and I could see that the movie clip 'mc' was being created and that the movie flash_file_created_by_hand.swf was being loaded into  it but well not much else. So I still coudn't see what was happening to the data. I currently suspect that the loadMovie was loading the movie into level0 or _root rather than into /mc (_root.mc) as thus wiping out the ming vabiables.. but I can't be sure. Given that the trace of the target etc happens before the loadmovie function I think this is entirely possible.

Anyhow, I thought I may aswell try to do this a differt way.. that is create the movieclip from actionsript rather than with php/ming.. ie:

$m->add(new SWFAction('
    this.createEmptyMovieClip("mc", 999);
');

instead of:

/* -- make movie clip 'mc' that we will load flash_file_created_by_hand.swf into -- */
$s1 = new SWFSprite();  /* (1 frames) */
$s1->nextFrame();  /* (end of sprite frame 0) */
$i1 = $m->add($s1);
$i1->setName('mc');
$m->nextFrame();  /* (end of frame 0) */
up
1
PHP User
16 years ago
Typo in first example above:

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

Should be:

  $m->add(new SWFAction("box.x += 3;"));
up
1
tore dot aurstad at ntebb dot no
16 years ago
There is some difficulty adressing objects in the swfmovie using swfaction, at least when using Windows and Flash Player 9. I debugged the .swf file generated with php ming in
Adobe Flash CS3 and saw that my references on swfdisplayitems was not set correctly. I tried labelling a submovie (swfsprite object) "showmovie", but the actionscript did not seem to be able to reference the object. The debugging in Adobe Flash 9 showed the "showmovie" object to instead have the labelling "_level0.instance1", I adjusted the code and then was able to manipulate the objects in my swfmovie. The naming scheme seems to follow this "_level0.instanceX" labelling, check by debugging your .swf movies generated from php ming at least in Windows+Flash player 9 to check if swfdisplayitem's method setname also does not work here.

Tore Aurstad, Norway
up
1
samrerb at gmail dot com
17 years ago
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....
up
1
julien/*AT*/theoconcept.com
18 years ago
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
up
1
ifrost at uos dot de
19 years ago
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
);
?>
up
1
jamesNOSPAMbarros at hotNOSPAMmail dot com
21 years ago
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. )
up
0
Mark Omohundro, ajamyajax dot com
15 years ago
<?php
// tip: if you want to STOP an animation using SWFAction(),
// add a nextFrame() method immediately after, like this:

$p = new SWFSprite();
// ...
$i->rotate(15);
$p->nextFrame();
// ...
$p->add(new SWFAction("stop();"));
$p->nextFrame();  // stops right here
//(eop)

// this also seems to work, stopping a movie *exactly* where you want
// but only if not using SWFSprite() movie clips... then try the above.
$m->add(new SWFAction("stop();"));
$m->nextFrame();
//(eop)

// also: setFrames(n) doesn't seem to stop animation immediately...
// by design, or perhaps a current version/older release bug(?)
// the above was only tested with Ming 0.3beta1.  hope this helps.
?>
up
-1
jerryscript at aol dot com
20 years ago
Ming 0.3 (current cvs) can use most MX actionscript, just set the swf version to 6

ming_useswfversion(6);
up
-1
Anze
20 years ago
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");
To Top