CS5+AS3 Preloader starts at 50%; no clue why - actionscript-3

I know there are a lot of previous topics about preloaders and I've tried to follow every one of them but I still get the same problem (well they have helped me go from 80% -> 50%)
Right now it starts at 61450 / 125207 which is about 50%.
Here is my Main Document (default class file for the entire project) class:
public class MainDocument extends MovieClip
{
private var preloader:Preloader;
private var sB:startButton;
public function MainDocument()
{
preloader = new Preloader();
preloader.x = 300;
preloader.y = 400;
addChild(preloader);
loaderInfo.addEventListener(Event.COMPLETE,addStartButton,false,0,true);
}
private function addStartButton(e:Event):void
{
sB = new startButton();
sB.x = 300;
sB.y = 450;
sB.addEventListener(MouseEvent.CLICK,sMainMenu,false,0,true);
addChild(sB);
loaderInfo.removeEventListener(Event.COMPLETE,addStartButton);
}
private function sMainMenu(e:Event):void
{
sB.removeEventListener(MouseEvent.CLICK,sMainMenu);
removeChild(sB);
removeChild(preloader);
sB = null;
preloader = null;
var menuScreen = new MenuScreen();
addChild(menuScreen);
//I have heard that the following code might work better:
//var menuScreen:Class = getDefinitionByName("MenuScreen") as Class;
//addChild(new menuScreen() as DisplayObject);
}
}
And the Preloader that it attaches:
public class Preloader extends MovieClip
{
public function Preloader()
{
addEventListener(Event.ENTER_FRAME,Load);
}
private function Load(e:Event):void
{
//"bar" is a movieclip inside the preloader object
bar.scaleX = loaderInfo.bytesLoaded/loaderInfo.bytesTotal;
//"percent" is a dynamic text inside the preloader object
percent.text = Math.floor(loaderInfo.bytesLoaded/loaderInfo.bytesTotal*100)+"%";
trace(loaderInfo.bytesLoaded+" / "+loaderInfo.bytesTotal);
if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal)
{
removeEventListener(Event.ENTER_FRAME,Load);
}
}
}
-> Nothing is set to Export on Frame 1 except for the Preloader
-> No objects exist on the first frame; the only code on first frame is stop();
-> I placed a copy of every single MovieClip in the second frame and when the startButton is clicked, a gotoAndStop(3); is run so no one ever sees frame 2.
If anyone knows of anything simple that I could have forgotten, please let me know!
Thanks!

You're tying to use a preloader in the file being preloaded. In that case, there is going to be bloat from the rest of the project's code and assets. The reason you are seeing your preloader seemingly delayed is because a swf must load completely before any code will execute. This includes all assets on stage regardless of what frame they are on, even if you have settings in place to export on something other than frame 1. Instead, try using a blank shell as your preloader. This shell will have nothing in it but the loader code and a preloader graphic or animation. When the load is finished, hide your preloader and add your loaded content to the stage of the shell, or a container movieclip in the shell.
All the following code goes in your shell, which is just another FLA file with nothing in it but this code, and a preloader bar. The dimensions of this file should be the same as the file you are loading into it, ie your original swf file you were trying to preload.
Use it by calling loadSwf( "mySwfNameOrURLToSwf.swf" );
The variable _percent will populate with the current load percentage, which you can correspond to your loading bar scale. Presuming the preloader bar is named "bar", the line bar.visible = false; in the onSwfLoaded function will hide it. addChild( _swf ) adds the loaded swf to the shell's stage. The line _swf.init(); references a function in the loaded swf you will need to add called init() that starts your loaded swf doing whatever it is its supposed to do. Have everything in the loaded swf start on the first frame now, including the init() function.
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.events.Event;
import flash.events.ProgressEvent;
var _swfLoader:Loader;
var _swf:DisplayObject;
var _percent:Number;
function loadSwf( swfURL:String ):void
{
_swfLoader = new Loader();
var req:URLRequest = new URLRequest( swfURL );
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.applicationDomain = ApplicationDomain.currentDomain;
loaderContext.checkPolicyFile = true;
_swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onSwfProgress);
_swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
_swfLoader.load(req, loaderContext);
}
function onSwfProgress( evt:Event ):void
{
_percent = Math.round( ( evt.target.bytesLoaded / evt.target.bytesTotal ) * 100 );
}
function onSwfLoaded( evt:Event ):void
{
_swfLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onSwfProgress);
_swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoaded);
_swf = _swfLoader.content;
addChild( _swf );
bar.visible = false;
_swf.init();
_swfLoader = null;
}

