开发者

A hundred thousand Ozzy's in AS3

I am making a Flash game with a bird as the avatar. And when the bird picks up an Iron Bird pickup, the song Iron Man is played. But the problem is: It keeps making new instances of this audio. So I have like a hundred thousand Ozzy's singing the song. But they have a delay of a second between each other. Is there a certain function to play the song only one time? Or a small script to get this done?

This is my script:

var IronManMusic:Sound = new IronManSong();
var IronManChannel:SoundChannel = new SoundChannel();

var backgroundMusic:Sound = new Backgroun开发者_如何学运维dMusic(); 
var myChannel:SoundChannel = new SoundChannel();
myChannel = backgroundMusic.play();

 if(ironbird==true){
  laatstejump = 9;
  myChannel.stop();
  IronManChannel = IronManMusic.play();
 }

The ironbird boolean is found in a Updatescreen function. I think this is the problem but I am not sure. Help is really appreciated! :)


Hm... looks like it should be:

if(ironbird==true) {
  [...]
  ironbird = false;
}

Or at least the Updatescreen function you mentioned should set ironbird back to false after one tick/iteration.

EDIT: Looking at the code you posted as an answer, it's even easier:

You're doing

if(birdie.hitTestPoint(ironbirdpickup.x,ironbirdpickup.y,true)){
    ironbird = true;
    ironbirdpickup = Null;
}

if(ironbird==true){
    laatstejump = 9;
    myChannel.stop();
    IronManChannel = IronManMusic.play();
}

and use the ironbird variable for animation and other things. Moving the sound trigger should only trigger the sound once (or as often as hitTestPoint is triggered).

if(birdie.hitTestPoint(ironbirdpickup.x,ironbirdpickup.y,true)){
    ironbird = true;
    ironbirdpickup = Null;
    myChannel.stop();
    IronManChannel = IronManMusic.play();
}

if(ironbird==true){
    laatstejump = 9;
}

You should still reset ironbird to false somewhere, f.e. when animation is done.


Dont know what you exactly mean with the audio and animation part, but here is my script.

//loads keyboard events
import flash.events.KeyboardEvent

var laatstelook:int = 1;
var ironbird:Boolean = false;
var laatstejump = 1;
var movebirdie:int = 2;
var birdfatigue : Boolean = false;
var birdtellery:int = 0;
var birdtelleryvertrager = 0;
var vy:Number=0;
var movement:Boolean=false;
var springen:Boolean=false;
var gv:Number=0.1;
var keyArray:Array = new Array();
var i:Number;
var leftOrrightpressed:Number=0;
var platformraak=false;
var PlatformArray:Array = new Array();

var IronManMusic:Sound = new IronManSong();
var IronManChannel:SoundChannel = new SoundChannel();

var backgroundMusic:Sound = new BackgroundMusic(); 
var myChannel:SoundChannel = new SoundChannel();
myChannel = backgroundMusic.play();

for(i=0;i<222;i++){
    keyArray.push([i,false]);
}
//creating multiple objects from 1 object
for (var a:int = numChildren - 1; a >= 0; a--){
    var child:DisplayObject = getChildAt(a);
    if (child.name == "platform"){
        PlatformArray.push(child);
    }
}

stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
this.addEventListener(Event.ENTER_FRAME, UpdateScreen);
function UpdateScreen(event:Event):void{

    //No multiple jumps in air
    if(springen==true){

        birdie.gotoAndStop(laatstejump);
        birdtelleryvertrager+=0.2;
        birdie.y-=15;
        birdie.y+=birdtelleryvertrager;
        birdtellery++;

        if(isKeyDown(39)==true){
            birdie.x += 5;
            background.x += 1;
            vcam.x += 5;
            if(birdie.x <= 261.95){
                vcam.x = 275.5;
                background.x -= 1;
            }
                if(ironbird != true){
                    birdie.gotoAndStop(5);
                }else{birdie.gotoAndStop(13);}
        }

        if(isKeyDown(37)==true){
            birdie.x-=5;
            vcam.x -= 5;
            background.x -= 1;
            if(birdie.x <= 261.95){
                vcam.x = 275.5;
                background.x += 1;
            }
            birdie.gotoAndStop(6);
        }

        //bird falls down
        if(birdtellery>25){
            birdfatigue == true;
            if(isKeyDown(39)==true){
                birdie.x-=4;
                vcam.x -= 4;
            }

            if(isKeyDown(37)==true){
                birdie.x += 4;
                vcam.x += 4;
            }

            birdtelleryvertrager+=0.8;
        }

        //bird can jump again (when on platform)
        if(birdtellery>30){
            springen=false;
        }

    }else{
            if(birdfatigue == true){
                birdie.gotoAndStop(7);
            }

            //if right arrow button is pressed
            if(isKeyDown(39)==true){
                birdie.x += movebirdie;

                if(birdie.x >= 261.95){
                    vcam.x += movebirdie;
                    background.x += 0.5;
                }
                if(ironbird != true){
                    birdie.gotoAndStop(2);
                    laatstelook =1;
                }else{birdie.gotoAndStop(11);
                        laatstelook =9;
                    }

                laatstejump =5;
                leftOrrightpressed =1;

            }

            if(isKeyDown(39)==false){
                if (leftOrrightpressed ==1){
                    birdie.gotoAndStop(laatstelook);
                }

            }

            //if left arrow button is pressed
            if(isKeyDown(37)==true){

                birdie.x -= movebirdie;
                background.x -= 0.5;
                vcam.x -= movebirdie;
                if(birdie.x <= 261.95){
                    vcam.x = 275.5;
                    background.x += 0.5;
                }
                if(ironbird != true){
                    birdie.gotoAndStop(3);
                    laatstelook =4;
                }else{birdie.gotoAndStop(12);
                        laatstelook = 10;
                    }

                laatstejump=6;
                leftOrrightpressed =2;
            }

            if(isKeyDown(37)==false){
                if (leftOrrightpressed ==2){
                    birdie.gotoAndStop(laatstelook);
                }
            }

            //if space button is pressed
            if(isKeyDown(32)==true && springen==false){
                springen=true;  
            }

        }

    if(birdie.hitTestPoint(ironbirdpickup.x,ironbirdpickup.y,true)){
        ironbird = true;
        ironbirdpickup = Null;
    }

    if(ironbird==true){
        laatstejump = 9;
        myChannel.stop();
        IronManChannel = IronManMusic.play();
    }



    addEventListener(Event.ENTER_FRAME,ctrl_birdie);


    function ctrl_birdie(e:Event){

        //when bird touches one of the platforms, bird stops falling
        for(var a in PlatformArray){
            if(PlatformArray[a].hitTestPoint(birdie.x,birdie.y,true)){
                birdtelleryvertrager=0;
                birdtellery = 0;
                birdie.y-=1;
            }
        }
    }

    if(birdie.hitTestPoint(platform.platformboundingbox.x,platform.platformboundingbox.y,true)){
        trace("hit");
    }

    //gravity
    vy = 10;
    birdie.y+=vy;
}

//checks if certain key is pressed
function checkKeysDown(event:KeyboardEvent):void{
    keyArray[event.keyCode][1]=true;
}
function checkKeysUp(event:KeyboardEvent):void{
    keyArray[event.keyCode][1]=false;
}
function isKeyDown(X){
    return keyArray[X][1];
}

I am a Dutch so some variables are strange. xD Just ask if you dont get it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