# AS3 Bubbles

**URL:** <https://forum.kirupa.com/t/as3-bubbles/261513>\
**Category:** flash\
**Created:** [May 28, 2008, 11:40am UTC](https://forum.kirupa.com/t/as3-bubbles/261513 "2008-05-28T11:40:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 28, 2008, 11:40am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/1 "2008-05-28T11:40:00Z")

</div>

hi.

im reletively new to actionscript and i wanted to try a simple animation where bubbles come out of the mouth of a fish (floating upwards), which you can pop with your pin cursor.

i prefer to do it in an external actionscript file(.as) rather than in the timeline.

could anyone help me?

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 1:49am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/2 "2008-05-29T01:49:14Z")

</div>

i found this code but instead of making bubbles when u click the mouse i wanted the bubbles to come up randomly along the x axis.

any help?

stage.addEventListener(MouseEvent.MOUSE\_DOWN, addBubble);  
var pin:mcpin = new mcpin();  
function addBubble(event:MouseEvent):void  
{  
var newBubble:mcBubble = new mcBubble();  
newBubble.x = mouseX;  
newBubble.y = mouseY;  
newBubble.scaleX = newBubble.scaleY = Math.random();  
//add a custom property (floatFactor) to newBubble:  
newBubble.floatFactor = Math.floor(Math.random() \* 2 + 1);  
newBubble.addEventListener(Event.ENTER\_FRAME, floatAndTestForHit);  
addChild(newBubble);  
}  
stage.addEventListener(KeyboardEvent.KEY\_DOWN, addPin);  
function addPin(event:KeyboardEvent):void  
{  
//disallow adding any more bubbles and pins once a key is pressed:  
stage.removeEventListener(MouseEvent.MOUSE\_DOWN, addBubble);  
stage.removeEventListener(KeyboardEvent.KEY\_DOWN, addPin);  
//add the pin to the stage and move it to the mouse coordinates:  
addChild(pin);  
pin.x = mouseX;  
pin.y = mouseY;  
pin.startDrag(true);  
}  
function floatAndTestForHit(event:Event):void  
{  
//float:  
event.target.y -= event.target.floatFactor;  
//test for hit with pin:  
if(event.target.hitTestPoint(pin.x - pin.width / 2, pin.y, true))  
{  
event.target.removeEventListener(Event.ENTER\_FRAME, floatAndTestForHit);  
event.target.parent.removeChild(event.target);  
}  
}

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 3:17am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/3 "2008-05-29T03:17:29Z")

</div>

i tried it myself but this is all i could get. can anyone help me?  
it doesnt work with the .fla file  
the bubble is called mcBubble in the library and the pin cursor is named mcPin

???

