Is there a way for me to optimize this code? At the current rate it slows my computer all the way down… there has to be a better way. Perhaps a substitute for enterFrame?
Problematic Code:
stage.addEventListener(Event.ENTER_FRAME, drawPath);
function drawPath(e:Event):void{
for (var m:int=0; m<hitArray.length; m++) {
var blueRect:Rectangle = myPoint.getBounds(this);
var blueOffset:Matrix = myPoint.transform.matrix;
blueOffset.tx = myPoint.x - blueRect.x;
blueOffset.ty = myPoint.y - blueRect.y;
var myPointBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
myPointBmpData.draw(myPoint, blueOffset);
var redRect:Rectangle = hitArray[m].getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
var redOffset:Matrix = hitArray[m].transform.matrix;
redOffset.tx = hitArray[m].x - redRect.x;
redOffset.ty = hitArray[m].y - redRect.y;
redClipBmpData.draw(hitArray[m], redOffset);
var rLoc:Point = new Point(redRect.x, redRect.y);
var bLoc:Point = new Point(blueRect.x, blueRect.y);
if(redClipBmpData.hitTest(rLoc,
255,
myPointBmpData,
bLoc,
255
))
{
trace("hit");
hitArray[m].visible = false;
}
//else
// {
// hitArray[m].visible = true;
// }
}
myPointBmpData.dispose();
redClipBmpData.dispose();
//}
In case you need to see res of the code i posted it here, but that enterFrame function ^^ combined with array… is the one causing everything to run slow
All of Code:
import flash.events.MouseEvent;
myPoint.addEventListener(MouseEvent.MOUSE_DOWN, dragBegin)
myPoint.addEventListener(MouseEvent.MOUSE_UP, dragEnd)
function dragBegin(e:MouseEvent){
e.target.startDrag();
}
function dragEnd(e:MouseEvent){
e.target.stopDrag();
}
///**************************************************************************\\\
//Part I -- Set up the board
var bWidth:Number = stage.stageWidth;
var bHeight:Number = stage.stageHeight;
var grid:Number = 5; // Size of the grid and number of lattice points in each direction
var dotsWide:Number = Math.ceil(bWidth/grid) - 1;
var dotsHigh:Number = Math.ceil(bHeight/grid) - 1;
var board:Sprite = new Sprite();
var shape1:MovieClip = new MovieClip();
addChild(shape1);
stage.addChild(board);
var j:int;
var i:int;
var k:int;
var hitArray:Array = new Array();
for (i = 0; i < dotsHigh; k++) {
hitArray[k] = new MovieClip();
hitArray[k].name = "target" + k;
addChild(hitArray[k]);
hitArray[k].graphics.beginFill(0x333333,1);
hitArray[k].graphics.drawRect(j*grid,i*grid,3,3);
hitArray[k].graphics.endFill();
j++;
if( j >dotsWide){
i++;
j = 0;
}
}
//main event to define path
stage.addEventListener(Event.ENTER_FRAME, drawPath);
function drawPath(e:Event):void{
for (var m:int=0; m<hitArray.length; m++) {
var blueRect:Rectangle = myPoint.getBounds(this);
var blueOffset:Matrix = myPoint.transform.matrix;
blueOffset.tx = myPoint.x - blueRect.x;
blueOffset.ty = myPoint.y - blueRect.y;
var myPointBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
myPointBmpData.draw(myPoint, blueOffset);
var redRect:Rectangle = hitArray[m].getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
var redOffset:Matrix = hitArray[m].transform.matrix;
redOffset.tx = hitArray[m].x - redRect.x;
redOffset.ty = hitArray[m].y - redRect.y;
redClipBmpData.draw(hitArray[m], redOffset);
var rLoc:Point = new Point(redRect.x, redRect.y);
var bLoc:Point = new Point(blueRect.x, blueRect.y);
if(redClipBmpData.hitTest(rLoc,
255,
myPointBmpData,
bLoc,
255
))
{
trace("hit");
hitArray[m].visible = false;
}
//else
// {
// hitArray[m].visible = true;
// }
}
myPointBmpData.dispose();
redClipBmpData.dispose();
}
myPoint.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE, pointMove);
}
function pointMove(e:MouseEvent):void {
myPoint.x = goodX(board.mouseX);
myPoint.y = goodY(board.mouseY);
e.updateAfterEvent();
}
stage.addEventListener(MouseEvent.MOUSE_UP, stopAll);
function stopAll(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, pointMove);
}
//Part III -- Helper functions to stay within boundary AND snap to grid
function goodX(inX:Number):Number {
if (inX > grid*dotsWide) {
return grid*dotsWide;
}
if (inX < grid) {
return grid;
}
return grid*Math.round(inX/grid);
}
function goodY(inY:Number):Number {
if (inY > grid*dotsHigh) {
return grid*dotsHigh;
}
if (inY < grid) {
return grid;
}
return grid*Math.round(inY/grid);
}