actionscript 3 - How do I create an object using code in AS3 and give it a picture as display? -
package { import flash.events.*; public class declareimage extends sprite { var ship:sprite = new sprite(); public function declareimage() { } } }
i declared object. want give background picture compute should use sprite data type or else?
here example. of course, it's simple case (using fixed file name, etc), illustrate how achieve want , provide foundation move on.
a tip, begin class name uppercase letter.
package { import flash.display.loader; import flash.display.sprite; import flash.events.event; import flash.events.progressevent; import flash.net.urlrequest; public class declareimage extends sprite { private const image_url:string = 'myimage.jpg'; private var ship:sprite; private var loader:loader; public function declareimage() { ship = new sprite(); addchild(ship); loader.contentloaderinfo.addeventlistener(progressevent.progress, loadprogresshandler, false, 0, true); loader.contentloaderinfo.addeventlistener(event.complete, loadcompletehandler, false, 0, true); loader.load(new urlrequest(image_url)); } private function loadprogresshandler(event : progressevent) : void { trace('loading: ' + math.round((event.bytesloaded/event.bytestotal) * 100) + '%'); } private function loadcompletehandler(event:event):void { loader.contentloaderinfo.removeeventlistener(progressevent.progress, loadprogresshandler); loader.contentloaderinfo.removeeventlistener(event.complete, loadcompletehandler); ship.addchild(loader); trace('complete'); } } }
Comments
Post a Comment