03-12-2010, 05:09 PM
|
#1
|
|
Senior Member
Join Date: Sep 2005
Location: NYC
Posts: 250
|
[AS3] Reclaimer Effect
Was going thru my files and found this little reclaimer effect that I haven't published before. It's basically one object pulling the particles from another object.
Looks something like this:
Although you really need to see it in action to understand what it is. Preview & source download here:
http://www.freeactionscript.com/2010...laimer-effect/
__________________
FreeActionScript.com- AS2 & AS3 Game Tutorials, Samples, Source Files & Downloads
|
|
|
03-12-2010, 09:16 PM
|
#2
|
|
Senior Member
Join Date: Jul 2009
Location: PA
Posts: 132
|
Looks really interesting! I think I've got a nice idea for something to add to it. Do you mind if use your code to try to make it?
|
|
|
03-12-2010, 09:19 PM
|
#3
|
|
Senior Member
Join Date: Sep 2005
Location: NYC
Posts: 250
|
Quote:
Originally Posted by hey
Looks really interesting! I think I've got a nice idea for something to add to it. Do you mind if use your code to try to make it?
|
Use it as you like. If you want to, show us what you come up with
__________________
FreeActionScript.com- AS2 & AS3 Game Tutorials, Samples, Source Files & Downloads
|
|
|
03-12-2010, 10:13 PM
|
#4
|
|
Senior Member
Join Date: Jul 2009
Location: PA
Posts: 132
|
My idea was to have a third object (titled thing3) deflect the particles around it. This actually wasn't as hard as I'd thought it be, and all I did was reuse some code from an old orbiting project.
It doesn't really work quite as it should yet, but it still looks cool. At the very least, you can get some interesting effects by messing around with the particle's mass. Here's what I did:
ActionScript Code:
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Reclaimer2 extends Sprite
{
private var particlesCurrent:Number = 0;
private var particlesTotal:Number = 30;
private var particleSpeed:Number = 10;
private var particleFadeoutSpeed:Number = .02;
private var particlesArray:Array;
private var radians:Number;
private var dx:Number;
private var dy:Number;
private var distSQ:Number;
private var dist:Number;
private var force:Number;
private var thing3Mass:Number = 5000;
public function Reclaimer2():void
{
init();
}
private function init():void
{
radians = 180 / Math.PI;
particlesArray = [];
addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);
thing1.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
thing2.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
//thing 3 in mine is essensially a bigger version of the above two
thing3.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
thing1.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
thing2.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
thing3.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
}
private function onThingDown(event:MouseEvent):void
{
event.currentTarget.startDrag();
}
private function onThingUp(event:MouseEvent):void
{
event.currentTarget.stopDrag();
}
private function createThing(target1:MovieClip, target2:MovieClip):void
{
if(particlesTotal <= particlesCurrent)
{
return;
}
particlesCurrent++;
var tempParticle:Particle = new Particle();
tempParticle.x = target1.x + Math.random() * target1.width;
tempParticle.y = target1.y + Math.random() * target1.height;
tempParticle.rotation = Math.random()*360;
tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x);
tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians / particleSpeed;
tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians / particleSpeed;
tempParticle.mass = 1;
particlesArray.push(tempParticle);
addChild(tempParticle);
}
private function updateParticle():void
{
for (var i = 0; i < particlesArray.length; i++)
{
var tempParticle:MovieClip = particlesArray[i];
tempParticle.x -= tempParticle.xSpeed;
tempParticle.y -= tempParticle.ySpeed;
tempParticle.alpha -= particleFadeoutSpeed;
dx = tempParticle.x - thing3.x;
dy = tempParticle.y - thing3.y;
distSQ = ((dx * dx) + (dy * dy));
dist = Math.sqrt(distSQ);
force = thing3Mass * tempParticle.mass/ distSQ;
tempParticle.x += force * dx / dist;
tempParticle.y += force * dy / dist;
if(tempParticle.hitTestObject(thing2))
{
destroyParticle(tempParticle);
}
else if (tempParticle.alpha <= 0)
{
destroyParticle(tempParticle);
}
else if (tempParticle.x < 0)
{
destroyParticle(tempParticle);
}
else if (tempParticle.x > stage.stageWidth)
{
destroyParticle(tempParticle);
}
else if (tempParticle.y < 0)
{
destroyParticle(tempParticle);
}
else if (tempParticle.y > stage.stageHeight)
{
destroyParticle(tempParticle);
}
}
}
private function destroyParticle(particle:MovieClip):void
{
for (var i = 0; i < particlesArray.length; i++)
{
var tempParticle:MovieClip = particlesArray[i];
if (tempParticle == particle)
{
particlesCurrent--;
particlesArray.splice(i,1);
removeChild(tempParticle);
}
}
}
private function onEnterFrameLoop(event:Event):void
{
createThing(thing1, thing2);
updateParticle();
}
}
}
Last edited by hey; 03-13-2010 at 03:21 PM.
|
|
|
03-13-2010, 12:19 PM
|
#5
|
|
Senior Member
Join Date: Sep 2005
Location: NYC
Posts: 250
|
Tried your code but didn't see any difference. What am I missing?
__________________
FreeActionScript.com- AS2 & AS3 Game Tutorials, Samples, Source Files & Downloads
|
|
|
03-13-2010, 12:31 PM
|
#6
|
|
Senior Member
Join Date: Jul 2009
Location: PA
Posts: 132
|
Should've explained what I did better, but I created a new symbol which was named symbol 2, which was for the most the same as symbol 1 except that I had its x,y cordinate in the center of itself. Then I added one of these to the stage, gave it the instance name of thing3 and set its width and height to 64. When its dragged around into the flow of particles, the particles will appear to be deflected around it.
|
|
|
03-13-2010, 02:07 PM
|
#7
|
|
Senior Member
Join Date: Sep 2005
Location: NYC
Posts: 250
|
That's pretty much what I did, aside from sizes. Problem was, I forgot to save the code after I replaced it with yours!  Doh!
I love what you did. Looks really cool
Would you mind if I release your version on my site as well? Of course with credit for your code.
__________________
FreeActionScript.com- AS2 & AS3 Game Tutorials, Samples, Source Files & Downloads
|
|
|
03-13-2010, 02:52 PM
|
#8
|
|
Senior Member
Join Date: Jul 2009
Location: PA
Posts: 132
|
Of course you can. I might mess around with it a bit latter and see what else I can do with it.
Thanks!
Edit: Oh yeah, to get it to look right you have to find the right mass to go with the size of thing3. I'll try to figure out a way to relate its mass to the size of it.
Edit2: I changed my code so that it's a bit cleaner.
Last edited by hey; 03-13-2010 at 03:20 PM.
|
|
|
03-13-2010, 06:49 PM
|
#9
|
|
Senior Member
Join Date: Sep 2005
Location: NYC
Posts: 250
|
I think i figured out how to calculate mass/force based on size. Also changed the center point of all movieclips.
Going to optimize this later and post it:
ActionScript Code:
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Reclaimer3 extends Sprite
{
private var particlesCurrent:Number = 0;
private var particlesTotal:Number = 50;
private var particleSpeed:Number = 10;
private var particleFadeoutSpeed:Number = .0175;
private var particlesArray:Array;
private var radians:Number;
private var dx:Number;
private var dy:Number;
private var distSQ:Number;
private var dist:Number;
private var forceX:Number;
private var forceY:Number;
public function Reclaimer3():void
{
init();
}
private function init():void
{
radians = 180 / Math.PI;
particlesArray = [];
addEventListener(Event.ENTER_FRAME, onEnterFrameLoop);
thing1.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
thing2.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
thing3.addEventListener(MouseEvent.MOUSE_DOWN, onThingDown);
thing1.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
thing2.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
thing3.addEventListener(MouseEvent.MOUSE_UP, onThingUp);
}
private function onThingDown(event:MouseEvent):void
{
event.currentTarget.startDrag();
}
private function onThingUp(event:MouseEvent):void
{
event.currentTarget.stopDrag();
}
private function createParticle(target1:MovieClip, target2:MovieClip):void
{
if(particlesTotal <= particlesCurrent)
{
return;
}
particlesCurrent++;
var tempParticle:Particle = new Particle();
tempParticle.x = (target1.x - target1.width / 2) + (Math.random() * target1.width);
tempParticle.y = (target1.y - target1.height / 2) + (Math.random() * target1.height);
tempParticle.rotation = Math.random()*360;
tempParticle.rot = Math.atan2(target1.y - target2.y, target1.x - target2.x);
tempParticle.xSpeed = Math.cos(tempParticle.rot) * radians / particleSpeed;
tempParticle.ySpeed = Math.sin(tempParticle.rot) * radians / particleSpeed;
tempParticle.mass = tempParticle.width / 2 + tempParticle.height / 2;
particlesArray.push(tempParticle);
addChild(tempParticle);
}
private function updateParticle():void
{
for (var i = 0; i < particlesArray.length; i++)
{
var tempParticle:MovieClip = particlesArray[i];
tempParticle.x -= tempParticle.xSpeed;
tempParticle.y -= tempParticle.ySpeed;
tempParticle.alpha -= particleFadeoutSpeed;
dx = tempParticle.x - thing3.x;
dy = tempParticle.y - thing3.y;
distSQ = ((dx * dx) + (dy * dy));
//force = mass x acceleration
forceX = tempParticle.mass * tempParticle.xSpeed;
forceY = tempParticle.mass * tempParticle.ySpeed;
var thing3Mass:Number = (thing3.width / 2 + thing3.height / 2);
tempParticle.x += thing3Mass * (dx+forceX) / distSQ;
tempParticle.y += thing3Mass * (dy+forceY) / distSQ;
if(tempParticle.hitTestObject(thing2))
{
destroyParticle(tempParticle);
}
else if (tempParticle.alpha <= 0)
{
destroyParticle(tempParticle);
}
else if (tempParticle.x < 0)
{
destroyParticle(tempParticle);
}
else if (tempParticle.x > stage.stageWidth)
{
destroyParticle(tempParticle);
}
else if (tempParticle.y < 0)
{
destroyParticle(tempParticle);
}
else if (tempParticle.y > stage.stageHeight)
{
destroyParticle(tempParticle);
}
}
}
private function destroyParticle(particle:MovieClip):void
{
for (var i = 0; i < particlesArray.length; i++)
{
var tempParticle:MovieClip = particlesArray[i];
if (tempParticle == particle)
{
particlesCurrent--;
particlesArray.splice(i,1);
removeChild(tempParticle);
}
}
}
private function onEnterFrameLoop(event:Event):void
{
createParticle(thing1, thing2);
updateParticle();
}
}
}
__________________
FreeActionScript.com- AS2 & AS3 Game Tutorials, Samples, Source Files & Downloads
|
|
|
03-15-2010, 03:05 PM
|
#10
|
|
Senior Member
Join Date: Sep 2005
Location: NYC
Posts: 250
|
__________________
FreeActionScript.com- AS2 & AS3 Game Tutorials, Samples, Source Files & Downloads
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 03:05 AM.
///
|
|