package {

import flash.display.MovieClip;  
import flash.events.\*;

public class main extends MovieClip {  
var holder:MovieClip = this.createEmptyMovieClip(“holder”, this.getNextHighestDepth());  
var pin:mcPin = new mcPin();  
holder.maxBubbles=60;  
var upspeed=80;

public function main() {  
var newBubble:mcBubble = new mcBubble();  
for(i=0; i\<holder.maxBubbles; i++) {  
holder.attachMovie(“mcBubble”, “bubble”+i, i+5);  
newBubble=holder[“bubble”+1]  
newBubble.x = random(Stage.width);  
newBubble.y = random(Stage.height);  
newBubble.scaleX = newBubble.scaleY = Math.random();  
newBubble.floatFactor = Math.floor(Math.random() \* 2 + 1);  
newBubble.addEventListener(Event.ENTER\_FRAME, floatAndTestForHit);  
addChild(newBubble);  
}  
}

var bubbleup=setInterval(morebubble, 80)  
public function morebubble() {  
for(i=0; i\<holder.maxBubbles; i++) {  
newBubble=holder[“bubble”+1]  
newBubble.\_y=upspeed/newBubble.\_alpha;  
newBubble.\_x=Math.sin(newBubble.\_y/10);  
if(newBubble.\_y\<=-20){  
newBubble.\_y=Stage.height;  
newBubble.\_x=random(Stage.width);  
}  
}  
}

public function addPin() {  
addChild(pin);  
pin.x = mouseX;  
pin.y = mouseY;  
pin.startDrag(true);  
}

public function floatAndTestForHit(event:Event) {  
event.target.y -= event.target.floatFactor;

if(event.target.hitTestPoint(pin.x - pin.width / 2, pin.y, true)) {  
event.target.removeEventListener(Event.ENTER\_FRAME, floatAndTestForHit);  
event.target.parent.removeChild(event.target);  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Gathan](https://avatars.discourse-cdn.com/v4/letter/g/df705f/32.png) [@Gathan](https://forum.kirupa.com/u/Gathan)\
**Post date:** [May 29, 2008, 3:50am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/4 "2008-05-29T03:50:55Z")

</div>

Hmm your using deprecated methods that don’t exist in AS3, how is that even compiling for you:stare:

---

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [May 29, 2008, 4:38am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/5 "2008-05-29T04:38:04Z")

</div>

try this,

put this code in your ‘bubble class’

```auto
package {

    import flash.display.*;
    import flash.events.*;
    public class Bubble extends Sprite {

        var angle:Number;
        var randomSpeed:Number
        var theParent:Object

        public function Bubble(theParent) {
            theParent = theParent;
            angle = Math.random() * 140 + 200;
            randomSpeed = Math.random() * 10 + 10;
            
            this.x = theParent.stage.stageWidth/2;
            this.y = theParent.stage.stageHeight-100;
            addEventListener(Event.ENTER_FRAME,floatUp);
        }
        public function floatUp(evt:Event) {
            var radian:Number = angle * Math.PI/180;
            var vx:Number = Math.cos(radian) * randomSpeed;
            var vy:Number = Math.sin(radian) * randomSpeed;
            this.x += vx;
            this.y += vy;
            
            
        }
    }
}

```

and put this in your main class

```auto
package{
    
    import flash.display.*;
    import flash.events.*;
    
    public class Main extends MovieClip{
        
        private var myBubble:Bubble;
        
        public function Main(){
            
            for(var i:int = 0; i < 100; i++){
                myBubble = new Bubble(this);
                addChild(myBubble);
                
                }
            
            }
        }
    }

```

Sorry, I ddin’t have time to comment, don’t forget to remove your event listeners

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 11:13am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/6 "2008-05-29T11:13:53Z")

</div>

wow thank you. im going through the code now. however when i test the movie it doesnt work. do you have any ideas why this may be

(thank you thank you thank you :party:)

---

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [May 29, 2008, 12:31pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/7 "2008-05-29T12:31:38Z")

</div>

Couple things come to mind:

1. make sure you link your bubble movie clip to the \*\*Bubble \*\*class. So in you .fla file, right click on your movie symbol in the _library_, hit \*linkage \*and type \*\*Bubble \*\*in the Class name
2. make you specify you document class as **Main** , hit CTR-F3 in your main .fla file
3. make sure all your files are in the same folder, try tracing and see if you can locate the problem  
4 all together you should have three files, **Main.fla, Main.as,** and \*\*Bubble.as  
\*\*  
let me know if this helps

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 12:45pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/8 "2008-05-29T12:45:55Z")

</div>

helped heaps thank you!  
im just getting a few errors out and it should be fine. thank you!

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 4:26pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/9 "2008-05-29T16:26:45Z")

</div>

i have an error stating that there is an incorrect number of arguments. expected 1.  
the code is:

private var myBubble = new Bubble();

do you know what should be there?

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 4:37pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/10 "2008-05-29T16:37:08Z")

</div>

ok so this is what’s happening. i have three classes (and three seperate .as files)

main.as  
\<as\>  
package{

import flash.display._;  
import flash.events._;

public class main extends MovieClip{

private var myBubble = new Bubble();

public function main(){

for(var i:int = 0; i \< 100; i++){  
addChild(myBubble);  
}  
}  
}  
}  
\</as\>

Bubble class

\<as\>  
package{

import flash.display._;  
import flash.events._;

public class Bubble extends Sprite {  
var angle:Number;  
var randomSpeed:Number  
var theParent:Object  
var myPin = new Pin();  
public function Bubble(theParent) {  
theParent = theParent;  
angle = Math.random() \* 140 + 200;  
randomSpeed = Math.random() \* 10 + 10;  
this.x = theParent.stage.stageWidth/2;  
this.y = theParent.stage.stageHeight-100;  
addEventListener(Event.ENTER\_FRAME,floatUp);  
addEventListener(Event.ENTER\_FRAME,floatAndTestForHit);  
}  
public function floatUp(evt:Event){  
var radian:Number = angle \* Math.PI/180;  
var vx:Number = Math.cos(radian) \* randomSpeed;  
var vy:Number = Math.sin(radian) \* randomSpeed;  
this.x += vx;  
this.y += vy;  
}  
public function floatAndTestForHit(evt:Event){  
evt.target.y -= evt.target.myBubble;  
if(evt.target.hitTestPoint(myPin.x - myPin.width / 2, myPin.y, true)){  
evt.target.removeEventListener(Event.ENTER\_FRAME,floatUp);  
evt.target.removeEventListener(Event.ENTER\_FRAME,floatAndTestForHit);  
evt.target.parent.removeChild(evt.target);  
}  
}  
}  
}  
\</as\>

and Pin Class

