0
votes

I'm making a quiz type animation for work where on clicking an answer it plays a short animation FLV file relating to what you picked. As everything I read points towards AS3 being OOP I decided to make a MovieClip containing an FLV player and linked it to an AS3 file called FLV_Player.as. That way I can create a new instance of the FLV_Player everytime I need to play a video. Here is the code in that file which seems to work fine:

package 
   {
import fl.video.VideoEvent;
import flash.events.VideoEvent;
import flash.display.MovieClip;

public class FLV_Player extends MovieClip
{

    public function FLV_Player(NextVideo:String)
    {
        animation_player.source=(NextVideo);
        animation_player.addEventListener(VideoEvent.COMPLETE, vcompleted);
    }

    private function vcompleted(e:VideoEvent):void
    {
        nextFrame();
    }

}

}

Now in the DocumentClass.as file I have this code:

private function NewVideo(videoname:String)
    {
        var nextvideo:FLV_Player = new FLV_Player(videoname);
        addChild(nextvideo);
        nextvideo.x = 0;
        nextvideo.y = 0;
    }

So when you click a button, go to the next frame or whatever the prompt is, it calls the NewVideo function and passes the name of whatever video is to be played next.

NewVideo("Introduction.flv");

Now I'm sure I'm going to run in to other issues later down the line as I really have no idea whether anything I've done is how it should be done, but the only issue I seem to be having at this point in time is removing the video and going to the next (or previous) frame to answer another question. I tried:

nextFrame();    
removeChild(newVideo);

But it didn't work. Well, it may have gone to the next frame but with the video taking up the whole window it's hard to see if it did or not.

So how do I remove the video I've created? The main issue seems to be that because I had to create a new instance of the FLV_Player class in a private function the child is defined locally "var", rather than "public" or "private" var so I can't reference it again. It tells me that you can only create a "private var" from within the document class but if I make it there it will create the class on load rather than from the function when I'm ready to pass the video name parameter to it. At load I don't know what video I need it to play?

2
You can declare the variable outside of your private function without initialising it (so just private var nextvideo:FLV_Player;), then within NewVideo() simply set it: nextvideo = new FLV_Player(videoname);.David Mear

2 Answers

1
votes

removeChild() must be called from the same object in which it was added. In this case, your DocumentClass. What you're trying to do now is telling an FLV_Player to remove itself, which won't work due to several reasons and bugs in your code.

The correct way to do things would be to have the FLV_Player object dispatch a custom event that your DocumentClass listens for. You need to create a new class which inherits from Event to create your custom event. I'd call it "PlayerEvent". In DisplayClass function you'd do this:

nextVideo.addEventListener(PlayerEvent.PLAYBACK_FINISHED, onPlaybackFinished);
addChild(nextVideo);

Then you need to create the onPlaybackFinished method:

private function onPlaybackFinished(event:PlayerEvent):void {
    nextVideo.removeEventListener(PlayerEvent.PLAYBACK_FINISHED, onPlaybackFinished);
    removeChild(nextVideo);
}

Inside the FLV_Player class, the vcomplete function should change to:

dispatchEvent(new Event(PlayerEvent.PLAYBACK_FINISHED));

Alternately, you could pass a pointer of the DocumentClass to the FLV_Player object, but this is very messy, can cause serious problems and not at all in the spirit of OOP. But it's a quick fix if you want to be lazy.

Events are an extremely important part of Actionscript 3 and I recommend you read up on them. Here's some good references:
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fca.html
http://www.blog.mpcreation.pl/actionscript-3-0-basics-custom-events-part-1/

0
votes

I think you're right that your first problem is simply how to reference the new video, so to expand on my comment a bit: You can declare a variable without also assigning a value, so you don't need to have var nextvideo within your NewVideo function. With a class level variable instead, you can then reference whatever you set nextvideo to when you want to remove the video:

public class DocumentClass {

    private var nextvideo:FLV_Player;

    private function NewVideo(videoname:String)
    {
        nextvideo = new FLV_Player(videoname);
        addChild(nextvideo);
    }

    private function removeVideo():void
    {
        removeChild(nextvideo);
        nextvideo = null;
    }

}