your code looks ok.
when you are not on a http server, loading process are simulated.
After compiling, press crtl + B.
In menu you can choose the downloading speed and simulate a download by pressing again ctrl+enter.
it might help you to debug your preloader

#Lee Burrows What you said was right but would have been better if you looked at what I mentioned at the end of the code (three bold points)
The solution I used was:
-> I set everything to Export on Frame 2 on my 3 frame document.
-> Removed everything on Frame 2
-> Created a TextField via constructor and used drawRectangle for loading bar
-> No movieclips present on frame 1, and used
var menuScreen:Class = getDefinitionByName("MenuScreen") as Class;
addChild(new menuScreen() as DisplayObject);
instead of the previous code.
The reason why what I had originally didn't work because, as Lee Burrows mentioned, the Export for Actionscript hangs the loading if X = 1 in Export on Frame X, regardless if Export on Frame 1 was checked or not. Changing it to 2 or unchecking Export for Actionscript were the two solutions (except if it isn't exported for actionscript, then its code cant be referenced to).
Preloader starts at about 2% now.

Related

ActionScript 3.0 sound not working

So having trouble making sound on keyboard press
I have the imports:
import flash.net.URLRequest;
import flash.media.Sound;
I have the variables
private var soundDownRequest:URLRequest = new URLRequest ("SoundDown.mp3");
private var downSound:Sound = new Sound (soundDownRequest);
and the event listener
private function keyDownHandler(evt:KeyboardEvent):void
{
if (evt.keyCode == 40)//ascii for down arrow
{
downSound.play();
}
}
The sound folder is in the same folder as the .as, its also in the library of the fla, yet it still doesn't work. Any idea why?
Thank you.
Update:
I got the sound to work but not using the external method I was trying to do above.
Had to do it internally.
so you need:
import flash.media.SoundChannel;
-Then you need to make sure your sound file is in your fla library.
once its in the library
-Right click > properties
-Select the Action Script Tab
-Check "export for action script"
-Give the class a name in accordance to the sound
-press ok
add this variable (your will be different):
private var downSound:TheDownSound = new TheDownSound();
downsound is the selected name of the variable, and TheDownSound is the name of the class (the one made earlier for the sound file)
then add this to where you want the sound to play:
var myDownSound:SoundChannel = downSound.play();
Do this if you cant get it working externally like me.
for a better explanation watch this guys youtube video:
https://www.youtube.com/watch?v=SZpwppe7yGs
Your code is working perfectly ok if you put your .mp3 file in the same folder as the output .swf, not near the class .as source file (because its the swf file loading the sound, so the path must be relative to it)
public class ASEntryPoint extends Sprite {
private var soundDownRequest:URLRequest = new URLRequest ("click.mp3");
private var downSound:Sound = new Sound (soundDownRequest);
public function ASEntryPoint() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(evt:KeyboardEvent):void{
if (evt.keyCode == 40) {
downSound.play();
}
}
}
You need to load the external file, which is asynchronous operation. Then you track the loading event and if it all goes normally you can play your loaded sound.
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
// Keep the sound loader from being garbage collected.
var soundLoader:Sound;
function loadSound(url:String):void
{
var aRequest:URLRequest = new URLRequest(url);
soundLoader = new Sound();
// Handle the normal loading.
soundLoader.addEventListener(Event.COMPLETE, onLoaded);
// Handle the error cases.
soundLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
soundLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);
soundLoader.load(aRequest);
}
var audioChannel:SoundChannel;
function onLoaded(e:Event):void
{
// Sound is available here for playback.
audioChannel = soundLoader.play();
}
function onError(e:Event):void
{
trace(e);
}
You can also handle your sound as a streaming audio, but I worked with that years ago in AS2 so I cannot help here. Still, internet suggests a link: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d22.html

