How do I load MovieClip containing FLV without stuttering - actionscript-3

So I have var MC_1 which is Movieclip that contains FLV on its timeline. MC_1 is supposed to appear on screen when user presses SPACE. It is working, except that before MC_1 is done loading on screen, its almost played itself once, showing you last seconds and then starting its second loop.
How do I make it so that it plays itself only when it has done loading? Here's simplified code:
package comm {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.system.*;
import comm.*;
import comm.assets.*;
public class main {
public var MC_1:comm.assets.intro_video = new comm.assets.intro_video();
public var cutscene_container:Sprite = new Sprite();
public function main() {
addChild(cutscene_container);
stage.addEventListener(KeyboardEvent.KEY_DOWN, introstart_handler);
function introstart_handler(event:KeyboardEvent){
if(event.keyCode == 32){
cutscene_container.addChild(MC_1);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, introstart_handler);
}
}
}
}
}
I hope I explained my question clearly enough :P Cheers!

Came up with solution to my problem. Thought it nice to share if anyone else is having same problem:
package comm {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.system.*;
import comm.*;
import comm.assets.*;
public class main {
public var MC_1:comm.assets.intro_video = new comm.assets.intro_video();
public var cutscene_container:Sprite = new Sprite();
public function main() {
addChild(cutscene_container);
cutscene_container.addChild(MC_1);
MC_1.visible = false;
MC_1.gotoAndStop(1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, introstart_handler);
function introstart_handler(event:KeyboardEvent){
if(event.keyCode == 32){
MC_1.visible = true;
MC_1.gotoAndPlay(1);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, introstart_handler);
}
}
}
}
}
MC_1 is added to stage/container, stopped and invisible before it is needed. When it is needed (SPACE button press here) it is made visible and it will be played from frame 1.
Hope this helps if anyone else is having trouble with stuttering animation in form of FLV inside a MovieClip.
It surely did solve my problem.

Related

How to make a class remove itself?

I'm creating a game over screen and there is a retry button on it, when that button is clicked, the game over screen remove itself. I tried "this.visible = false;" but it doesn't seems to work. When I first restart the game, it works fine, the game over screen is gone. But when I restart the game for the second time, the game over screen is here again, it just keeps coming back after the first restart! So how to make the retry button to check the game over screen is on stage and if so then remove it? Any help is greatly appreciated!
package objects {
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.*
public class GameOverMenu extends BaseMenu {
public function GameOverMenu(stageRef: Stage = null) {
this.stageRef = stageRef;
btnRetry.addEventListener(MouseEvent.MOUSE_DOWN, returnSelectionMenu, false, 0, true);
}
private function returnSelectionMenu(e: MouseEvent): void {
unload(new SelectionMenu(stageRef));
this.visible = false;
}
}
}

AS3 debugger stops responding while trying to load image into sprite using Loader

I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files
Main code:
package {
//Other Files
import Menu;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
stage.addEventListener(Event.ENTER_FRAME, update);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
//Function starts game
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton);
}
//Updates every 60 seconds
public function update():void {
trace("Updated");
}
}
}
And Menu Image code:
package {
//Other files
import Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Menu extends Sprite {
public static function imageLoaded():void {
startButton.addChild(loader);
//initizlize values for startButton Bitmap
startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
}
//create startButton Bitmap
public static var startButton:Sprite = new Sprite();
public static var loader:Loader = new Loader();
loader.load(new URLRequest("lib/menustartbutton.png"));
loader.addEventListener(Event.COMPLETE, imageLoaded);
}
}
By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.
The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.
Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.

Why should I use preloader in Flash

