My favorites | Sign in
Project Logo
                
Changes to /trunk/src/com/newgrounds/as3/as3_collab_1/as3_collab_1.as
r5 vs. r7   Edit
  Compare: vs.   Format:
Revision r7
Go to: 
Sign in to write a code review
/trunk/src/com/newgrounds/as3/as3_collab_1/as3_collab_1.as   r5 /trunk/src/com/newgrounds/as3/as3_collab_1/as3_collab_1.as   r7
1 package // do not copy 1 package // do not copy
2 { // do not copy 2 { // do not copy
3 public class as3collab1 // do not copy 3 public class as3collab1 // do not copy
4 { // do not copy 4 { // do not copy
5 5
6 /* AS3 Code Collab! <http://www.newgrounds.com/bbs/topic/966360> 6 /* AS3 Code Collab! <http://www.newgrounds.com/bbs/topic/966360>
7 * 7 *
8 * This program is free software: you can redistribute it and/or modify 8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 12
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 17
18 * A copy of the GNU General Public License is available at: 18 * A copy of the GNU General Public License is available at:
19 * <http://www.gnu.org/licenses/>. 19 * <http://www.gnu.org/licenses/>.
20 */ 20 */
21 21
22 // === BEGIN INITIALIZATION ROUTINES === 22 // === BEGIN INITIALIZATION ROUTINES ===
23 stage.frameRate = 30; 23 stage.frameRate = 30;
24 const CIRCLE_RADIUS_MIN:int = 25; 24 const CIRCLE_RADIUS_MIN:int = 25;
25 const CIRCLE_RADIUS_MAX:int = 50; 25 const CIRCLE_RADIUS_MAX:int = 50;
26 const CIRCLE_SPD:int = 7; 26 const CIRCLE_SPD:int = 7;
27 const CIRCLE_CREATION_DELAY:int = 500; 27 const CIRCLE_CREATION_DELAY:int = 500;
28 const GRAVITY:int = 5; //Gravity applied to particles ***Added by Calipe*** 28 const GRAVITY:int = 5; //Gravity applied to particles ***Added by Calipe***
29 var particleVX:Number = 15; //Particle's max x velocity ***Added by Calipe*** 29 var particleVX:Number = 15; //Particle's max x velocity ***Added by Calipe***
30 var particleVY:Number = 50; //Particle's initial y velocity ***Added by Calipe*** 30 var particleVY:Number = 50; //Particle's initial y velocity ***Added by Calipe***
31 var maxParticles:int = 10; //Max number of particles ***Added by Calipe*** 31 var maxParticles:int = 10; //Max number of particles ***Added by Calipe***
32 var maxCircles:int = 50; 32 var maxCircles:int = 50;
33 var circles:Array = new Array(); //lets make a container so we can easily access the circles? 33 var circles:Array = new Array(); //lets make a container so we can easily access the circles?
34 var bgColor:int = 0x000000; //Background Color 34 var bgColor:int = 0x000000; //Background Color
35 35
36 36
37 var circleCreationTimer:Timer = new Timer(CIRCLE_CREATION_DELAY, 0); 37 var circleCreationTimer:Timer = new Timer(CIRCLE_CREATION_DELAY, 0);
38 circleCreationTimer.addEventListener(TimerEvent.TIMER, createCircleEvent, false, 10, false); 38 circleCreationTimer.addEventListener(TimerEvent.TIMER, createCircleEvent, false, 10, false);
39 circleCreationTimer.start(); 39 circleCreationTimer.start();
40 40
41 //Creates and adds the background to the display list ***Added by Calipe*** 41 //Creates and adds the background to the display list ***Added by Calipe***
42 42
43 var bg:Bitmap = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, false, bgColor)); 43 var bg:Bitmap = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, false, bgColor));
44 addChild(bg); 44 addChild(bg);
45 45
46 var textFormat:TextFormat = new TextFormat(); 46 var textFormat:TextFormat = new TextFormat();
47 47
48 //Modify with the desired format ***Added by Calipe*** 48 //Modify with the desired format ***Added by Calipe***
49 with (textFormat) 49 with (textFormat)
50 { 50 {
51 font = "Arial"; 51 font = "Arial";
52 size = "12"; 52 size = "12";
53 color = "0x0088FF"; 53 color = "0x0088FF";
54 bold = true; 54 bold = true;
55 } 55 }
56 56
57 var poppedCircles:int = 0; //Circle Counter Startup (Kajenx waz hur :3) 57 var poppedCircles:int = 0; //Circle Counter Startup (Kajenx waz hur :3)
58 var poppedCirclesCounter:TextField = new TextField; 58 var poppedCirclesCounter:TextField = new TextField;
59 addChild(poppedCirclesCounter); 59 addChild(poppedCirclesCounter);
60 poppedCirclesCounter.x = 10; 60 poppedCirclesCounter.x = 10;
61 poppedCirclesCounter.y = 10; 61 poppedCirclesCounter.y = 10;
62 poppedCirclesCounter.selectable = false; 62 poppedCirclesCounter.selectable = false;
63 poppedCirclesCounter.text = "Popped: 0"; 63 poppedCirclesCounter.text = "Popped: 0";
64 poppedCirclesCounter.setTextFormat(textFormat); 64 poppedCirclesCounter.setTextFormat(textFormat);
65 65
66 var mymenu:ContextMenu= new ContextMenu(); // context menu by **jaye19**
67 mymenu.hideBuiltInItems();
68 //Take that out if you dont want it to hide the default items of the right click menu
69 var txt:String='The AS3 Code Collab *Line By Line*';
70 //Change <strong>The AS3 Code Collab *Line By Line*</strong> to the text you wanna appear
71 var item:ContextMenuItem= new ContextMenuItem(txt);
72 mymenu.customItems.push(item);
73 this.contextMenu=mymenu;
74 item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,item_select);
66 //===END INITIALIZATION ROUTINES === 75 //===END INITIALIZATION ROUTINES ===
67 76
68 //===BEGIN EVENT HANDLERS === 77 //===BEGIN EVENT HANDLERS ===
69 function createCircleEvent(timerEvent:TimerEvent):void 78 function createCircleEvent(timerEvent:TimerEvent):void
70 { 79 {
71 var newCircle:Sprite = createCircleSprite(); 80 var newCircle:Sprite = createCircleSprite();
72 newCircle.x = Math.random()*(stage.stageWidth - newCircle.width); 81 newCircle.x = Math.random()*(stage.stageWidth - newCircle.width);
73 newCircle.y = - newCircle.height; 82 newCircle.y = - newCircle.height;
74 circles.push(newCircle); //Here we add the circles to the array 83 circles.push(newCircle); //Here we add the circles to the array
75 84
76 newCircle.addEventListener(Event.ENTER_FRAME, onCircleEnterFrame, false, 5, true); 85 newCircle.addEventListener(Event.ENTER_FRAME, onCircleEnterFrame, false, 5, true);
77 newCircle.addEventListener(MouseEvent.MOUSE_OVER, mouseOverListener, false, 0, true); //Registers the circle for the mouse over event ***Added by Calipe*** 86 newCircle.addEventListener(MouseEvent.MOUSE_OVER, mouseOverListener, false, 0, true); //Registers the circle for the mouse over event ***Added by Calipe***
78 87
79 addChild(circles[circles.length - 1]); 88 addChild(circles[circles.length - 1]);
80 swapChildren(poppedCirclesCounter, circles[circles.length -1]); //Keeps the counter on the top of the balls 89 swapChildren(poppedCirclesCounter, circles[circles.length -1]); //Keeps the counter on the top of the balls
81 90
82 } 91 }
83 92
84 // original content **PrettyMuchBryce** ( modified ) 93 // original content **PrettyMuchBryce** ( modified )
85 function onCircleEnterFrame(event:Event):void 94 function onCircleEnterFrame(event:Event):void
86 { 95 {
87 var currentCircle:Sprite = event.currentTarget as Sprite; 96 var currentCircle:Sprite = event.currentTarget as Sprite;
88 currentCircle.y += CIRCLE_SPD; 97 currentCircle.y += CIRCLE_SPD;
89 98
90 if ((currentCircle.y - currentCircle.height) > stage.stageHeight) 99 if ((currentCircle.y - currentCircle.height) > stage.stageHeight)
91 { 100 {
92 currentCircle.removeEventListener(Event.ENTER_FRAME, onCircleEnterFrame); // EXTREMELY IMPORTANT FOR GARBAGE COLLECTION 101 currentCircle.removeEventListener(Event.ENTER_FRAME, onCircleEnterFrame); // EXTREMELY IMPORTANT FOR GARBAGE COLLECTION
93 removeChild(currentCircle); 102 removeChild(currentCircle);
94 circles.splice(circles.indexOf(currentCircle), 1); 103 circles.splice(circles.indexOf(currentCircle), 1);
95 } 104 }
96 } 105 }
97 //The event listener function that is triggered when the mouse over event is dipatched ***Added by Calipe*** 106 //The event listener function that is triggered when the mouse over event is dipatched ***Added by Calipe***
98 function mouseOverListener(event:MouseEvent):void 107 function mouseOverListener(event:MouseEvent):void
99 { 108 {
100 109
101 popCircle(Sprite(event.target), mouseX, mouseY); 110 popCircle(Sprite(event.target), mouseX, mouseY);
102 } 111 }
103 112
104 //Particles enter frame listener ***Added by Calipe*** 113 //Particles enter frame listener ***Added by Calipe***
105 114
106 function particleEnterFrame(event:Event):void 115 function particleEnterFrame(event:Event):void
107 { 116 {
108 117
109 118
110 var tempParticle:MovieClip = MovieClip(event.target); 119 var tempParticle:MovieClip = MovieClip(event.target);
111 if ( tempParticle.y < stage.stageHeight) 120 if ( tempParticle.y < stage.stageHeight)
112 { 121 {
113 checkIfParticlePopsCircle(tempParticle); 122 checkIfParticlePopsCircle(tempParticle);
114 123
115 tempParticle.vy += GRAVITY; 124 tempParticle.vy += GRAVITY;
116 tempParticle.x += tempParticle.vx; 125 tempParticle.x += tempParticle.vx;
117 tempParticle.y += tempParticle.vy; 126 tempParticle.y += tempParticle.vy;
118 127
119 128
120 } 129 }
121 else 130 else
122 { 131 {
123 removeChild(tempParticle); 132 removeChild(tempParticle);
124 tempParticle.removeEventListener(Event.ENTER_FRAME, particleEnterFrame, false); 133 tempParticle.removeEventListener(Event.ENTER_FRAME, particleEnterFrame, false);
125 } 134 }
126 } 135 }
127 136
128 //===END EVENT HANDLERS === 137 //===END EVENT HANDLERS ===
129 138
130 //===BEGIN GENERAL FUNCTIONS=== 139 //===BEGIN GENERAL FUNCTIONS===
140 function item_select(e:ContextMenuEvent):void // context menu by **jaye19**
141 {
142 var url:String='newgrounds.com';
143 /*Here, change newgrounds.com to the site it will lead to or <strong>url:String-'newgrounds.com'</strong> to s code that you like when you click the item*/
144 var site:URLRequest=new URLRequest(url);
145 navigateToURL(site);
146 }
147
131 function createCircleSprite():Sprite 148 function createCircleSprite():Sprite
132 { 149 {
133 var radius:int = CIRCLE_RADIUS_MIN + Math.random()*(CIRCLE_RADIUS_MAX - CIRCLE_RADIUS_MIN); 150 var radius:int = CIRCLE_RADIUS_MIN + Math.random()*(CIRCLE_RADIUS_MAX - CIRCLE_RADIUS_MIN);
134 var tempCircle:Sprite = new Sprite(); 151 var tempCircle:Sprite = new Sprite();
135 152
136 tempCircle.graphics.lineStyle(); 153 tempCircle.graphics.lineStyle();
137 tempCircle.graphics.beginFill(Math.random()*0xFFFFFF); 154 tempCircle.graphics.beginFill(Math.random()*0xFFFFFF);
138 tempCircle.graphics.drawCircle(radius, radius, radius); 155 tempCircle.graphics.drawCircle(radius, radius, radius);
139 tempCircle.graphics.endFill(); 156 tempCircle.graphics.endFill();
140 157
141 return tempCircle; 158 return tempCircle;
142 } 159 }
143 // ** Ben-Fox ** random display object getter 160 // ** Ben-Fox ** random display object getter
144 function getRandomSprite(sourceArray:Array = null):Sprite { 161 function getRandomSprite(sourceArray:Array = null):Sprite {
145 if (sourceArray == null) { //If no array reference is passed to the function... 162 if (sourceArray == null) { //If no array reference is passed to the function...
146 //Choose any child on the stage to return. 163 //Choose any child on the stage to return.
147 return getChildAt(Math.floor(Math.random() * numChildren)) as Sprite; 164 return getChildAt(Math.floor(Math.random() * numChildren)) as Sprite;
148 } else { 165 } else {
149 //Return a reference from the specified array 166 //Return a reference from the specified array
150 return sourceArray[Math.floor(Math.random() * sourceArray.length)] as Sprite; //Just in case 167 return sourceArray[Math.floor(Math.random() * sourceArray.length)] as Sprite; //Just in case
151 } 168 }
152 } 169 }
153 170
154 // updated the circle pop, so mouse overs were not the only method for triggering them. ** deadlock32 ** 171 // updated the circle pop, so mouse overs were not the only method for triggering them. ** deadlock32 **
155 function popCircle(targetCircle:DisplayObject, centerX:Number, centerY:Number):void 172 function popCircle(targetCircle:DisplayObject, centerX:Number, centerY:Number):void
156 { 173 {
157 // Count popped circles 174 // Count popped circles
158 poppedCircles++; 175 poppedCircles++;
159 poppedCirclesCounter.replaceText(8, poppedCirclesCounter.length, String(poppedCircles)); //Be sure to change the first argument if the initial title ever changes 176 poppedCirclesCounter.replaceText(8, poppedCirclesCounter.length, String(poppedCircles)); //Be sure to change the first argument if the initial title ever changes
160 177
161 removeChild(targetCircle); 178 removeChild(targetCircle);
162 circles.splice(circles.indexOf(targetCircle), 1); 179 circles.splice(circles.indexOf(targetCircle), 1);
163 targetCircle.removeEventListener(Event.ENTER_FRAME, onCircleEnterFrame); 180 targetCircle.removeEventListener(Event.ENTER_FRAME, onCircleEnterFrame);
164 createParticles(centerX, centerY); 181 createParticles(centerX, centerY);
165 } 182 }
166 183
167 //Creates the particles ***Added by Calipe*** 184 //Creates the particles ***Added by Calipe***
168 185
169 function createParticles(centerX:Number, centerY:Number):void 186 function createParticles(centerX:Number, centerY:Number):void
170 { 187 {
171 for (var i:int = 0; i < maxParticles; i++) 188 for (var i:int = 0; i < maxParticles; i++)
172 { 189 {
173 var particle:MovieClip = new MovieClip(); 190 var particle:MovieClip = new MovieClip();
174 particle.graphics.beginFill(Math.random() * 0xFFFFFF); 191 particle.graphics.beginFill(Math.random() * 0xFFFFFF);
175 particle.graphics.drawCircle(0, 0, Math.random() * 10 + 5); 192 particle.graphics.drawCircle(0, 0, Math.random() * 10 + 5);
176 particle.vx = Math.random() * (particleVX*2) - (particleVX); 193 particle.vx = Math.random() * (particleVX*2) - (particleVX);
177 particle.vy = Math.random() * -particleVY; 194 particle.vy = Math.random() * -particleVY;
178 particle.x = centerX; 195 particle.x = centerX;
179 particle.y = centerY; 196 particle.y = centerY;
180 particle.addEventListener(Event.ENTER_FRAME, particleEnterFrame, false, 0, true); 197 particle.addEventListener(Event.ENTER_FRAME, particleEnterFrame, false, 0, true);
181 addChild(particle); 198 addChild(particle);
182 } 199 }
183 } 200 }
184 201
185 // added a function to check if a particle is touching another circle 202 // added a function to check if a particle is touching another circle
186 // by deadlock32 203 // by deadlock32
187 function checkIfParticlePopsCircle(targetParticle:DisplayObject):void 204 function checkIfParticlePopsCircle(targetParticle:DisplayObject):void
188 { 205 {
189 var targetParticleRadius:Number = targetParticle.width * 0.5; 206 var targetParticleRadius:Number = targetParticle.width * 0.5;
190 for each(var tempCircle:DisplayObject in circles) 207 for each(var tempCircle:DisplayObject in circles)
191 { 208 {
192 var circleRadius:Number = tempCircle.width * 0.5; 209 var circleRadius:Number = tempCircle.width * 0.5;
193 var collisionDistanceMax:Number = targetParticleRadius + circleRadius; 210 var collisionDistanceMax:Number = targetParticleRadius + circleRadius;
194 211
195 var distanceApart:Number = Point.distance(new Point(targetParticle.x, targetParticle.y), new Point(tempCircle.x, tempCircle.y)); 212 var distanceApart:Number = Point.distance(new Point(targetParticle.x, targetParticle.y), new Point(tempCircle.x, tempCircle.y));
196 213
197 if(distanceApart <= collisionDistanceMax) 214 if(distanceApart <= collisionDistanceMax)
198 { 215 {
199 popCircle(tempCircle, tempCircle.x, tempCircle.y); 216 popCircle(tempCircle, tempCircle.x, tempCircle.y);
200 break; 217 break;
201 } 218 }
202 // the above was 100%... for some reason if you wanna figure out why go for it 219 // the above was 100%... for some reason if you wanna figure out why go for it
203 // I added the below to help compensate 220 // I added the below to help compensate
204 if(tempCircle.hitTestPoint(targetParticle.x, targetParticle.y, true)) 221 if(tempCircle.hitTestPoint(targetParticle.x, targetParticle.y, true))
205 { 222 {
206 popCircle(tempCircle, tempCircle.x, tempCircle.y); 223 popCircle(tempCircle, tempCircle.x, tempCircle.y);
207 break; 224 break;
208 } 225 }
209 } 226 }
210 } 227 }
211 // === END GENERAL FUNCTIONS === 228 // === END GENERAL FUNCTIONS ===
212 229
213 } // do not copy 230 } // do not copy
214 } // do not copy 231 } // do not copy
Hosted by Google Code