May 25, 2013, 09:06:23 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] 2   Go Down
Print
Author Topic: upgrade AS2.0 swarm to AS3.0  (Read 4484 times)
0 Members and 1 Guest are viewing this topic.
chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« on: December 14, 2009, 10:48:17 AM »

<a href="http://www.biteycastle.com/brackenwood/titles.swf" target="_blank">http://www.biteycastle.com/brackenwood/titles.swf</a>
Hey guys, I got an email recently from someone trying to implement the swarm component that Sham built for Prowlies at the River, waaaay back when he also built the first vCam. It doesn't work in an AS3.0 document so could any of our geniuses'es's take a crack at converting it? It's been such a useful component in the past that I reckon it needs to be in every Flash animator's toolbox alongside the vCam.

It's a component that is controlled through the Component Inspector panel and duplicates a sprite movieclip from the library to create the swarm. Here's a screenshot:



And here's Sham's original AS2.0 code from inside the component:

Code: (AS2.0 Swarm Code by Sham Bhangal)
function flyInit():Void {
this.speedX = Math.random()*speed;
this.speedY = Math.random()*speed;
this.onEnterFrame = flyMove;
}
function flyMove():Void {
this._x += this.speedX;
this._y += this.speedY;
if (Math.abs(this._x-sX)>sWidth) {
this.speedX = -this.speedX;
this._x = this._x+this.speedX;
}
if (Math.abs(this._y-sY)>sHeight) {
this.speedY = -this.speedY;
this._y = this._y+this.speedY;
}
if (Math.random()<jitter) {
this.speedX = Math.random()*speed-speed/2;
this.speedY = Math.random()*speed-speed/2;
}
}
function flyDie():Void {
for (var i = 0; i<numFlies; i++) {
var fly:MovieClip = _parent["fly"+i];
fly.removeMovieClip();
}
}
var sWidth:Number = this._width/2;
var sHeight:Number = this._height/2;
var sX:Number = this._x;
var sY:Number = this._y;
for (var i = 0; i<numFlies; i++) {
var fly:MovieClip = _parent.attachMovie("fly", "fly"+i, _parent.getNextHighestDepth());
fly._x = sX+(Math.random()*sWidth*2)-sWidth;
fly._y = sY+(Math.random()*sHeight*2)-sHeight;
fly.onEnterFrame = flyInit;
}
this._visible = false;
this.onUnload = flyDie;
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
Jaehl
Mobal Gloderator
Global Moderator
Veteran
***
Offline Offline

Gender: Male
Posts: 1418


/* No comment */


WWW
« Reply #1 on: December 14, 2009, 11:19:18 AM »

kapow!

Code removed due to an unexpected amount of FAIL.
« Last Edit: December 14, 2009, 11:41:17 PM by Jaehl » Logged


<a href="http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf" target="_blank">http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf</a><a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf</a>
Sarus translator:
<a href="http://www.jaehl.com/files/progress.swf" target="_blank">http://www.jaehl.com/files/progress.swf</a>
chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« Reply #2 on: December 14, 2009, 11:29:32 AM »

Jaehl, you are an absolute Actionscript juggernaut. A coding god in this community of code-fearing creative types! Thanks (yet again) mate Smiley
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
Snowman
Senior Member
****
Offline Offline

Gender: Male
Posts: 554


Save a plant, eat a vegetarian


« Reply #3 on: December 14, 2009, 11:31:49 AM »

That was so fast...
Logged

chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« Reply #4 on: December 14, 2009, 10:04:20 PM »

