AS3 Bubbles

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?

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);
}
}

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);
}
}
}
}

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

try this,

put this code in your ‘bubble class’

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

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

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:)

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

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

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?

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

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

“Main” class I mean

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

.

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>

:slight_smile:

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