Skip to main content
NyaaaChan
Participant
December 29, 2014
Question

.swf file is lagging when I try the animation but not while I preview it on Flash

  • December 29, 2014
  • 1 reply
  • 1180 views

Hello,

I've started a file in Flash CS6 that includes animation and actionscript 3 and everything looked fine when I previewed it with CTRL + ENTER during the process. After I saved and closed my .fla I tried the .swf and it is lagging whenever I reach the part that involved most of the complicated actionscript.

I already tried to downsize the preview settings quality and it's still lagging at 20, so I'm not really sure if it's the actionscript itself.

stop();

var lineSize:Number=400;

// doDraw is true when user's mouse is down

var doDraw:Boolean=false;

// resumeDrawing is true when user drags their mouse off stage while drawing

var resumeDrawing:Boolean=false;

/*

Create a bitmap to act as our image mask

Add it to stage & cache as bitmap

*/

var erasableBitmapData:BitmapData = new BitmapData(1280, 768, true, 0xFFFFFFFF);

var erasableBitmap:Bitmap = new Bitmap(erasableBitmapData);

erasableBitmap.cacheAsBitmap = true;

addChild(erasableBitmap);

/*

Set the erasable bitmap as a mask of our image & cache image as bitmap

*/

puddle_mc.cacheAsBitmap = true;

puddle_mc.mask = erasableBitmap;

/*************************

ERASER

**************************/

/*

Create a sprite for drawing the eraser lines onto

Set its lineStyle, and move the line to the current mouse x,y

*/

var eraserClip:Sprite = new Sprite();

initEraser();

function initEraser():void {

    eraserClip.graphics.lineStyle(lineSize,0xff0000);

    eraserClip.graphics.moveTo(stage.mouseX,stage.mouseY);

}

/*

This is required to ensure a smooth erase effect (applying eraserClip

directly to erasableBitmapData leaves jaggy edges  around alpha areas.

If anyone knows why, I'd love to know!)

*/

var drawnBitmapData:BitmapData = new BitmapData(1280, 768, true, 0x00000000);

var drawnBitmap:Bitmap = new Bitmap(drawnBitmapData);

/*************************

MOUSE EVENTS

**************************/

/*

Add event listeners for mouse movements

*/

stage.addEventListener(MouseEvent.MOUSE_MOVE,puddle_mcMove);

stage.addEventListener(MouseEvent.ROLL_OUT, puddle_mcOut);

stage.addEventListener(MouseEvent.ROLL_OVER,puddle_mcOver);

stage.addEventListener(MouseEvent.MOUSE_DOWN,startDrawing);

stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing);

/*

Mouse down handler

Begin drawing

*/

function startDrawing(e:MouseEvent):void {

    eraserClip.graphics.moveTo(stage.mouseX,stage.mouseY);

    doDraw=true;

}

/*

Mouse up handler

Stop drawing

*/

function stopDrawing(e:MouseEvent):void {

    doDraw=false;

    resumeDrawing = false;

}

/*

Mouse out handler

If user was drawing when they moved mouse off stage, we will need

to resume drawing when they move back onto stage.

*/

function puddle_mcOut(e:Event):void {

    if (doDraw){

        resumeDrawing = true;

    }

}

/*

Mouse over handler

If user's mouse if still down, continue drawing from the point where

the mouse re-entered the stage.

*/

function puddle_mcOver(e:MouseEvent):void {

    if (resumeDrawing){

        resumeDrawing = false;

        eraserClip.graphics.moveTo(stage.mouseX,stage.mouseY);

    }

}

/*

Mouse move handler

*/

function puddle_mcMove(e:MouseEvent):void {

    if (doDraw && !resumeDrawing){

        // Draw a line to current mouse position

        eraserClip.graphics.lineTo(stage.mouseX,stage.mouseY);

        // Clear the drawn bitmap by filling it with a transparent color

        drawnBitmapData.fillRect(drawnBitmapData.rect, 0x00000000);

        // Copy our eraser drawing into the erasable bitmap

        // (This is required to ensure the smooth alpha edges on our eraser are retained)

        drawnBitmapData.draw(eraserClip , new Matrix(), null, BlendMode.NORMAL);

        // Fill the erasable bitmap with a solid color

        erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);

        // Copy the scribble bitmap to our main bitmap, with blendmode set to ERASE

        // This erases the portion of the mask that has been drawn.

        erasableBitmapData.draw(drawnBitmap, new Matrix(), null, BlendMode.ERASE);

    }

    // Update after event to ensure no lag

    e.updateAfterEvent();

}

//MOUSE

Mouse.hide();

mouse_mc.visible = true;

mouseclick_mc.visible = false;

stage.addEventListener(Event.ENTER_FRAME, mouseFollower)

function mouseFollower (e:Event):void {

    mouse_mc.x = mouseX;

    mouse_mc.y = mouseY;

    mouseclick_mc.x = mouseX;

    mouseclick_mc.y = mouseY;

}

stage.addEventListener (MouseEvent.MOUSE_DOWN, mouseClicked)

function mouseClicked (e:Event):void {

    mouse_mc.visible = false;

    mouseclick_mc.visible = true;

}

stage.addEventListener (MouseEvent.MOUSE_UP, mouseRelease)

function mouseRelease (e:Event):void {

    mouse_mc.visible = true;

    mouseclick_mc.visible = false;

}

I'm leaving the actionscript I used here so it's easier if someone knows how to help me out.

Thank you

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 29, 2014

there's not necessarily a simple fix for you.

but for animations that have limited bitmap changes, enabling the cacheAsBitmap property can have a dramatic effect on smoothing the display,.