A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Reverse Cursor Follow

  1. #1

    Reverse Cursor Follow

    I'm looking for a tutorial or example of a reverse cursor follow script.

    So instead of having duplicate movie clips following the cursor around the Flash piece, I want to randomly place them on the stage and as the cursor passes by, they will move away, kind of like a leaf blower effect.

    Any help would be appreciated.

    Thanks.

  2. #2
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Well if you know how to make an object more towards the mouse why not just reverse the + and -'s.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  3. #3
    Member
    Join Date
    Sep 2004
    Posts
    89
    can't you hitTest them against the player's location, and if they hit it (shapeFlag) move them away in the angle (atan2) reversed to the mouse movement?

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    use reverse gravitational fields
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  5. #5
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    Here's a working example with comments, this will hopefully give you some ideas. To make it work, create a small movie clip, right-click on its symbol in the library and set its linkage id to particleMC:

    Code:
    createParticles(100);
    
    
    function createParticles(numOfParticles) {
    	for (var n=0;n<numOfParticles;n++) {
    		// attach a movie clip
    		var mc = _root.attachMovie("particleMC", "p"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
    		// place it at a random start position
    		mc._x = random(Stage.width);
    		mc._y = random(Stage.height);
    		// set it's starting velocity to zero
    		mc.xVel = 0;
    		mc.yVel = 0;
    		// set it's starting acceleration to zero
    		mc.xAcc = 0;
    		mc.yAcc = 0;
    		// define the code that will run for each particle on every frame
    		mc.onEnterFrame = function() {
    			// move the particle
    			this._x += this.xVel;
    			this._y += this.yVel;
    			// calculate new velocity 
    			this.xVel += this.xAcc;
    			this.yVel += this.yAcc;
    			// add friction (so that the particle eventually stops if no force (acceleration) is applied
    			this.xVel *= 0.95;
    			this.yVel *= 0.95;
    
    			// if the particles moves outside the stage, let if appear on the opposite side
    			if (this._x > Stage.width) {
    				this._x -= Stage.width;
    			} else if (this._x < 0) {
    				this._x += Stage.width;
    			}
    			if (this._y > Stage.height) {
    				this._y -= Stage.height;
    			} else if (this._y < 0) {
    				this._y += Stage.height;
    			}
    
    			// calculate the acceleration, based on the mouse position
    			// this is something you don't have to do every frame, 
    			// to speed up the code, you could run it say every 3:rd frame
    			this.calcAcceleration();
    		}
    		mc.calcAcceleration = function() {
    			// find the distance in x between the mouse and the particle
    			var dx = _root._xmouse - this._x;
    			// find the distance in y between the mouse and the particle
    			var dy = _root._ymouse - this._y;
    			
    			// if the distance is more than half the stage,
    			// the force should wrap so the mouse can affect
    			// particles near the opposite edge
    			// (this is so that when the mouse is near the right stage edge
    			// it will repell particles near the left side of the stage)
    			if (Math.abs(dx) > Stage.width/2) {
    				if (dx > 0) {
    					dx -= Stage.width;
    				} else {
    					dx += Stage.width;
    				}
    			}
    			if (Math.abs(dy) > Stage.height/2) {
    				if (dy > 0) {
    					dy -= Stage.height;
    				} else {
    					dy += Stage.height;
    				}
    			}
    			
    			// find the angle between the mouse and particle
    			var angleToMouse = Math.atan2(dy, dx);
    			// calculate the distance
    			var distance = Math.sqrt(dx*dx + dy*dy);
    			// set the acceleration to the direction away from 
    			// the mouse, and make it fainter the farther away from the mouse
    			this.xAcc = -20 * Math.cos(angleToMouse)/distance;
    			this.yAcc = -20 * Math.sin(angleToMouse)/distance;		
    		}
    	}
    }

  6. #6
    Thanks! I'll test this out this afternoon and reply again.

    I appreciate the help and suggestions from everyone.

  7. #7
    Strille,

    Thanks for the script. It was close enough to what I was looking for, that I could make a bunch of tweaks and use it. One thing that I can't seem to change though, is the fact that eventually, if the cursor is off the Flash piece, all the clips seem to float to the same point.

    I actually, wrote a quick script to detect no mouse movement for a couple of seconds that triggers a loop, adjusting the velocity and other variables, but I still can't seem to get them to not congregate.

    Any suggestions on maybe adding something in there to make them slightly repel each other? Or another suggestion possibly?

    Thanks

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