Drawing line graphics leads Flash to spiral out of control!
Posted
by drpepper
on Stack Overflow
See other posts from Stack Overflow
or by drpepper
Published on 2010-05-04T15:42:03Z
Indexed on
2010/05/05
9:18 UTC
Read the original article
Hit count: 200
Hi, I'm having problems with some AS3 code that simply draws on a Sprite
's Graphics
object. The drawing happens as part of a larger procedure called on every ENTER_FRAME
event of the stage
.
Flash neither crashes nor returns an error. Instead, it starts running at 100% CPU and grabs all the memory that it can, until I kill the process manually or my computer buckles under the pressure when it gets up to around 2-3 GB. This will happen at a random time, and without any noticiple slowdown beforehand.
WTF? Has anyone seen anything like this?
PS: I used to do the drawing within a MOUSE_MOVE
event handler, which brought this problem on even faster.
PPS: I'm developing on Linux, but reproduced the same problem on Windows.
UPDATE: You asked for some code, so here we are. The drawing function looks like this:
public static function drawDashedLine(i_graphics : Graphics, i_from : Point, i_to : Point, i_on : Number, i_off : Number) : void
{
const vecLength : Number = Point.distance(i_from, i_to);
i_graphics.moveTo(i_from.x, i_from.y);
var dist : Number = 0;
var lineIsOn : Boolean = true;
while(dist < vecLength)
{
dist = Math.min(vecLength, dist + (lineIsOn ? i_on : i_off));
const p : Point = Point.interpolate(i_from, i_to, 1 - dist / vecLength);
if(lineIsOn) i_graphics.lineTo(p.x, p.y);
else i_graphics.moveTo(p.x, p.y);
lineIsOn = !lineIsOn;
}
}
and is called like this (m_graphicsLayer
is a Sprite
):
m_graphicsLayer.graphics.clear();
if (m_destinationPoint)
{
m_graphicsLayer.graphics.lineStyle(2, m_fixedAim ? 0xff0000 : 0x333333, 1);
drawDashedLine(m_graphicsLayer.graphics, m_initialPos, m_destinationPoint, 10, 10);
}
© Stack Overflow or respective owner