0

I am working on a project which is loading png(png1) on run-time in front of another png(png2) image(which is placed earlier)

I have done this and it is working properly, the problem is after some time png1 transparenting to the background even the png2 placed in middle of png1 and background, bellow i have attached screenshots of the issue and the code.

import flash.net.URLLoader;
import flash.net.URLRequest;

var fl_TimerInstance: Timer = new Timer(1000);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();

var fl_SecondsElapsed: Number = 1;

function fl_TimerHandler(event: TimerEvent): void {

    var imageLoader: Loader = new Loader();
    var image: URLRequest = new URLRequest("C:\\Users\\Public\\Pictures\\pic.png"); //png1 = pic.png
    imageLoader.load(image);
    Zerolocation.addChild(imageLoader);

}

ScreenShots:

Before Error - https://drive.google.com/file/d/19a0t2jEGfDoX2voQ96rap4XpvDlGMWBd/view?usp=sharing

Error - https://drive.google.com/file/d/1a--EIEXz2Qzt5SBfl8Y8SxDIAG3DkYZf/view?usp=sharing

Timeline - https://drive.google.com/file/d/1s2uPSpOYAcfEJqdNqD4QpDGla8Gvs5LC/view?usp=sharing

It would be much appreciated if anyone can give me a clue about what is wrong the with this.

  • You load 1 and add to the display list external image every 1 second. Is there a point to it? – Organis Dec 04 '18 at 10:30
  • Yes. image is subject to change time by time. – Nishan Nilupul Dec 04 '18 at 10:36
  • 1
    Do you remove the existing image before adding the next one? I don't see that in your code. If no, then you like load and put 3600 **distinct** image objects per hour. No wonder the application misbehaves in some way. – Organis Dec 04 '18 at 10:47
  • Added bellow code bottom of the f1_TimerHandler but ,No luck, removeChild(imageLoader); imageLoader = null; – Nishan Nilupul Dec 04 '18 at 11:20
  • The **imageLoader** variable is a local function variable, it does not persist between function calls. You cannot access any temporary data from the previous function call. – Organis Dec 04 '18 at 11:31
  • Welcome to Stackoverflow! Please describe the problem in a bit more detail (edit your question and add it, to supplement your images). Right now it's not very clear (at least to me) what exactly the problem is. How much time passes between your before and after screenshots? One second? The length of the timeline shown? some random longer amount of time? – BadFeelingAboutThis Dec 04 '18 at 15:48

1 Answers1

0

You need to replace your png1 each time you load a new file. Best if you'd check if the file is still the same so not to download it again and again each second. The reason of why does the png2 stop being displayed is exactly because you have too many transparent layers in front of png2. I recall having 24 pictures with alpha channel on top of each other caused me to lose some background that would otherwise be visible. To solve your issue I say you add a Sprite container in front of your png2 layer wise, then you could just clear all the children of that container to remove obsolete imageLoaders, then add your newly downloaded picture. Something in line of this:

// Zerolocation has a child named "runtimePic" to place loaded pics
var didWeLoad:Boolean=true;
function fl_TimerHandler(event: TimerEvent): void {
  if (!didWeLoad) return; // why downloading more while we haven't finished?
  var imageLoader: Loader = new Loader();
  var image: URLRequest = new URLRequest("C:\\Users\\Public\\Pictures\\pic.png"); //png1 = pic.png
  imageLoader.load(image);
  imageLoader.addEventListener(Event.COMPLETE,loaded);
  didWeLoad=false;
}
function loaded(e:Event):void {
  didWeLoad=true; // allowing more downloading
  e.target.removeEventListener(Event.COMPLETE,loaded); // clean up, or else you'll leak memory
  // remove old pic(s)
  Zerolocation.runtimePic.removeChildren();
  // and now add new pic
  Zerolocation.runtimePic.addChild(e.target);
}

Be warned, this code does not handle downloading errors.

Vesper
  • 18,599
  • 6
  • 39
  • 61