3 Replies Latest reply on Jul 29, 2006 7:40 PM by butcho

    detect if mouse is over a text and out

    julieta
      Hi, I am new to flash, and I wonder if someone could help me. I have a dynamic text field on the stage. I want to be able to do the following: when the user rolls over the text, the text changes its content, continuously displaying different texts out of an array that I have populated with texts. This should only stop when the user rolls the mouse out of the field.
      I have the array and the textfield. The question is: how do I know when the mouse is over this field? I have used hitTest but I think that's just for movies, and I also tried an invisible button on top. The problem is that the action of getting text out of the array should be infinite, going through the array as many times as necessary, while the mouse is on the field. I tried onRollover, but then i wouldn't be able to break the loop. So I need something like:

      While the mouse is on the button, or on the text field, do this. As soon as the mouse is out of it, stop.

      I did this in director once, but flash logic is different...

      Could someone give me please a hint?

        • 1. Re: detect if mouse is over a text and out
          Hi,

          look like you post twice the same message so ill post again my answer! Hope it will help you...

          try something like this:

          assume your textfield is called textBox and put it in a movieclip called myMcBtn. Assume also that your array is called textArray.

          var textArray:Array = ["me", "you", "them", "us", "my dog", "and everyone else"];
          var loop:Number;
          var myArrayPosition:Number = 0;

          myMcBtn.onRollOver = function(){
          loop = setInterval(startTextLoop, 2000);
          }
          myMcBtn.onRollOut = function(){
          stopTextLoop();
          }

          function startTextLoop(){
          myMcBtn.textBox.text = textArray[myArrayPosition];
          myArrayPosition++;
          if(myArrayPosition >= textArray.length){
          myArrayPosition = 0;
          }
          }

          function stopTextLoop(){
          clearInterval(loop);
          }
          • 2. Re: detect if mouse is over a text and out
            julieta Level 1
            Thanks! It absolutely worked. I was trying to do on rollover and on roll out as well, but it would get into an infinite loop, I do not know why. Could it be because I was using while instead of set interval?
            Any ways, later I came to another solution, too, but I don't think is as efficient as yours. It was somwthing like

            _root.onEnterFrame = function() {
            if (this.textwindow3_mc.hitTest(_xmouse,_ymouse,true)) {
            this.textwindow3_mc.text1_txt.text=my_texts ;
            i=i+1;
            if (i==n) { i=0;}
            }
            }
            • 3. detect if mouse is over a text and out
              Level 1
              Hi,

              well if you mean "as efficient" for the onEnterFrame Loop it is as efficient. But for your case, your loop time will be set by your frame rate (wich can be too fast to read the text properly) with no possibility to change it. Thats why i suggested a setInterval(). In my example i put a timing of 2000 miliseconds .. but you can change it to whatever you want ... im sure you figure it out.

              I also suggest to put that time number in a variable. So if you want to change it on runtime it will be possible.

              var timerNum:Number = 2000;

              myMcBtn.onRollOver = function(){
              loop = setInterval(startTextLoop, timerNum);
              }

              And also the use of a while is definitly not worknig for what you want to achieve. The while loop as no loop time, it will loop as fast as your machine can process the data. So no chance anyone will be able to read the changing text.