A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: follow the cursor

  1. #1
    Junior Member
    Join Date
    Jan 2003
    Location
    Sitting in my chair
    Posts
    3

    follow the cursor

    How would i make something on screen follow around the cursor + how would i set a certain area on the screen that the symbol can only stay in. e.g. if i had a dog and i only wanted it to stay in this one area - e.g. a cirle, but still follow the cursor about as far as it can.

    any help or links would be very helpful


    thanks.

  2. #2
    Member
    Join Date
    May 2002
    Posts
    68
    heylo

    Forgive my lack of syntax i dont have flash open and dont trust myself to remember it correctly. You should find somewhere in the action bar on the left a startDrag action. Double click it and it will add the scripting. Make sure you've given the object you want to drag an instance name, then choose it as the target in the scripting. make sure the action is on(load) or on(enterframe) [yes, this is incorrect syntax but i think you will get the point.]
    As for limiting it to a certain area, it will have a constrain to rectangle option and you can specify its limits. I dont know how you would do this with a circle though.

    I hope this helped a bit... probably not the best explanation of things.

    I invite others to answer since i did such a poor job

  3. #3
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    if you want to keep within a circle you can use some trigonometry to find how far the mouse is from the centre point of that circle

    you can add this code to the main timeline

    Code:
    Math.hyp = function(x1, x2, y1, y2) {
    	return Math.sqrt(Math.pow(Math.abs(x1 - x2), 2) + Math.pow(Math.abs(y1 - y2), 2));
    };
    this returns the distance between the point (x1, y1) and (x2, y2) Math.abs is used to get round a bug in the flash 5 player (it couldn't calculate the power of a negative number correctly) since we are sqauring the values the sign of the number we square has no impact on the answer, so we use Math.abs to ignore the sign.

    now we have this function anywhere within our movie we can calculate the distance between 2 points using Math.hyp

    okay now for the code on the movie clip that moves areound,

    Code:
    onClipEvent(load) {
        // set 2 variables representing the centre point of the circle
        xCentre = 250;
        yCentre = 200; 
        // and set the limit for how far away from the centre the clip can go
        maxDist = 100;
        // the clip isn't being draghed
    }
    onClipEvent(mouseMove) {
        if (Math.hyp(xCentre, _root._xmouse, yCentre, _root._ymouse) < maxDist) { // the mouse position is still within the circle
            // move the clip to the mouse position
            this._x = _root._xmouse;
            this._y = _root._ymouse;
            updateAfterEvent();
        }
    }

  4. #4
    Junior Member
    Join Date
    Jan 2003
    Location
    Sitting in my chair
    Posts
    3
    hmm, i noticed u said drag - i dont mean drag it about...i mean say i moved the cursor tto the top of the screen and symbol would follow the cursor...however if it was in a cirlce then it would bit the top of the cirle at whatever angle my cursor was at.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center