AS3 BitmapData and .as files

i have .fla and .as files.
.fla:
import test ;
var b:test = new test();
var myBitmap:BitmapData = new BitmapData(150, 150, true, 0x80FF3300);
var test:BitmapData = new BitmapData(150, 150, false, 0xFF0000);
var myImage:Bitmap = new Bitmap(test);
addChild(myImage);
and .as
package
{
import flash.display.*;
public dynamic class test extends flash.display.BitmapData
{
public function test(arg1:int=621, arg2:int=427)
{
super(arg1, arg2);
return;
}
}
}
But its not working, BitmapData must have the same name like loading .as (test.as), but i don't know how do that :|
If you will not add some functionality on top of the BitmapData why bother creating another class and extend the BitmapBata Class you can just use it directly.
as i understand you are not seeing anything on the screen, well this is normal because you're adding a blank bitmap to the stage ie: its BitmapData has nothing to deliver.
Maybe you need to link the Test class to an image imported into your fla so you'll get something to see on the screen.

Flixel - How to load and play an embedded swf file

I have been searching the web and all of the codes I have found are to play external swf files that have a timeline. The file that I am trying to load does not have a timeline. I am using the Flixel framework for this project and the file that I want to play is also made in Flixel(don't have the source file just the swf file).
Most of the code I have is from a cutscene template that I found on the Flixel forum. Here is what I have so far:
package
{
import org.flixel.FlxState;
import org.flixel.FlxG;
import flash.display.MovieClip;
import flash.media.SoundMixer;
import flash.events.Event;
public class SponsorsState extends FlxState
{
//Embed the cutscene swf relative to the root of the Flixel project here
[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')] 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();
}
}
}
}
When I run this I get this error: Type Coercion failed: cannot convert SponsorsState_SwfClass#fb5161 to flash.display.MovieClip., all I want to do is play the swf file for a set frame count then move onto the next state.
Any ideas on how to do this?
Try to replace the following:
[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')]
private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
into
//Mark your symbol for export and name it => MyExportedSymbol
[Embed(source='assets/DirtPileLogo.swf', symbol = "MyExportedSymbol")]
private var SwfSymbol:Class;
//Make sure that MyExportedSymbol base class is MovieClip
private var movie:MovieClip = new SwfSymbol;
Basically, you mark your symbol for export, give it a name and use that in the embed code. You will embed that symbol only.
You are incorrectly setting a mimeType on your embed. Remove the mimeType and it should work properly. See the docs on embedding assets for more information.
I believe the solution you are looking for is to use the Loader class.
[Embed (source = "assets/DirtPileLogo.swf", mimeType = "application/octet-stream")]
private var content:Class;
private var loader:Loader;
public function Main():void
{
var data:ByteArray = new content();
loader = new Loader();
addChild( loader );
loader.loadBytes( data, new LoaderContext(false, new ApplicationDomain() ) );
// ... add listener to loader if necessary, etc...
}

AS3 Stop external swf

Hi I'm loading an external swf into a MovieClip, and I want it to stop until I choose to play. Currently it plays upon loading immediately.
var mc:MovieClip;
var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, eventLoaded);
var request:URLRequest;
request = new URLRequest("external.swf");
swfLoader.load (request);
function eventLoaded(e:Event): void
{
mc = e.target.content as MovieClip;
// does not stop the clip
mc.Stop ();
}
So I tried adding a Event.ENTER_FRAME to the movieclip and stop it there, that will stop but it will play the first frame. Is there a way to get it to stay stopped when loaded until I choose Play?
It's actually very close to what Jochen Hilgers suggested. However, in this instance, the event you want is actually INIT instead of COMPLETE. INIT is fired when the content is not yet fully loaded but is ready for use (and will start playing on its own).
Attach the event with
loader.contentLoaderInfo.addEventListener(Event.INIT, handleReady );
And handle it with
public function handleReady( initEvent:Event ):void{
MovieClip(initEvent.currentTarget.content).stop();
}
You'll notice that you can cast the content property of currentTarget as a MovieClip and stop it even before it has been attached to the stage.
It is important to note that it is not safe to use the content property in a PROGRESS event (or any time prior to an INIT or COMPLETE event). You will get an error to the effect that the object is not ready.
I wrote this simple TestCase and it works fine... the loaded swf is quite simple, just a tween on the main timeline.
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Test extends Sprite
{
private var loader:Loader = new Loader;
public function Test()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoaded );
loader.load( new URLRequest( 'testFile.swf' ) );
}
public function handleLoaded( event:Event ):void
{
addChild( loader.content );
var mc:MovieClip = loader.content as MovieClip ;
mc.stop();
}
}
}
I was looking for a similar problem/solution, but my problem was little diferent. I know this was not your issue, but looks fair to share my solution. When I tried to do
event.currentTarget.stop(); // AS1&AS2 -> BAD swf to import
with the content of a loader, my Flash IDE showed me this error:
"Property stop not found on flash.display.AVM1Movie and there is no default value."
This happened to me because the swf I imported was created using AS1, and not AS3 as the main movie ( so I decompiled the swf to a fla and recompiled using as3, it was an output from After Effects). Now I know AVM1 and AVM2 are classes that represent actionscript 1 and 2 files.