I do not have quite knowledge about preloaders but I have read couple of articles and Adobe instructions. So I am confused about preloaders using in Flash applications.
I am planning to call all MovieClips, sounds, and etc. from library and nothing will be on the stage. For this situation, is it logical to apply preloader, if so which approach will be the most suitable one (even with smaller swf sizes)?
It's impossible to tell you which approach is the best when you're not telling the context of your application.
For banners and smaller swf ( <100k) etc I shouldn't use any sort of a preloader. Flash will handle the loading itself (only without showing a visual loader)
For bigger swf games I normally let one small swf loads the main swf.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
[SWF(width="992", height="768", frameRate="30", backgroundColor="0x000000")]
public class Preloader extends Sprite
{
private var percent:Number;
private var loader:Loader;
public function Preloader()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var movieurl:String = loaderInfo.parameters.movieurl;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
var loaderContext : LoaderContext = new LoaderContext(false,new ApplicationDomain(null));
loader.load( new URLRequest("main.swf" + version),loaderContext);
}
private function progressHandler(event:ProgressEvent):void
{
percent = ((event.bytesLoaded / event.bytesTotal)*100);
trace ("laoded": percent)
}
private function completeHandler(event:Event):void{
//removeChild(progressBar);
addChild(loader);
}
}
}
If your application must exist of 1 swf. You could use a Preloader class
main swf class
[Frame(factoryClass="Preloader")]
[SWF(width = "950", height = "600")]
public class Main extends Sprite
{
// do your coding
}
preloader swf class
package {
import erasmus.simulation.LoaderFC;
import flash.display.DisplayObject;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.UncaughtErrorEvent;
import flash.utils.getDefinitionByName;
public class Preloader extends MovieClip {
public function Preloader(){
if (stage){
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
}
private function progress(e:ProgressEvent):void {
var progress : Number = e.bytesLoaded / e.bytesTotal;
trace ("loader progress")
}
private function checkFrame(e:Event):void {
if (currentFrame == totalFrames){
stop();
loadingFinished();
}
}
private function loadingFinished():void {
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
var mainClass:Class = getDefinitionByName('Main') as Class; // class must be a string reference
this.stage.addChild(new mainClass(this) as DisplayObject);
}
}
}
Nothing will be on stage - by that I assume you're using Flash IDE with timeline?
In this case (as well as other cases, in fact) you must use the preloader. There is a possibility (even when running locally) that when you try to access something from the library, it will not be fully loaded yet.
In Flash IDE a preloader can be first two frames in the timeline: some progress sprite or just a TextField that spans two frames, first frame does nothing, second frame checks bytesLoaded vs. bytesTotal and goes to first frame if the movie is not fully loaded yet. The third frame is where all main code starts.
Note that all your library assets must be set for 'export in Frame 3', i.e. not in any of frames where loader is active.
Alternately, you can use single frame with event-based loader.
When using FlashDevelop, there is a ready-made template for a project with a preloader.
There is a common mistake when people use some of their library classes or assets in the preloader to show a nice progress indicator. In that case, all that data has to be loaded first, and preloader cannot work immediately. It looks like empty screen with long pause and no preloader, and then the application is 100% loaded at once. So the preloader becomes pointless, it can't show any progress to the user.

AS3 - How to get current scene name in class

I'm playing around with flash, and I've created multiple scenes for things like menu's, buttons, etc. When trying to add event handlers for buttons that are in one scene, but not others, the compiler complains saying that it can't reference to objects that don't exist.
I figured the solution to be simple... Get the scene name, match that against an if statement and load the event handlers through the if statements...
However, after digging around on the net for far too long, I just can't seem to find a way to do this properly. Does anyone know a way?
I've tried using the following :
var scene:Scene = myflvandclassname.currentScene;
var sName:String = MovieClip.currentScene.name;
Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class".
Omit MovieClip and scenes as source for organising your project, and code on the timeline. Use Document class as entry point. This tutorial should help you to grasp main concept.
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
private function setup():void {
const padding:int = 20;
//Initiate your UI components and place them in the display list
var menu:MyMenu = new MyMenu();
var buttons:MyFooterButtons = new MyFooterButtons();
var etc:AnotherComponent = new AnotherComponent();
addChild(menu);
addChild(buttons);
addChild(etc);
menu.x = menu.y = padding;
//Place them and initialise with concrete information
}
}
}
For example, MyMenu, MyFooterButtons, AnotherComponent could be MovieClip/Sprite in the library with export settings, where you did all your work with placement, styling, etc
I had a same problem. I wanted to check from an external Class the current scene name and depending on the name (name of the game level) to pass some values in some attributes… So what I did and it worked is that.
//main in 1st frame of the fla
stop();
var myCheckSceneClass: CheckSceneClass = new CheckSceneClass();
myCheckSceneClass.myCurrentScene = currentScene;
myCheckSceneClass.checkScene();
//CheckSceneClass
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Scene;
public class CheckSceneClass extends flash.display.MovieClip {
public var myCurrentScene : Scene;
public function CheckSceneClass () {
}
public function checkScene() {
switch (myCurrentScene.name) {
case "Scene 1":
trace("It is the 1st Scene");
break;
default:
trace("Error with Scene Name");
break;
}
}
}
}

ActionScript play audio

I am just trying to make a simple .swf file that plays a piece of audio when it loads. This compiles but when I bring it up into the browser nothing happens. I could only find sprite based tutorials so I took a stab that you can extend Sound the same way as you would extend Sprite. The final version is going to be headless and called my Java Script to play audio on Events.
package {
import flash.media.Sound;
import flash.net.URLRequest;
public class typeRight extends Sound {
public function HelloWorld( ) {
load(new URLRequest('./sound.mp3'));
play();
}
}
}
I am NOT working in Flash so please no GUI advice ; )
Rather than subclassing the Sound class, create a document class like this that contains a Sound class in it:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class SoundPlayer extends Sprite
{
protected var _sound : Sound;
protected var _channel : SoundChannel;
public function SoundPlayer()
{
_sound = new Sound();
_sound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
_sound.addEventListener(IOErrorEvent.IO_ERROR, loadError);
_sound.load(new URLRequest("./sound.mp3"));
}
protected function soundLoadCompleteHandler(evt : Event) : void
{
// Use the _channel object to control sound properties such as pan and volume.
_channel = _sound.play();
}
protected function loadError(evt : IOErrorEvent) : void
{
trace ("ERROR :: " + evt);
// You could try recovering from the error here.
}
}
}