Find the Mouse Location in Flash (5 & MX)
Results 1 to 2 of 2

Thread: Find the Mouse Location in Flash (5 & MX)

  1. #1
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447

    Find the Mouse Location in Flash (5 & MX)

    Ok this tut will use the old Flash 5 way of coding (may work in earlier versions to tho not sure) simply for the fact I taught myself in Flash 5 and have still not completly transfered to Flash MX coding

    Ok basically what this will allow you to do is have a cross hair follow the mouse around the screen - in the example I have used it to find locations on a map but really is just to show you a little of how to use flash's ability to move movieclips (mc) depending on mouse postion.

    In the example you can see there are 5 main elements
    1. Crosshair
    2. Vertical bar
    3. Horiz bar
    4. Map


    I know I said 5 and there are only 4 there but the crosshair is actiualy made of tewo parts the inner circle and the lines.

    ok lets set up our movie -
    your gonna need 8 layers :::

    1. Map
    2. Border
    3. Horiz bar
    4. Vert bar
    5. circle
    6. lines
    7. Buttons
    8. actions


    ok the first 2 are self explanitary as is the two bars (just make sure they are long enough) also as we will be applying actionscript to the bars please make sure they are movieclips

    the circle and lines mc's will contain the animation for when you mouse over an active area

    the first frame in each mc should be when it is at rest make sure to also add a
    Code:
    stop();
    function on the first frame
    now animate it when you rollover and rollout making sure to add another stop at the end of the rollover animation (not following me - take a look at the .fla while reading this tutorial for a better idea)

    ok once you have your mc made add them to the main timeline on the correct layers (by giving everything its own labeled layer and mc now it makes editing one element at a later time alot easier and is good practice for when you try more advanced flash projects)

    right now we got to add our buttons - i drew roughly round the different areas on the map but if you want (and are better at geography than me) you could add lil dots for actual places etc

    once you have your buttons made set their alpha to 0 (unless you want them to be seen)

    ok time to add our code :::

    click once on the horiz bar mc and add this code

    Code:
    onClipEvent (enterFrame) {
    	_y += (_parent._ymouse-_y);
    }
    This tells flash to move the mc along with the mouse along the Y axis

    ok now the vert bar :::

    Code:
    onClipEvent (enterFrame) {
    	_x += (_parent._xmouse-_x);
    	
    }
    exactly same thing but now is getting postion along X axis

    ok adding the code to our circle and lines mc is just the same except we need it to get both the X & Y postions - the code for both movies is the same

    Code:
    onClipEvent (enterFrame) {
    	_x += (_parent._xmouse-_x);
    	_y += (_parent._ymouse-_y);
    }
    dont forget to give your mc's instance names (i just used circle & lines) as we will be refering to them in our code a little later

    now we have got to tell the flash movie what to do when you move over one of our buttons

    the code is the same for all the buttons :::

    Code:
    on(rollover){
    	_root.circle.gotoAndPlay(2);
    	_root.lines.gotoAndPlay(2);
    }
    on(rollout){
    	_root.circle.gotoAndPlay(6);
    	_root.lines.gotoAndPlay(8);
    }
    now the frame numbers are just waht i used in my .fla and these will need to be changed depending upon the set up og your own mc's

    a quick break down of the code :::

    • on(rollout){ - tells flash to perform the following function when the mouse is placed over the button
    • _root.circle.gotoAndPlay(6); - this means that flash is to look for the mc (instance) name circle and go to the 6 frame and play


    simple

    ***Flash Mx only***
    I'm not sure as I have not tested but if you wanted to code it in flash MX i think instead of placing the rollover & rollout code on each button you could use

    Code:
    display = function () {
    and then define the function before using
    Code:
    display.apply(button);
    to attach the function to each button?

    ****End of Flash MX only****

    Ok if you test it now you should see that the horiz line, vert line and both mc's follow the cursor but lets get rid of the cursour as well

    in your actions layer place the following code

    Code:
    Mouse.hide();
    stop();
    so there you go thats it now there are other things that could be done with this - add a mc at the bottom to display different flags depending what country you are over etc - it could even be used as the basis of a lash shooting game!!

    v_Ln

    .zip file includes .swf .fla and world .gif

  2. #2
    Banned
    Join Date
    Feb 2003
    Posts
    27
    great tutorial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides