Using Action Script for Interactive Applications


Watching movies is great, but it's even better to interact with a movie. In that case, Action Script can be used.

For database developers it can be useful to collect feedback from the Flash movie. The next example counts the number of clicks of a red square.

To store the hits in the database, we add a table called hits :

 myflash=#  \d   hits  Table "hits"  Attribute            Type                               Modifier -----------+--------------------------+------------------------- -------------- ---------  id         integer                   not null default nextval('"hits_id_seq"'::text)  click      timestamp with time zone   typ        integer 

We need two files for interacting with the database. The next file is called index3.php :

 <?php      dl('php_ming.so');      # creating button ...      $b = makebutton();      # creating the movie ...      $m = new SWFMovie();      $m->setDimension(320, 240);      $m->setBackground(0xff, 0xff, 0xff);      $i = $m->add($b);      $i->moveTo(160, 120);      header('Content-type: application/x-shockwave-flash');      $m->output(); # creating button ... function makebutton() {      $b = new SWFButton();      $b->setUp(react(0xff, 30, 30));      $b->setOver(react(30, 0xff, 30));      $b->setDown(react(30, 30, 0xff));      $b->setHit(react(30, 30, 30));      $b->addAction(new SWFAction("getURL('redirect.php', '_blank', post);" ), SWFBUTTON_MOUSEUP);      return $b; } # reaction ... function react($r, $g, $b) {      global $dbh, $m;      $s = new SWFShape();      $s->setRightFill($s->addFill($r, $g, $b));      $s->movePenTo(-100,-100);      $s->drawLineTo(100,-100);      $s->drawLineTo(100,100);      $s->drawLineTo(-100,100);      $s->drawLineTo(-100,-100);      return $s; } ?> 

We create a button and bind certain events to it. The react function changes the color of the square, depending on what is happening. If we click the square, for instance, the color of the square will be blue. addAction defines an action ”what to do ”when the button is pressed. In this case, we want redirect.php to be called by the Flash movie.

redirect.php opens a connection to the database and inserts the current time into table hits . After that, the user is redirected to Flash again:

 <?php      # connecting to the database      $dbh=pg_connect("dbname=myflash user=hs");      if    (!$dbh)      {              echo ("ERROR: connecting failed");              exit;      }      # inserting clicks into database      $sql="INSERT INTO hits(click) VALUES(now())";      $res=pg_exec($dbh, $sql);      if (!$res)      {              exit;      }      # redirecting back to Flash ...       echo ('<head> <meta http-equiv="refresh" content="0; URL=index3.php"> </head>'); ?> 

The following shows what has been inserted into the database:

 myflash=#  SELECT * FROM hits;  id           click           typ -----+------------------------+-----  428  2001-06-26 10:27:03+02   429  2001-06-26 10:27:04+02 

Someone clicked the red square twice; the time stamps of the two events have been stored in the database.

Note

Redirecting has to be done by the Flash movie; don't include any IF clauses when the events are added to the movie by the PHP file. They won't work, because the movie is displayed when the generation of the movie is over and not before. The interaction with the database is not done by Flash.


The example shows that it is an easy task to build database-enabled Flash applications with the help of PHP. The development of the Ming module will continue, so the Open source community provides a powerful platform for building attractive applications.



PostgreSQL Developer's Handbook2001
PostgreSQL Developer's Handbook2001
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net