A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: as3 bubble animation (as3class) wont appear behind objects instead over them!!?

  1. #1
    Knowsnothingaboutnothing
    Join Date
    Dec 2008
    Location
    Guatemala
    Posts
    42

    as3 bubble animation (as3class) wont appear behind objects instead over them!!?

    I am using this code I got from a tutorial website:

    PHP Code:
    //The number of bubbles we will have on the stage
    const NUMBER_OF_BUBBLES:uint 60;
     
    //Save the stage width and height
    var stageWidth:Number stage.stageWidth;
    var 
    stageHeight:Number stage.stageHeight;
     
    //This array will contain all the bubbles
    var bubbles:Array = new Array();
     
    //This loop creates and positions the bubbles to random locations
    for (var i:uint 0NUMBER_OF_BUBBLESi++) {
     
        
    //Create a new bubble
        
    var bubble:Bubble = new Bubble();
     
        
    //Assign  a random y coordinate (from 0 to 300)
        
    bubble.Math.random() * stageHeight;
     
        
    //Calculate the horizontal distance from the mouse cursor to the center of the stage
        
    var distanceFromCenter:Number mouseX stageWidth 2;
     
        
    //Calculate the x coordinate (from -400 to 800)
        
    bubble.Math.random()*stageWidth-Math.random()*distanceFromCenter*2;
     
        
    //Assign a random scale
        
    bubble.scaleX bubble.scaleY Math.random() + 0.3;
     
        
    //Assign a random alpha
        
    bubble.alpha Math.random() + 0.5;
     
        
    //Assign a random current angle for the bubble
        
    bubble.currentAngle Math.random() * Math.PI 2;
     
        
    //Assign a random angle speed
        
    bubble.angleSpeed Math.random() * 0.1;
     
        
    //Assign a random radius for the bubble
        
    bubble.radius Math.random() * 5;
     
        
    //Assign a random y speed
        
    bubble.ySpeed =  -  Math.random() * 0.5;
     
        
    //Assign a random y acceleration speed
        
    bubble.acceleration =  -  Math.random() * 0.2;
     
        
    //Add the bubble to the bubbles array
        
    bubbles.push(bubble);
     
        
    //Add the bubble to the stage
        
    addChild(bubble);
    }
     
    //Add ENTER_FRAME listener for animating the bubbles
    addEventListener(Event.ENTER_FRAMEmoveBubbles);
     
    //This function is called in each frame
    function moveBubbles(e:Event):void {
     
        
    //Calculate the horizontal distance from the mouse cursor to the center of the stage
        
    var distanceFromCenter:Number mouseX stageWidth 2;
     
        
    //Calculate a new x speed for the bubbles
        
    var xSpeed:Number distanceFromCenter 32;
     
        
    //Loop through the bubbles array
        
    for (var i:uint 0bubbles.lengthi++) {
     
            
    //Save the bubble to a local variable
            
    var bubble:Bubble = (Bubble)(bubbles[i]);
     
            
    //Update the ySpeed of the bubble (accelerate)
            
    bubble.ySpeed += bubble.acceleration;
     
            
    //Update the y coorinate
            
    bubble.y+=bubble.ySpeed;
     
            
    //Update the current angle of the bubble
            
    bubble.currentAngle+=bubble.angleSpeed;
     
            
    //Update the x coordinate
            
    bubble.x+=Math.sin(bubble.currentAngle)*bubble.radius+xSpeed;
     
            
    //Check if the bubble is over the top of the stage
            
    if (bubble.y<0) {
     
                
    //Assign a new random x coordinate
                
    bubble.y=stageHeight;
                
    bubble.x=Math.random()*stageWidth-Math.random()*distanceFromCenter*2;
     
                
    //Assign a new random current angle
                
    bubble.currentAngle=Math.random()*Math.PI*2;
     
                
    //Assign a new random ySpeed
                
    bubble.ySpeed= -Math.random()*0.5;
            }
        }

    I went to linkage on the movieclip I created and assigned a class to it. Now it creates random bubbles across the screen, which I want. HOWEVER, they will not appear behind the layers over them. I can understand why - but is there a way to make them appear behind graphical elements on my page? sort of as a background??

  2. #2
    Knowsnothingaboutnothing
    Join Date
    Dec 2008
    Location
    Guatemala
    Posts
    42
    basically these bubbles appear in the screen but they appear OVER EVERYTHING. Is there a way to make them appear behind certain layers?

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Then you need to add your elements using addChild instead of placing them directly on the stage as hardcopies.
    Alternatively you use addChild(i,bubble);
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Knowsnothingaboutnothing
    Join Date
    Dec 2008
    Location
    Guatemala
    Posts
    42
    weird that it didnt sub me for this thread but I simply placed it behind the intended layers and the seemed to do the trick :S Thank you for your input though, as always on the spot and fast. I am grateful and admire your inclination to help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center