May 25, 2012, 12:36:19 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Prowlie Welcome back! The site migration is complete and normal service resumed.
Advanced search
Pages: [1]   Go Down
Print
Author Topic: Swarm Effect for Mouse  (Read 683 times)
0 Members and 1 Guest are viewing this topic.
Triangle Man
Fledgling
*
Offline Offline

Gender: Male
Posts: 1


« on: June 13, 2010, 12:06:37 AM »

Hello

This is a tutorial from Newgrounds i found. I thought somebody might find this useful. What happens is that it loads pixels and they swarm around your mouse. It is realistic and really cool. I didn't write this code and do not take any credit of this. I have purely copied the code of a Newgrounds swarm tutorial and it works fine except the person who made the tutorial didn't mention that you have to change the identifier to "bugs" for it to work as you call on importing the bugs to the stage in the code.

This is the link if you want to watch it  http://www.newgrounds.com/portal/view/325278

the code is
Code:
fly = function () {
this.animate = function() {
// Capture mouse positions and distance from mouse
this.targetX = _root._xmouse;
this.targetY = _root._ymouse;
this.distX = this.targetX-this.meX+this.flockX;
this.distY = this.targetY-this.meY+this.flockY;
//
if ((this.targetX == this.oldTargetX) && Math.random()>0.9) {
// add small scale random darting if mouse is still
this.flockX = (Math.random()*100)-50;
this.flockY = (Math.random()*100)-50;
} else if ((this.targetX<>this.oldTargetX) && Math.random()>0.8) {
// add large scale random darting if mouse is moving
this.flockX = (Math.random()*400)-200;
this.flockY = (Math.random()*400)-200;
}
// Apply inertia equation
this.meX = Math.round(this.meX+(this.distX)/20);
this.meY = Math.round(this.meY+(this.distY)/20);
// perform animation
this._x = this.meX;
this._y = this.meY;
// remember the current mouse pos so we can tell if
// it has moved next time around
this.oldTargetX = this.targetX;
};
this.initialize = function() {
this.targetX = 0;
this.targetY = 0;
this.distX = 0;
this.distY = 0;
this.meX = this._x;
this.meY = this._y;
this.oldTargetX = 0;
this.flockX = (Math.random()*200)-100;
this.flockY = (Math.random()*200)-100;
};
// set up onEnterFrame script to animate _parent...
this.initialize();
this.onEnterFrame = this.animate;
};
//
//
var i:Number = 0;
var bugClip:MovieClip;
for (i=0; i<30; i++) {
bugClip = this.attachMovie("bug", "bug"+i, i);
fly.apply(bugClip);
}

As i have said before this is not my work and don't take credit for it.
Enjoy.
« Last Edit: August 13, 2010, 10:35:06 PM by Triangle Man » Logged

I am not a triangle nor am I a man. I am Triangle Man
Brackenwood
   

 Logged
Pages: [1]   Go Up
Print
Jump to:  

Theme by Pieter, based on Black Rain by Crip Powered by SMF 1.1.16 | SMF © 2011, Simple Machines XHTML | CSS

Page created in 0.055 seconds with 24 queries.