Make an image dynamically appear in FLASH
I'm new to flash, but I have drag and drop code for my movieclips and so can manipulate them in this way. But I would like to be able to displa开发者_Python百科y an image depending on where the user clicks on the movie scene. What would the code be that loads an image, onClick, at the mouse X and Y? Thanks
The code you're looking for differs a little bit from the other answer.
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
var loader:Loader;
var toLoad:URLRequest;
stage.addEventListener(MouseEvent.CLICK, clicked, false, 0, true);
function clicked(evt:MouseEvent):void
{
//if loader already exists, remove it from stage
if(loader)
{
removeChild(loader);
}
loader = new Loader();
toLoad = new URLRequest("image.png");
loader.load(toLoad);
addChild(loader);
//listen for when load completes
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleted, false, 0, true);
}
function loadCompleted(evt:Event):void
{
//center to mouse position
loader.x = (mouseX - (loader.contentLoaderInfo.width)/2);
loader.y = (mouseY - (loader.contentLoaderInfo.height)/2);
}
I'm mainly a Flex developer but this code snippet of AS3 should work fine for you in Flash as well:
addEventListener(MouseEvent.CLICK,myMouseHandler);
private function myMouseHandler(event:MouseEvent):void
{
var swfLoader:SWFLoader=new SWFLoader();
swfLoader.source="http://www.google.com/images/logos/ps_logo2.png";
swfLoader.x=mouseX;
swfLoader.y=mouseY;
addChild(swfLoader); //note in flex 4 adding to container need to use addElement otherwise this code executes in there fine as well.
}
The documentation for AS3 is pretty thorough and can be found here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html they did a good job with this version of the documentation of giving you filters at the top of the page to see only items available in Flash only or using a Flex framework or using AIR. A quick breakdown of how all this relates: AS3 is the scripting language used to write code that gets compiled into what's known as AS3 bytecode (typically all in a file with a swf or swc extension) which is then interpreted by the Flash plugin in the browser. MXML is a "declarative markup language" it's very similar in appearance to HTML using tags and nesting to represent relationships of parent/child (they are both loosely XML), the MXML gets compiled to AS3 then to AS3 bytecode and once again interpreted by the flash plugin. Nice thing about Flex is there's a whole lot of pre-built components provided and the framework creates a robust set of rules for how you can create extensible components with a well defined life cycle (you know it gets created once, you know only to update the parts that require updating ie createChildren, commitProperties, measure etc.). AIR is adobe integrated run-time which is basically a flash player for the desktop where it can live outside the browser and is therefore not limited to the functionality allowed by the browser (due to security issues lots of operations are blocked by the browser), basically AIR is Flex+Desktop related goodies. I know this is more than what your asking for but I hope you find this info useful.
Good luck,
Shaun
精彩评论