开发者

Load image dynamically in Flash

I have to load image dynamically using actionscript 3.0, PHP and Mysql. I have the image path in the database. Now how to show 开发者_高级运维the image in Flash?


you have to use AS3 Loader class to do that. Loader class should load php script. Php script should load image from MySQL database and display it. It could look like that:

AS3:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ImageLoaded);
loader.load(new URLRequest(urlToYourPhpScript));

function ImageLoaded(e:Event) {
    addChild(e.target.loader.content);
}

where urlToYourPhpScript can contain some get variables to identify your image: urlToYourPhpScript = 'http://www.example.com/?imageid=10'

PHP:

$link = mysql_connect("localhost", "username", "password") or die("Error: " . mysql_error());

// select our database
mysql_select_db("test") or die(mysql_error());

// get the image from the db
$sql = "SELECT image FROM images WHERE id=".$_GET['imageid'];

// the result of the query
$result = mysql_query("$sql") or die("Query error: " . mysql_error());

// set the header for the image
header("Content-type: image/jpeg");
echo mysql_result($result, 0);

// close the db link
mysql_close($link);

where $_GET['imageid'] is a parameter passed from AS3 for identyfying your image.


I don't know about PHP part but in ActionScript it's better to add Loader to stage instead of adding it's content:

  1. In that case code is simpler because you don't have to handle complete event at all.
  2. If you'll try to access 'content' field directly for image that is loaded from different server - you'll get security exception and will have to put crossdomain.xml on server with images (and that is not possible if image-server is not belong to you).

So AS3 part looks like this:

const loader:Loader = new Loader();
loader.load( new URLRequest( imageURL ) );
addChild( loader )


Check these tutorials from Flash/Actionscript evangelist Lee Brimelow:
Introduction to AmfPHP part 2
Introduction to Zend AMF

If you didn't know, gotoandlearn.com is an excellent resource for all things Flash.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