Skip to main content
Participant
September 30, 2013
Question

How to change external pictures in a sprite with a timer

  • September 30, 2013
  • 1 reply
  • 1196 views

Hi

I want to display the images from a webcam but everytime the sprite updates the picture disapears for a short period.

This is my code:

import flash.utils.Timer;

import flash.events.TimerEvent;

import flash.display.Loader;

import flash.display.LoaderInfo;

import flash.net.URLRequest;

import flash.events.Event;

import flash.display.Sprite;

var e45Sprite:Sprite = new Sprite;

var myTimer:Timer = new Timer(6000);

var imageLoader:Loader = new Loader();

var imageRequest:URLRequest = new URLRequest('http://someurl.jpg');

e45Sprite.x = 0;

e45Sprite.y = 0;

addChild(e45Sprite);

imageLoader.load(imageRequest);

imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showPic);

myTimer.addEventListener(TimerEvent.TIMER,someFunction);

myTimer.start();

function someFunction(event:TimerEvent) {

imageLoader.load(imageRequest);

imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showPic);

}

function showPic(Event):void {

e45Sprite.addChild(imageLoader);

}

Can anyone tell me how I can avoid the "blinks" or holes when the sprite gets updated?

Best regards

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
September 30, 2013

Since you are reusing the same Loader object for each image you are probably catching the point where what was previously loaded is being removed and replaced with the newly loaded.  Try using a new Loader for each new image.  That way the intention of the showPic function might be better served - add the new Loader instead of the same one over and over again.  As is, after it adds the imageLoader the first time there is no need to re-add it each time a new image is loaded.

o3jvindAuthor
Participant
September 30, 2013

Thank you, Ned

I'm not sure I fully understand - I'm just starting with AS3.

I thought that the content of "imageLoader" was copied to "e45Sprite" in

function showPic(Event):void {

e45Sprite.addChild(imageLoader);

}

But it seems that "e45Sprite" is  a representation of "ImageLoader" and not a copy. Is this correct?

I'm not sure I understand your last sentence.

As is, after it adds the imageLoader the first time there is no need to re-add it each time a new image is loaded.

Ned Murphy
Legend
September 30, 2013

e45Sprite.addChild(imageLoader);

is not adding the content of the Loader.  It is adding the Loader itself.  e45Sprite is not a copy or representation of the Loader... it contains the Loader.  The Loader contains the loaded image.  And once you add the Loader there is no reason to keep doing so.

Since a Loader can only hold one loaded object at any given time, when you use the same Loader to load another image there is a point where the new replaces the old.

The content of the Loader is acquired using the content property of the Loader, literally as .... imageLoader.content

If you want to isolate the content you can create a Bitmap using it...

var image:Bitmap = imageLoader.content;