Skip to main content
Known Participant
January 3, 2014
Question

If mouse is moving horizontally back and forth over movieclip

  • January 3, 2014
  • 1 reply
  • 2149 views

I am a rookie flash developer, and I have a unique scenario I need to create. I need to put out a fire, only when the mouse is moving horizontally left and right overtop of the fire movieclip.

Right now I have some code that does the trick but if the mouse is going up and down the fire is supposed to grow. And my code is just sketchy.

function everything () {

          controlMC.buttonMode = true;

          Mouse.hide();

          controlMC.startDrag(true);

          var prevX:int = 0;

          var prevY:int = 0;

          var curX:int = 0;

          var curY:int = 0;

          import flash.net.*;

 

          function Start()

          {

                    stage.addEventListener(MouseEvent.MOUSE_MOVE, CheckDirection);

          }

          Start();

 

          function CheckDirection(e:MouseEvent)

          {

                    setChildIndex(controlMC,numChildren - 1);

                    getDirection();

                    e.updateAfterEvent();

          }

 

          function getDirection(){

                    prevX = curX;

                    curX = stage.mouseX;

                    fireMC.scaleX = fireMC.scaleY;

                    prevY = curY;

                    curY = stage.mouseY;

                    success();

 

 

                    if (prevX > curX || prevX < curX && fireMC.hitTestPoint(mouseX,mouseY,true)) {

                                        fireMC.height = fireMC.height - 5;

                    }

                    else if (prevY > curY || prevY < curY && fireMC.hitTestPoint(mouseX,mouseY,true)) {

                                        fireMC.height = fireMC.height + 5;

                    }

                    function success()

                    {

                              if (fireMC.height <= 200) {

                                        myTimer.stop();

                                        myTimer.removeEventListener(TimerEvent.TIMER, countdown);

                                        Mouse.show();

                                        controlMC.stopDrag();

                                        TweenLite.to(fireMC, 1, {alpha: 0});

                                        TweenLite.to(yesMC, .5, {alpha: 1});

                                        setChildIndex(yesMC,numChildren - 1);

                                        controlMC.visible = false;

                                        yesMC.continueMC.addEventListener(MouseEvent.MOUSE_DOWN, fin);

                                        yesMC.continueMC.buttonMode = true;

                                        function fin () {

                                                  yesMC.continueMC.buttonMode = false;

                                                  yesMC.continueMC.removeEventListener(MouseEvent.MOUSE_DOWN, fin);

                                                  TweenLite.to(yesMC, .5, {alpha: 0});

                                                  setChildIndex(yesMC, 0);

                                                  gotoAndStop(1);

                                        }

                              }

                    }

          }

}

Feel free to tell me I'm a terrible as3 coder. I don't even understand classes

This topic has been closed for replies.

1 reply

Participating Frequently
January 3, 2014

wow that works ? lol just kidding but ya its a lil hard to look at .

I think the issue is inside your if statement the condition asks if coridnate is less then or greater then current or previous position. Since you are checking down to the exact pixel it is easy to move the mouse where only the first condition get fired. What you want is a threshhold of maybe 5 pixels to eliminate any unwanted movement.

so

if (prevX > curX || prevX < curX && fireMC.hitTestPoint(mouseX,mouseY,true)) {

                                        fireMC.height = fireMC.height - 5;

                    }

                    else if (prevY > curY || prevY < curY && fireMC.hitTestPoint(mouseX,mouseY,true)) {

                                        fireMC.height = fireMC.height + 5;

                    }

should be

if(fireMC.hitTestPoint(curX,curY,true) {

     if (prevX > (curX-5) || prevX < (curX+5)) {

          fireMC.height = fireMC.height - 5;

     }

     else if (prevY > (curY-5) || prevY < (curY+5)) {

          fireMC.height = fireMC.height + 5;

     }

}


yuleCoderAuthor
Known Participant
January 3, 2014

haha yeah it works. thanks for your code its cleaner and more accurate. but the else if is still doing the same thing. it shrinks the fire as well. it seems like even when trying to move vertically, there is still slight horizontal movement that throws everything off

Participating Frequently
January 3, 2014

oh i think i got the - and + backwards for both if statements , if you change those does it work