Search code examples
memoryactionscriptgraphdrawing

Actionscript - Heart-rate monitor with the drawing API?


Good evening all. I've got a movieclip which monitors and graphs the level of activity coming in, like a heart-rate monitor at the hospital. The graph scrolls the data off the screen, but the movieclip is never cleared, and the off-screen data builds up indefinitely. The longer the user stays on the page, the more memory the un-seen part of the graph takes.

What's the best way to clean up the unseen part of the movieclip?

The code is as follows:

this.stage.addEventListener(flash.events.Event.ENTER_FRAME,
  function(event){
    var dif = md.getDifferences();
    meter.graphics.lineTo(xpos,dif);
    meter.graphics.moveTo(xpos,dif);
    if(xpos>WIDTH){
    // once the graph exceeds the width of the clip, the graph scrolls off the screen
      meter.x -= 1;
    }
    xpos ++;
  }
);

Solution

  • This is how I'd do it:

    1. Store the coordinates that are visible on the screen in an array,
    2. DisplayObject.graphics.clear();
    3. Redraw the graph with the visible area only.

    Hope it helps. Rob