Hey Jaehl, got a few errors when I tried to use the component. Here's the log:
Code:
**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 13: 1120: Access of undefined property jitter.
     if (Math.random()<jitter) {

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 14: 1120: Access of undefined property speed.
     flies[i].speedX = Math.random()*speed-speed/2;

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 14: 1120: Access of undefined property speed.
     flies[i].speedX = Math.random()*speed-speed/2;

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 15: 1120: Access of undefined property speed.
     flies[i].speedY = Math.random()*speed-speed/2;

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 15: 1120: Access of undefined property speed.
     flies[i].speedY = Math.random()*speed-speed/2;

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 33: 1120: Access of undefined property speed.
     flies[i].speedX = Math.random()*speed;

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 34: 1120: Access of undefined property speed.
     flies[i].speedY = Math.random()*speed;

**Error** Symbol 'swarmEnvelope', Layer 'actions', Frame 1, Line 29: 1120: Access of undefined property numFlies.
     for (var i = 0; i<numFlies; i++) {

Total ActionScript Errors: 8,  Reported Errors: 8

Any thoughts? The code is inside a Swarm Envelope component (screenie in first post), and the numbers are tweaked via the component inspector.
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
ASP
Formerly Stojkovski
Junior Member
**
Offline Offline

Gender: Male
Posts: 116



« Reply #5 on: December 14, 2009, 10:20:50 PM »

Hehehe...

Jaehl: KAPOW! Too fast. Kicked all your asses. No, Chluaid, no need to thank me, just doin' what I do best...

Chluaid: it didn't work...

Man I love this forum
Logged

<a href="http://spamtheweb.com/ul/upload/2309/46159_BiteyForumSigProper.swf" target="_blank">http://spamtheweb.com/ul/upload/2309/46159_BiteyForumSigProper.swf</a>
Jaehl
Mobal Gloderator
Global Moderator
Veteran
***
Offline Offline

Gender: Male
Posts: 1418


/* No comment */


WWW
« Reply #6 on: December 14, 2009, 10:30:37 PM »

Hmm, it seems to not be finding the component parameters. Lame. I guess AS3 handles components differently. Now I gotta find out how components work in AS3.

Edit: Okay, it turns out AS3 components use external class files. I'll put up a fixed version soon.
« Last Edit: December 14, 2009, 10:54:31 PM by Jaehl » Logged


<a href="http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf" target="_blank">http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf</a><a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf</a>
Sarus translator:
<a href="http://www.jaehl.com/files/progress.swf" target="_blank">http://www.jaehl.com/files/progress.swf</a>
chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« Reply #7 on: December 14, 2009, 11:06:19 PM »

no rush no pressure mate. Always grateful for your help Smiley
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
Jaehl
Mobal Gloderator
Global Moderator
Veteran
***
Offline Offline

Gender: Male
Posts: 1418


/* No comment */


WWW
« Reply #8 on: December 14, 2009, 11:29:34 PM »

Okay, Let's try that again.
Logged


<a href="http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf" target="_blank">http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf</a><a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf</a>
Sarus translator:
<a href="http://www.jaehl.com/files/progress.swf" target="_blank">http://www.jaehl.com/files/progress.swf</a>
chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« Reply #9 on: December 14, 2009, 11:48:03 PM »

perfect! So that class file must be in the same directory as any FLA in which the component is used?
Makes sense Smiley
Legend! Thanks again man, I'll add this to the tutorials and resources thread first thing tomorrow.
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
Jaehl
Mobal Gloderator
Global Moderator
Veteran
***
Offline Offline

Gender: Male
Posts: 1418


/* No comment */


WWW
« Reply #10 on: December 14, 2009, 11:53:39 PM »

perfect! So that class file must be in the same directory as any FLA in which the component is used?
Makes sense Smiley
Yeah, bit of a pain, really. With AS2 you could just right click a movieclip and give it some parameters. With AS3 you have to create a separate class file, and then mess about linking it to the movieclip. Undecided
Logged


<a href="http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf" target="_blank">http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf</a><a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf</a>
Sarus translator:
<a href="http://www.jaehl.com/files/progress.swf" target="_blank">http://www.jaehl.com/files/progress.swf</a>
chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« Reply #11 on: December 15, 2009, 12:05:42 AM »

Yeah that's a pain but no real biggie for an ultra simple implementation like this. I guess it's the most efficient way of working with reusable code blocks from a library of class files huh?
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
supermoose
Full Member
***
Offline Offline

Gender: Male
Posts: 240


Yes. I beat IWBTG. Only took ~60 hours of play.


« Reply #12 on: December 16, 2009, 08:04:13 AM »

I cleaned it up a bit.

The code never removes its listeners from the stage.  Flash doesnt do that automatically, in case the listeners would affect something more than the one object deleted.  Depending on how many swarms are being used, how many flies are in each, etc. it would most likely lag slower computers, because every time a future frame was entered, it would still try to move it's old invisible swarm of flies.  This should help improve the speed of any .swf using the component.  Let me know if you like/dont like.  Also, this is a pretty simple script, I can comment it up for future use, if you would like.

Code:
       private function flyDie(event:Event):void {
           this.removeEventListener(Event.ENTER_FRAME,flyMove);
           this.removeEventListener(Event.REMOVED_FROM_STAGE,flyDie);
           for (var i = 0; i<flies.length; i++) {
               parent.removeChild(flies[i]);
           }
       }
Logged

Jaehl
Mobal Gloderator
Global Moderator
Veteran
***
Offline Offline

Gender: Male
Posts: 1418


/* No comment */


WWW
« Reply #13 on: December 16, 2009, 10:16:07 PM »

If the event listeners were registered to the stage (i.e. stage.addEventListener) then it would be a good idea to remove them, but as these ones are registered to the component, they stop existing when it's removed from the stage, so you don't have to worry about removing them manually. Smiley
Logged


<a href="http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf" target="_blank">http://i17.photobucket.com/albums/b90/Jaehll/Actionscript_Banner.swf</a><a href="http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/motm.swf?vTitle=oct%2Fnov%2007.swf</a>
Sarus translator:
<a href="http://www.jaehl.com/files/progress.swf" target="_blank">http://www.jaehl.com/files/progress.swf</a>
supermoose
Full Member
***
Offline Offline

Gender: Male
Posts: 240


Yes. I beat IWBTG. Only took ~60 hours of play.


« Reply #14 on: December 17, 2009, 02:05:19 AM »

ah.  righto then, carry on.  Grin
Logged

Brackenwood
   

 Logged
Pages: [1] 2   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.069 seconds with 25 queries.