#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Question Random animation + random sounds + background music


    Hi guys !
    I'm new to AS3, but I have to get this project done for class, and I can't really understand how to do it.
    I searched the forums and the internet, but did not find that much information. I found most of this code on the Internet though, and while I quite understand it, I don't master AS3 enough to be able to program it myself.
    Could you guys please help me a bit with this ?
    This is what I'm trying to achieve :

    - A background music looping in the back, that starts with the opening of the swf and never stops. I think I achieved that with this bit of code :

    Code:
    import flash.media.Sound;
    	import flash.net.URLRequest;
    	import flash.media.SoundChannel;
    	
    	// bg music
    	var music:Sound = new Sound(new URLRequest("music1.mp3"));  // same folder
    	var sc:SoundChannel;
    	
    	sc = music.play();
    One thing though : it would be cool to loop it, and I don't know how to do that.

    - I also need various small sound clips to be played randomly, above the aforementioned background music.I'm using this, which kinda works, although I'd like to have more control over the randomness. Ideally the clips should not play twice in a row. I don't know how to do that.

    Code:
    import flash.media.Sound;
    	import flash.net.URLRequest;
    	import flash.media.SoundChannel;
    	import flash.events.Event;
    	
    	var songs:Array=new Array("sound1.mp3","sound2.mp3");
    	var sound:Sound;
    	var soundChannel:SoundChannel;
    	var randnum:uint;
    	function playsong()
    	{
    	randnum=Math.floor(Math.random()*songs.length);
    	sound=new Sound();
    	sound.load(new URLRequest("sounds/"+songs[randnum]));
    	soundChannel=sound.play();
    	soundChannel.addEventListener(Event.SOUND_COMPLETE,playrandomsong);
    	}
    	function playrandomsong(e:Event)
    	{
    	playsong();
    	}
    	playsong();
    - Finally I need to jump from frame to frame randomly to play various animations. For example :
    Project starts, background music starts playing, we hear random sounds played one after the other, while some animations can be seen on screen, playing in a random order.
    I have no idea how to achieve that.

    Thank you for your help ! I'll be trying to find it out on my own, but any help will be appreciated.
  2. #2
  3. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Hi, to stop the sound, using the code you posted above you can use:

    sc.stop();

    You can use "sort" to shuffle the array, there's a bunch of ways to do that, I thought this one was nice and simple:
    Code:
    var songs:Array=new Array("sound1.mp3","sound2.mp3", "sound3.mp3", "sound4.mp3");
    songs.sort(function () {return Math.round(Math.random());});
    trace(songs);
    So then inside playsong you wouldn't have randnum, instead you would have a variable that you increment instead. You'll need to check for when you reach songs.length and then re-randomise the array and wrap back to 0 for your increment. You could also put an additional check in to make sure that the first song in the freshly randomised array is not the same as the last one you had before you wrapped around.

    You can use Math.random() to jump to different frames:

    some_movie_clip.gotoAndPlay(Math.floor(Math.random() * some_movie_clip.totalFrames));

    You might want to use gotoAndStop depending on what you are doing. You should check out the ActionScript LiveDocs as they tell you everything you need to know.

    Again though, you will probably want to put something in there to check if the same random animation just played and if it did to pick another one.
    Quis custodiet ipsos custodes?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Smile


    Thanks !

    Will look into it.

    Thanks a lot for your time.

IMN logo majestic logo threadwatch logo seochat tools logo