Flash AS3: Unable to view loaded swf after loading it inside of package

Good morning friendly Flashers ;) So I've been trying since yesterday to just load a SWF file into my main movie. I've done this before just placing code inside a movieClip, but this time I'm working inside of Class files. I have my main class which calls a function inside of my sub class which contains the loader. My problem is that the swf will load (I can tell via traces) but I cannot see the loaded swf :(
Below is the code inside of my sub class
package src.howdinicurtain {
import flash.net.*;
import flash.display.*;
import flash.events.Event;
public class HowdiniFrame extends MovieClip {
//public var splashLoader;
public var introLoader:Loader = new Loader();
public var introContainer:MovieClip;
private var holdX:Number;
private var holdY:Number;
public function HowdiniFrame(url:String, loadX, loadY):void {
holdX = loadX;
holdY = loadY;
this.addChild(introLoader);
//this.addChild(introContainer);
introLoader.load(new URLRequest(url));
introLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);
}
public function swfLoaded(e:Event):void {
introLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoaded);
introContainer = introLoader.content as MovieClip;
//introContainer = MovieClip(introLoader.contentLoaderInfo.content);
addChild(introContainer);
introContainer.x = holdX;
introContainer.y = holdY;
trace("holdX = "+holdX);
trace("holdY = "+holdY);
}
}
}
The code above will load the swf file, I can see the swf files trace statements from the start of the animation to the end, but I cannot actually see the swf file inside of the main swf.
Traces:
The SWF file is = intro.swf
Intro Movie Starts :)
contentLoaderInfo event removed
Intro Movie Ends :(
Here is the code in my main class that calls the sub class function that loads the movie:
var introPath:String = xmlOutput.intro;
trace("The SWF file is = "+introPath+"\r"+"\r");
hc = new HowdiniFrame(introPath, 0, 20);
I swear I throw my code into the first frame of a movieClip and it works fine, I see the animation in the loaded SWF play instantly, but when I have my code inside of Class files I cannot see my SWF at all :( thoughts? ideas? Thanks for any tips!
~ Leon
Always treat your children right. Don't forget to add them in everything you do or else you're a bad parent.
What is hc? Is that a MovieClip on the stage? What if you try:
hc.addChild(new HowdiniFrame(introPath, 0, 20));
or if hc is not a clip on the stage
hc = new HowdiniFrame(introPath, 0, 20);
addChild(hc);