Jump to content
Search Community

Adding sound effects to a tweenlite animation

barredow test
Moderator Tag

Recommended Posts

I'm new to greensock trying to create a simple animation with three circles (yellow, green and red) hitting the right side of the wall and making a sound after each of them they hit. So far I haven't had any problems with the animation but the sound has been really hard. I've looked at several similar posts, read the documentation and also searched online. Any help would be greatly appreciated. - Alex

 

 

 

here's my code:

 

import com.greensock.*;

import com.greensock.easing.*;

import com.greensock.plugins.*;

import com.greensock.OverwriteManager;

import com.greensock.events.LoaderEvent;

import com.greensock.loading.*

import flash.media.Sound;

import flash.media.SoundChannel;

 

 

var loader:MP3Loader = new MP3Loader("sound.mp3");

loader.load();

 

OverwriteManager.init(2)

var clips:Array = [yellow, red, green, rectangle ];

 

TweenMax.allTo(clips, 0, {autoAlpha:0});

 

green.mask = rectangle

 

 

var timeline:TimelineMax = new TimelineMax({repeat:-1, delay:2.5});

 

TweenLite.to(green, 1, {x:-11, y:-7});

timeline.append(TweenLite.to(green, 6, {autoAlpha:1}));

timeline.append(TweenLite.to(green, 1.7, {x:412.95, y:-7}));

//add sound

 

TweenLite.to(yellow, 1, {x:-12, y:134});

timeline.append(TweenLite.to(yellow, 6, {autoAlpha:1}));

timeline.append(TweenLite.to(yellow, 1.7, {x:412.95, y:134}));

//add different sound

 

TweenLite.to(red, 1, {x:0, y:269});

timeline.append(TweenLite.to(red, 6, {autoAlpha:1}));

timeline.append(TweenLite.to(red, 1.7, {x:412.95, y:269, onComplete:testFunction}));

 

 

function testFunction(event:LoaderEvent):void {

var sound:Sound = loader.content;

var channel:SoundChannel = sound.play(0, 1);

}

Link to comment
Share on other sites

something like this should work fine:

 

var sound:MP3Loader = new MP3Loader("sound.mp3",  {onComplete:init, autoPlay:false});

sound.load();

//set up the timelinemax
var tl:TimelineMax = new TimelineMax({paused:true})

tl.append(TweenLite.to(mc, 2, {x:589}));
tl.addCallback(playSound, tl.duration);


//wait for the sound to load
function init(e:LoaderEvent):void
{
tl.play();
}


function playSound():void{
sound.gotoSoundTime(0, true);
}

 

you could also use playSound as an onComplete in a tween like you had, but make sure the callback function is not expecting a LoaderEvent.

  • Like 1
Link to comment
Share on other sites

Thanks for your help Carl. I tweaked the code based on your suggestions but I'm still having problems. Is there a class that I'm not importing correctly? Would you mind looking at it again? I keep getting 1046 errors and 3590 errors.

 

 

 

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
import com.greensock.OverwriteManager;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*
import flash.media.Sound;
import flash.media.SoundChannel;


OverwriteManager.init(2)
var clips:Array = [yellow, red, green, rectangle ];

TweenMax.allTo(clips, 0, {autoAlpha:0});

green.mask = rectangle

//load two sounds
var sound1:MP3Loader = new MP3Loader("sound.mp3", {autoPlay:false});
var sound2:MP3Loader = new MP3Loader("sound2.mp3", {autoPlay:false});

var queue:LoaderMax = new LoaderMax({onComplete:init});

queue.append(sound1);
queue.append(sound2);
queue.load();


//set up the timelinemax
var tl:TimelineMax = new TimelineMax({paused:true})

//play sound1 after green circle appears
tl.append(TweenLite.to(green, 3, {autoAlpha:1}));
tl.addCallback(playSound1, tl.duration);

//yellow circle appears no sound
tl.append(TweenLite.to(yellow, 3, {autoAlpha:1}));

//play sound2 after red appears
tl.append(TweenLite.to(red, 3, {autoAlpha:1}));
tl.addCallback(playSound2, tl.duration);

//wait for the sound to load
function init(e:LoaderEvent):void
{
 trace("soundsComplete"); 
 tl.play();
}

function playSound():void{
  sound1.gotoSoundTime(0, true);
  }

  function playSound2():void{
  sound2.gotoSoundTime(0, true);
  }

Link to comment
Share on other sites

I don't know what those errors are. please post the complete error. it will either tell you what line of code is the problem or what function the error is caused by.

 

nothing jumped out after a quick glance. best bet is to zip you files and attach them.

Link to comment
Share on other sites

the big problem is that you are using an old and incomplete version of the greensock files.

you have none of the LoaderMax classes.

 

Go to http://www.greensock.com/tweenlite and download the latest version.

copy the com folder to the same folder as your fla

 

second: your addCallback function doesn't match the name of the function you want to reference

 

you have

 

tl.addCallback(playSound1, tl.duration);

 

but your method is called playSound() (you are missing the 1)

 

function playSound():void{

sound1.gotoSoundTime(0, true);

}

 

make sure those names match, use playSound1 or playSound in BOTH cases.

 

after that I couldn't test anymore as you didn't provide the sound files. I think that will get you pretty close though.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...