EventListener never get's hit - actionscript-3

I'm ripping my hair off at the moment! Now hoping that one of you guys can help me solving my (now two) problem(s).
First one.
I've got the following code:
private var tmpLoader:Loader = new Loader();
private function myFunction():void {
tmpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
tmpLoader.load(new URLRequest(front.url));
}
private function onLoadComplete(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var loadedBitmap:Bitmap = loaderInfo.content as Bitmap;
var sprite:Sprite = new Sprite();
sprite.addChild(loadedBitmap);
addChild(sprite);
sprite.x = 100;
sprite.y = 200;
}
I should also mention that the front.url is equal to a local file path to an image on my computer. Like: "file:///Users/bob/Desktop/potrait.jpg"
My first problem is; why doesn't my onLoadComplete get's hit? Could it have something to do with the url/file path is passed as an argument to URLRequest? Or what could it be?
My second problem revolves hair loss and will also be solved if my first problem gets solved ;-)
Thanx!

first load your content than go for eventListener
just like this:
tmpLoader.load(new URLRequest(front.url));
tmpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
and make sure that there is no problem with your URL address

Try adding Event.COMPLETE for tmpLoader instead.

Related

Loading images using the URLRequest

recently started to learn ActionScript 3 and already have questions.
question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:
var loader:Loader=new Loader();
var urlR:URLRequest=new URLRequest("Image.jpg");
public function main()
{
loader.load(urlR);
var h:Number = loader.height;// here instead of the width of the image of h is set to 0
// And if you do like this:
DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below
// 256 is displayed, as necessary
}
private function DrawText(text:String, x:int, y:int):void
{
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = true;
txt.border = true;
txt.backgroundColor = 0xff000000;
var tFor:TextFormat = new TextFormat();
tFor.font = "Charlemagne Std";
tFor.color = 0xff00ff00;
tFor.size = 20;
txt.x = x;
txt.y = y;
txt.text = text;
txt.setTextFormat(tFor);
addChild(txt);
}
Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.
Well it's simple.
Flash is focused on the Internet, hence such problems.
If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach.
Yeah plus still highly dependent on the size of the file that you read.
Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.
You need to wait for your image to load to be able to get values out of it.
Attach an eventListener to your URLLoader.
var urlR:URLRequest = new URLRequest("Image.jpg");
loader.load(urlR);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
function loader_complete(e:Event): void {
// Here you can get the height and width values etc.
var target_mc:Loader = evt.currentTarget.loader as Loader;
// target_mc.height , target_mc.width
}
Loader

Restarting sound files in AS3

stop();
var mySound:Sound = new MyFavSong();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
myChannel = mySound.play();
pause_btn1.addEventListener(MouseEvent.CLICK, onClickPause);
function onClickPause(e:MouseEvent):void
{
lastPosition = myChannel.position;
myChannel.stop();
}
play_btn1.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPlay(e:MouseEvent):void
{
myChannel = mySound.play(lastPosition);
}
I am trying to make a restart button however everything I have read is not correctly working on bringing it back to the start of the sound. I am not really great with flash so I made it as simple as I could. Any and all help would be great.
Your code is fine and that's the correct way to implement a pause/play method in as3.
That means the problem is elsewhere. Print lastPosition at the start of your onClickPlay function. Is it printing something other than 0?
Take this as a comment. This thing only lets me post answers.

What is wrong with my JPGEncoder

Here is my code
if (event.target.content is Bitmap)
{
infotext.text = "got something";
var image:Bitmap = Bitmap(event.target.content);
var bitmapData:BitmapData = image.bitmapData;
this.addChild(image);
var j:JPGEncoder = new JPGEncoder(100);
var bytes:ByteArray = new ByteArray();
bytes=j.encode(bitmapData);
}
else
{
throw new Error("What the heck bob?");
}
When I run a debug session everything works fine till it reaches to the line
bytes=j.encode(bitmapData);
after that nothing happens and my program just goes into limbo Please help
I made changes to the code as per your suggestion
var myImage:Bitmap =Bitmap(e.target.content);
var bitmapData:BitmapData = new BitmapData(myImage.width,myImage.height,true,0xffffffff);
bitmapData.draw(myImage);
var encoder:JPGEncoder = new JPGEncoder();
var bytes:ByteArray = encoder.encode(bitmapData);
this.addChild(myImage);
but it gets stuck again after
var bytes:ByteArray = encoder.encode(bitmapData);
What am i doing wrong here?
Pretty sure I ran into this same issue a long time ago so I pulled up my code from then that I got to work.
After reviewing the code the only thing I see different is I construct the bitMapData first and assign the image via the load function.
So I think your issue is with the construction of the bitmapData var.
The following code block was cut down from a function I created that did a lot of other image manipulation.
So basically it is a cut down version and untested but it should work.
var myImage:Image = new Image();
myImage.load( Bitmap(event.target.content) );
var bitmapData:BitmapData = new BitmapData(myImage.width, myImage.height, true, 0xffffffff );
bitmapData.draw(myImage);
var encoder:JPEGEncoder = new JPEGEncoder();
var data:ByteArray = encoder.encode(bitmapData);
this.addChild(myImage);
Just in case anyone runs into this, Make sure you are using the bytearray JPEGEncoder class, it's faster: http://www.bytearray.org/?p=775
the as3core is JPGEncoder not JPEGEncoder. JPG vs JPEG... this got me also.

Loaded swf does not display

I am trying to use the Loader class to load an swf animation. If I do addChild(ldr); the animation is displayed and perma-looped, but if I try it as below, the animation doesn't display.
public var ldr:Loader;
public var explosion:MovieClip;
public var req:URLRequest;
ldr = new Loader();
req = new URLRequest("../graphics/explosion1.swf");
ldr.load(req);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
and
public function onCompleteHandler(loadEvent:Event):void {
explosion = ldr.content as MovieClip;
addChild(explosion);
}
Any ideas? Thanks!
not sure, but it may be a simple security problem : loaderInfo.content is subject to security restrictions : http://help.adobe.com/fr_FR/AS3LCR/Flash_10.0/flash/display/LoaderInfo.html#content
so then your ldr.content as MovieClip fails silently, and explosion is null :)
addChild(ldr) should be just fine for what you're doing, but if you really need access to contentLoaderInfo.content, you should either use Security.allowDomain(domain of your main app) in your explosion1.swf or set a crossdomain policy in the repertory.
hope this helps.

How to modify a already made tween?

I have a tween like this :
new Tween(myObject, "x",null,nowPosition,finalPosition,time,true);
sometween.start();
Now when the tween has not finished and is somewhere in the middle and the final position changes. I want this tween to be modified so instead of moving to its already defined postion the object goes to the final position. Any ideas?
There are many ways to achieve that, but the lazy thing that comes to mind is using the continueTo() method, assuming you're using fl.transition.Tween.
e.g.:
import fl.transitions.Tween;
var nowPosition:Number = 0;
var finalPosition:Number = 200;
var time:int = 3;
var sometween:Tween = new Tween(myObject, "x",null,nowPosition,finalPosition,time,true);
sometween.start();
stage.addEventListener(MouseEvent.MOUSE_DOWN, update);
function update(event:MouseEvent):void {
sometween.continueTo(mouseX,.2);
}
HTH,
George