Skip to main content
Participant
September 26, 2012
Question

Avoid Cursor within a bound box

  • September 26, 2012
  • 1 reply
  • 1146 views

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.

This topic has been closed for replies.

1 reply

esdebon
Inspiring
September 26, 2012

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;

    }

    if(20>dist(mouseX-butterfly_mc.x,mouseY-butterfly_mc.y)){

        Mouse.hide();

    }

}

function dist(x0:Number,x1:Number,y0:Number,y1:Number):Number{

    return Mathg.sqrt(((x0-x1)*(x0-x1))+((y0-y1)*(y0-y1)));

   

}

Participant
September 26, 2012

Hi,

Thanks for your help. I see I made a typo and the sqrt function was added at the bottom here. Though what I'm not understanding is why my butterfly now only moves to the right.

Thanks,

Joy

esdebon
Inspiring
September 26, 2012

the problem with the move of the butterfly are the conditionals, con you tell more about the bounds?

    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){    //<--------------------------

          trace("butterfly_mc.y >= stageTop")

        butterfly_mc.y += speed * moveBottom;

    } else if (butterfly_mc.y <= stageBottom){   //<--------------------------

          trace("butterfly_mc.y <= stageTop")

        butterfly_mc.y += speed * moveTop;

    } else if (myButterfly.y == stageBottom){

        trace("butterfly_mc.y == stageTop")

        butterfly_mc.y += speed * moveTop;

    }

    if(20>dist(mouseX-butterfly_mc.x,mouseY-butterfly_mc.y)){  // <---------------  when the pointer is within 20 pixels of the butterfly the cursor is hidden

        Mouse.hide();

    }

}

function dist(x0:Number,x1:Number,y0:Number,y1:Number):Number{

    return Math.sqrt(((x0-x1)*(x0-x1))+((y0-y1)*(y0-y1)));     // <----------- caluculate the distance butterfly to mouse

   

}