\<as\>  
package{

import flash.display._;  
import flash.events._;

public class Pin extends MovieClip{  
var myPin = new Pin();

public function addPin(){  
addChild(myPin);  
myPin.x = mouseX;  
myPin.y = mouseY;  
myPin.startDrag(true);  
}  
}  
}  
\</as\>

at the moment. it’s coming up with an error saying incorrect number of arguments. Expecting 1 with private var myBubble = new Bubble(); in the main.as class.

can anyone help me… or fix up the code. what ever i do creates more and more errors.  
:S

---

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [May 29, 2008, 5:32pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/11 "2008-05-29T17:32:23Z")

</div>

do this: myBubble = [COLOR=#000000] **new** [/COLOR] BubbleCOLOR=#000000[/COLOR];

check the code I posted, ‘this’ simply refers to the “Main”, you pass it in as an argument so that you can refer to it in the Bubble class

---

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [May 29, 2008, 5:33pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/12 "2008-05-29T17:33:03Z")

</div>

“Main” class I mean

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 5:38pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/13 "2008-05-29T17:38:40Z")

</div>

thank you. that worked of course lol silly me.  
what does this error mean?

error #1023: Stack overflowed occurred.

it goes on to list at Pin$iinit() repeatedly

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 5:42pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/14 "2008-05-29T17:42:42Z")

</div>

.

---

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [May 29, 2008, 6:11pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/15 "2008-05-29T18:11:39Z")

</div>

> **[Flash/Flex AS3 Error #1023: Stack overflow occurred.](http://curtismorley.com/2007/08/19/flashflex-as3-error-1023-stack-overflow-occurred/)**
>
> Error #1023: Stack overflow occurred at MyClass/get id() Description: This will happen if the stack has so much data that it is full. Nothing like Thanksgiving dinner to overflow my stack.  What is…

---

<div class="post-metadata">

**Author:** ![opul777](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@opul777](https://forum.kirupa.com/u/opul777)\
**Post date:** [May 29, 2008, 8:43pm UTC](https://forum.kirupa.com/t/as3-bubbles/261513/16 "2008-05-29T20:43:46Z")

</div>

heya,  
it’s nearly complete. theres just a problem of the pin cursor not working.  
if anyone wants to look at it feel free because i’ve tried everything but whats right.  
here’s code again.

main class

\<as\>  
package{

import flash.display._;  
import flash.events._;

public class main extends MovieClip{

private var myBubble:Bubble;

public function main(){  
for(var i:int = 0; i \< 15; i++){  
myBubble = new Bubble(this);  
addChild(myBubble);  
}  
}  
}  
}

\</as\>

bubble class

\<as\>  
package {  
import flash.display._;  
import flash.events._;  
public class Bubble extends Sprite {  
var angle:Number;  
var randomSpeed:Number;  
var theParent:Object;  
public function Bubble(theParent) {  
theParent = theParent;  
angle = Math.random() \* 140 - 200;  
randomSpeed = Math.random() \* 10;  
this.x = theParent.stage.stageWidth/2.5;  
this.y = theParent.stage.stageHeight-150;  
this.scaleX = this.scaleY = Math.random();  
addEventListener(Event.ENTER\_FRAME,floatUp);  
}  
public function floatUp(evt:Event) {  
var radian:Number = angle \* Math.PI/400;  
var vx:Number = Math.ceil(radian) \* randomSpeed;  
var vy:Number = Math.sin(radian) \* randomSpeed;  
this.x += vx;  
this.y += vy;  
}  
}  
}  
\</as\>

Pin class

\<as\>  
package{

import flash.display._;  
import flash.events._;

public class Pin extends MovieClip{  
var mypin = new Pin();

public function addPin(evt:Event){

addEventListener(MouseEvent.MOUSE\_DOWN,addPin);  
addChild(mypin);  
mypin.x = mouseX;  
mypin.y = mouseY;  
mypin.startDrag(true);

stage.removeEventListener(MouseEvent.MOUSE\_DOWN, addPin);

}

public function floatUp(evt:Event) {  
evt.target.y -= evt.target.theParent;  
if (evt.target.hitTestPoint(mypin.x - mypin.width / 2, mypin.y, true)) {  
evt.target.removeEventListener(Event.ENTER\_FRAME,floatUp);  
evt.target.theParent.removeChild(evt.target);  
}  
}  
}  
}  
\</as\>

🙂

---

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [May 30, 2008, 4:07am UTC](https://forum.kirupa.com/t/as3-bubbles/261513/17 "2008-05-30T04:07:26Z")

</div>

I could be wrong but I don’t think you can add an object to itself

var myPin = new Pin() inside the Pin class might not work, try putting var myPin = new Pin() in the Main parent class
