Author Topic: Cutscene Template Class  (Read 4472 times)

Willseph

  • Member
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
    • Willseph Design
Cutscene Template Class
« on: Thu, Oct 7, 2010 »
I'm not sure if adding cutscenes to Flixel games is an easy thing to do for most developers, but it was a bit of work for me. I looked around looking for tutorials with little luck, until finally I stumbled upon a solution that was able to work.

To help those in my situation, I wrote a template class file to use when wanting to add cutscenes from external SWF files. I had trouble trying to write a class that you could construct, passing down the cutscene parameters as arguments. The only way I could get it to work for myself was to write a unique class for each cutscene. Here is the template code for the class:

LogoCutscene.as:
Code: [Select]
package
{
import org.flixel.FlxG;
import org.flixel.FlxState;
import flash.display.MovieClip;
import flash.media.SoundMixer;
import flash.events.Event;
public class LogoCutscene extends FlxState
{
//Embed the cutscene swf relative to the root of the Flixel project here
[Embed(source = 'logo.swf')] private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
//This is the length of the cutscene in frames
private var length:Number;

override public function create():void
{
movie = new SwfClass();
//Set your zoom factor of the FlxGame here (default is 2)
var zoomFactor:int = 2;
movie.scaleX = 1.0/zoomFactor;
movie.scaleY = 1.0 / zoomFactor;
//Add the MovieClip container to the FlxState
addChildAt(movie, 0);
//Set the length of the cutscene here (frames)
length = 100;
//Adds a listener to the cutscene to call next() after each frame.
movie.addEventListener(Event.EXIT_FRAME, next);
}
private function next(e:Event):void
{
//After each frame, length decreases by one
length--;
//Length is 0 at the end of the movie
if (length <= 0)
{
//Removes the listener
movie.removeEventListener(Event.EXIT_FRAME, next);
//Stops all overlaying sounds before state switch
SoundMixer.stopAll();
//Enter the next FlxState to switch to
FlxG.state = new PlayState();
}
}
}
}

I have some of my own options in there, so here's the data you should be replacing for your project.

Line 8:
Code: [Select]
public class LogoCutscene extends FlxStateReplace LogoCutscene with the name of your unique cutscene class file.

Line 11:
Code: [Select]
[Embed(source = 'logo.swf')] private var SwfClass:Class;Replace logo.swf with the SWF filename to play as the cutscene.

Line 20:
Code: [Select]
var zoomFactor:int = 2;Replace 2 with the Zoom in your main FlxGame file. Default is 2.

Line 27:
Code: [Select]
length = 100;Replace 100 with the length of your cutscene in frames.

Line 43:
Code: [Select]
FlxG.state = new PlayState();Replace PlayState with the class name of the FlxState to switch to after the cutscene.

That should be it! Because this class is an extension of FlxState, you can use:
Code: [Select]
FlxG.state = new LogoCutscene();to switch to a cutscene (obviously replacing LogoCutscene with the unique class name of your cutscene).

Thanks to Tyranus, there's a much easier way to stop the cutscene at the end of its timeline using the number of frames, rather than milliseconds. In line 27, simply change 100 to the number of frames in your cutscene SWF (don't worry about frame rate compensation).

This code was written and tested with Flixel v2.43.
« Last Edit: Fri, Oct 8, 2010 by Willseph »

AlexanderOcias

  • Active Member
  • ***
  • Posts: 110
  • Karma: +0/-0
    • View Profile
Re: Cutscene Template Class
« Reply #1 on: Thu, Oct 7, 2010 »
I haven't worked with swfs built through the Flash IDE (which is what I assume you're getting at here), but couldn't you just use

Code: [Select]
if (movie.currentFrame == movie.totalFrames) {
//Go to next state
}

To automatically sort that whole thing out, without using the length var?

Willseph

  • Member
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
    • Willseph Design
Re: Cutscene Template Class
« Reply #2 on: Thu, Oct 7, 2010 »
Trust me, that was the very first thing I tried. I think because it's purely loading byte data when you embed an SWF as a class, it can't read the inside frames for some reason. I tried tracing the current frame and it stayed locked at 0. I even went down into its children and tried to find something in there, but no luck.

Until someone finds a better method than me, you'll have to use the timer.

Tyranus

  • Member
  • **
  • Posts: 54
  • Karma: +0/-0
    • View Profile
    • Tyranus:: Indie Games.
Re: Cutscene Template Class
« Reply #3 on: Fri, Oct 8, 2010 »
Hi people,

I "solve" this problem using a trick.

First you must now the total frames (no mather how you do that).

Second:
Code: [Select]
private var frames=0;//current frame in the swf
private static const MAX = 456; // total frame in the swf.

blablabla

private function next(e:Event):void {
frames++;
if (frames >= MAX) {
swf.removeEventListener(Event.EXIT_FRAME,next);
// remove the movie
        //change the state
}

}

And when you create the movie

Code: [Select]
movie.addEventListener(Event.EXIT_FRAME, next);

Greetings!


Willseph

  • Member
  • **
  • Posts: 21
  • Karma: +0/-0
    • View Profile
    • Willseph Design
Re: Cutscene Template Class
« Reply #4 on: Fri, Oct 8, 2010 »
Oh that's a much better solution Tyranus. I'll change the original post to use that! :D

Chelnok

  • Contributor
  • ****
  • Posts: 257
  • Karma: +0/-0
    • View Profile
Re: Cutscene Template Class
« Reply #5 on: Thu, Apr 7, 2011 »
One solution to get totalFrames

Use movieclip as container in swf, and embed only that symbol:
(dont forget to export it for actionscript)

[Embed(source="cutscene.swf", symbol="csMc")] private var CutSceneSwf:Class

...

var mc:MovieClip = new CutSceneSwf();
this.addChild(mc);      
trace(mc.totalFrames) //21 ..well in my case :)


I havnt try this LogoCutscene.as, but should work.

zxz1661

  • Member
  • **
  • Posts: 62
  • Karma: +0/-0
  • Full of Fail
    • View Profile
Re: Cutscene Template Class
« Reply #6 on: Tue, May 31, 2011 »
a couple issues:

is there an easy way to find the amount of frames in a .swf file?

And i'm also getting the error:

Call to a possibly undefined method addChildAt.
addChildAt(movie, 0);

Code: [Select]
package
{
import org.flixel.FlxG;
import org.flixel.FlxState;
import flash.display.MovieClip;
import flash.media.SoundMixer;
import flash.events.Event;
public class Cutscene1 extends FlxState
{
//Embed the cutscene swf relative to the root of the Flixel project here
[Embed(source='org/flixel/data/cutscene.swf')] private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
//This is the length of the cutscene in frames
private var length1:Number;
 
override public function create():void
{
movie = new SwfClass();
//Set your zoom factor of the FlxGame here (default is 2)
var zoomFactor:int = 2;
movie.scaleX = 1.0/zoomFactor;
movie.scaleY = 1.0 / zoomFactor;
//Add the MovieClip container to the FlxState
addChildAt(movie, 0);
//Set the length of the cutscene here (frames)
length1 = 100;
//Adds a listener to the cutscene to call next() after each frame.
movie.addEventListener(Event.EXIT_FRAME, next);
}
private function next(e:Event):void
{
//After each frame, length decreases by one
length1--;
//Length is 0 at the end of the movie
if (length1 <= 0)
{
//Removes the listener
movie.removeEventListener(Event.EXIT_FRAME, next);
//Stops all overlaying sounds before state switch
SoundMixer.stopAll();
//Enter the next FlxState to switch to
FlxG.switchState(new MenuGame)
}
}
}
}