Avoid Cursor within a bound box
Hello,
I'm working on a game for a course I'm taking and I need an object to avoid the cursor, but so far, it only moves down!
import flash.events.MouseEvent;
var stageLeft = 15;//left boundary
var stageRight = 285;//right boundary
var stageTop = 40;//top boundary
var stageBottom = 185;//bottom boundary
var mPositionX = stage.mouseX; //sets horizontal margin 20
var mPositionY = stage.mouseY; //sets vertical margin 20
var moveLeft = - 1; //left movement
var moveRight = 1; //right movement
var moveTop = - 1; //top movement
var moveBottom = 1; //bottom movement
var speed:int = 5; //speed
butterfly_mc.addEventListener(MouseEvent.MOUSE_MOVE, avoidCursor);
function avoidCursor(evt:MouseEvent) {
var middle:int = butterfly_mc.width/2;
butterfly_mc.x += speed;
butterfly_mc.y += speed;
trace('x: '+mouseX+', y:'+mouseY);
trace('m: '+middle);
if (butterfly_mc.x <= stageLeft){
butterfly_mc.x += speed * moveRight;
} else if (butterfly_mc.x >= stageRight){
butterfly_mc.x += speed * moveLeft;
} else if (butterfly_mc.x == stageRight){
butterfly_mc.x += speed * moveLeft;
}
if (butterfly_mc.y <= stageTop){
butterfly_mc.y += speed * moveBottom;
} else if (butterfly_mc.y >= stageBottom){
butterfly_mc.y += speed * moveTop;
} else if (myButterfly.y == stageBottom){
butterfly_mc.y += speed * moveTop;
}
}
Any idea what I might be doing wrong here? Super confused!
ETA: I realized after I posted I was very vague on the purpose of the game. I need to accomplish certain things in certain ways to obtain credit here. The butterfly needs to avoid the mouse pointer when the pointer is within 20 pixels of the butterfly.