Search code examples
actionscript-3flashfunctiontween

stoping a yoyo() Tween within a function


I am wanting to stop this yoyo movement when a sound stops.

This is not all the code but the important parts:

I DECLARED THE VAR OUTSIDE OF THE FUNCTION:

var BB:Tween;

........

function BounceBeau()
{
    var BB:Tween = new Tween(Beau,"y",Strong.easeOut,stage.stageHeight - BeauHeight,33,5,false);
    BB.addEventListener(TweenEvent.MOTION_FINISH, PlayBB);
    function PlayBB(e:TweenEvent):void 
               {
        BB.yoyo();
           }

}

THE BOUNCE STARTS WITH SOUND AND STOPS WHEN OVER.

function PlaySound()
{

    var ThemeSong:SoundChannel;
    var s:Sound = new Sound(new URLRequest("MySound.mp3"));
    ThemeSong = s.play();

...

BounceBeau();

...

    ThemeSong.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); 
    function onPlaybackComplete(event:Event):void
        {
        BounceBeauStop();
        }
}

... THIS CODE WAS IN BUT I FORGOT TO POST IT

function BounceBeauStop()
{
BB.stop();
}

THE ERROR I GET: TypeError: Error #1009: Cannot access a property or method of a null object reference.

Any Thoughts. :)


Solution

  • In your BounceBeau(), change var BB:Tween to just BB. You are inadvertently shadowing the declaration of BB by redeclaring it locally.

    function BounceBeau()
    {
        BB = new Tween( ...
        ...