I am a Flash and ActionScript newbie. I am trying to follow a video tutorial to make a preloader and I'm having a problem that the video didn't seem to address. I believe I have entered in all of the code correctly from the video. This is it:
stop();
addEventListener(Event.ENTER_FRAME, loaderF);
function loaderF(e:Event):void{
var toLoad:Number = loaderInfo.bytesTotal;
var loaded:Number = loaderInfo.bytesLoaded;
var total:Number = loaded/toLoad;
if( loaded == toLoad ){
removeEventListener(Event.ENTER_FRAME, loaderF);
gotoAndStop(2);
} else {
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
}
What I typed in doesn't generate a compiler error, but the output tells me:
TypeError: Error #1010: A term is undefined and has no properties.
at preloader_fla::MainTimeline/loaderF()
And since I really don't have any experience outside of what I'm learning from this tutorial series, I don't know what to do to fix this.
I don't use Flash CS5, but you should be able to get the line # for where the error is occurring, I believe, by executing the SWF by pressing CTRL+SHIFT+ENTER.
Once you get the line number, you should see that something on that line is null or not defined. The error says it occurs in the function loaderF(), and looking at that code the only place such an error could occur is in the else block:
} else {
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
In the above code block, one of these things is not defined:
preloader_mc.preloaderFill_mc,
preloader_mc.percent_txt,
preloader_mc.ofBytes_txt,
preloader_mc.totalBytes_txt
Maybe your preloader movie clip is missing one of these objects...
First, you'll want to turn on debugging found under (File > Publish Settings > Flash (.swf) > Permit Debugging). This will provide line numbers and allow additional debugging to help track down errors.
Secondly, in the code sample you've provided, you haven't declared a loader, so when you call on loaderInfo, it makes sense that flash complains about "a term is undefined". Although, technically, the loaderInfo object is a child of the event object. Thus, loaderInfo.bytesTotal would become e.loaderInfo.bytesTotal, assuming you added the event listener to the loader object; currently yours is added to the timeline.
Bookmark Adobe's Actionscript 3.0 Reference. Use it. As you begin your journey in Flash, this will be your indispensable handbook to speaking AS3. Specifically, you'll want to refer to the Loader class.
Here's what you're likely missing in your code:
var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("path/to/my/file"));
Your function loaderF is being called during every frame update to the screen (likely every .034 seconds). You'd probably be happier with ProgressEvent.PROGRESS instead of Event.ENTER_FRAME. If so, you'll also want to catch the complete event, and that'd look like this:
myLoader.addEventListener(Event.COMPLETE, loadComplete);
myLoader.addEventListener(ProgressEvent.PROGRESS, loadProgress);
function loadComplete(e:Event):void {
// Stuff to do when the file finishes loading.
}
function loadProgress(e:Event):void {
var current:int = e.bytesLoaded;
var total:int = e.bytesTotal;
var percent:Number = current/total;
// Update the readout of your loading progress.
}
Hopefully that points you in the right direction. :)
Related
I'm setting up a button on the first frame which when clicked will transfer the user to the 2nd frame using this code:
stop();
Btn_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);
function fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent):void
{
gotoAndPlay(2);
}
and on the second frame, I set up a dynamic text that will perform a countdown using this code:
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
countdown_text.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(3);
}
the thing is keep getting TypeError: Error #1009 message after debugging it. I know the fault is in line 7 of the 2nd code but I have no idea what is wrong with it. Pls help!
I should see your source fla, but it is most likely related to countdown_text not being accessible in that frame. Error description is "Cannot access a property or method of a null object reference", that means it cannot find the reference which is "countdown_text".
It is very very bad practice to write AS directly in frames. Convert code into a class and assign it as a document class.
You can find Adobe documentation for document class here: https://helpx.adobe.com/animate/using/actionscript-publish-settings.html
I'm doing a little animation using Actionscript 3.0 but I'm having a few problems (I'm new with AS3) when I want to play a movie clip in reverse and, then, return to frame 1(main).
Here is my code:
B6_btn.addEventListener(MouseEvent.CLICK, onClickReverse6);
function onClickReverse6(event:MouseEvent):void{
m6_mc.addEventListener(Event.ENTER_FRAME, playReverse6, false, 0, true);
}
function playReverse6(event:Event):void{
if(m6_mc.currentFrame == 1){
if(playMusic){
playMusic.stop();
}
gotoAndStop(1);
}else{
m6_mc.prevFrame();
}
}
The error I get is with the line
"if(m6_mc.currentFrame == 1)" - ERROR #1009: Cannot access a property
or method of a null object reference
If I remove the command gotoAndStop(1), no error is presented.
Can anyone, please, help me with my code?
As far as I can see, you are calling gotoAndStop some other MovieClip (it's actually whatever this is in your case). But at this time you are checking the currentFrame property fo the m6_mc.
I think there might be some error with that. If you switch this to frame 1, is there a m6_mc instance (if you're not keeping it in a variable)?
I have this code on frame 1, regarding the code presented before:
function onClick6(event:MouseEvent):void{
musicLoader = new URLRequest("music/GABRIEL.mp3");
music = new Sound();
music.load(musicLoader);
playMusic = music.play(0,4);
gotoAndStop(7);
m6_mc.gotoAndPlay(1);}
Currently I have an intro screen to my flash file which has two objects.
A button which will load an external flash file using:
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("flashgame.swf");
The second thing is a Numeric Stepper, which will be from 1 to 10. If the user selects a number e.g. 3 then the game speed I have set in the flashgame.swf should be changed
Such as:
var gameSpeed:uint = 10 * numericStepper.value;
But I think my problem is coming into place because the stepper and gamespeed are from different files.
Anyone got any idea please?
I have also tried creating a stepper in the game file and used this code:
var gameLevel:NumericStepper = new NumericStepper();
gameLevel.maximum = 10;
gameLevel.minimum = 1;
addChild(gameLevel);
var gameSpeed:uint = 10 * gameLevel.value;
For some reason the stepper just flashes on the stage, no errors come up and the game doesn't work
When you execute you code, the stepper has no chance to wait for user input.
There is no time between theese two instructions.
addChild(gameLevel);
var gameSpeed:uint = 10 * gameLevel.value;
You should wait for user input in your NumericStepper, and then, on user event, set the game speed.
Edit: Yeah I know it's kinda sad to type out all this code (especially since some people wouldn't even be grateful enough to say thanks) but I think this question is important enough to justify the code as it may be helpful to others in future also.
Hi,
You were close. In your game file you could have put a var _setgameSpeed and then from Intro you could adjust it by flashgame._setgameSpeed = gameSpeed; It's a bit more complicated though since you also have to setup a reference to flashgame in the first place. Let me explain...
Ideally you want to put all your code in one place (an .as file would be best but...) if you would rather use timeline then you should create a new empty layer called "actions" and put all your code in the first frame of that.
Also change your button to a movieClip type and remove any code within it since everything will be controlled by the code in "actions" layer. In the example I have that movieclip on the stage with instance name of "btn_load_SWF"
Intro.swf (Parent SWF file)
var my_Game_Swf:MovieClip; //reference name when accessing "flashgame.swf"
var _SWF_is_loaded:Boolean = false; //initial value
var set_gameSpeed:int; //temp value holder for speed
var swf_loader:Loader = new Loader();
btn_load_SWF.buttonMode = true; //instance name of movieclip used as "load" button
btn_load_SWF.addEventListener(MouseEvent.CLICK, load_Game_SWF);
function load_Game_SWF (event:MouseEvent) : void
{
//set_gameSpeed = 10 * numericStepper.value;
set_gameSpeed = 100; //manual set cos I dont have the above numericStepper
if ( _SWF_is_loaded == true)
{
stage.removeChild(swf_loader);
swf_loader.load ( new URLRequest ("flashgame.swf") );
}
else
{ swf_loader.load ( new URLRequest ("flashgame.swf") ); }
swf_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, Game_SWF_ready);
}
function Game_SWF_ready (evt:Event) : void
{
swf_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, Game_SWF_ready);
//Treat the Loader contents (flashgame.swf) as a MovieClip named "my_Game_Swf"
my_Game_Swf = swf_loader.content as MovieClip;
my_Game_Swf.gameSpeed = set_gameSpeed; //update gameSpeed variable in flashgame.swf
//also adjust SWF placement (.x and .y positions etc) here if necessary
stage.addChild(my_Game_Swf);
_SWF_is_loaded = true;
}
Now in you flashgame file make sure the there's also an actions layers and put in code like this below then compile it first before debugging the Intro/Parent file. When your Intro loads the flashgame.swf it should load an swf that already has the code below compiled.
flashgame.swf
var gameSpeed:int;
gameSpeed = 0; //initial value & will be changed by parent
addEventListener(Event.ADDED_TO_STAGE, onAdded_toStage);
function onAdded_toStage (e:Event):void
{
trace("trace gameSpeed is.." + String(gameSpeed)); //confirm speed in Debugger
//*** Example usage ***
var my_shape:Shape = new Shape();
my_shape.graphics.lineStyle(5, 0xFF0000, 5);
my_shape.graphics.moveTo(10, 50);
my_shape.graphics.lineTo(gameSpeed * 10, 50); //this line length is affected by gameSpeed as set in parent SWF
addChild(my_shape);
}
The key line in intro.swf is this: my_Game_Swf.gameSpeed = set_gameSpeed; as it updates a variable in flashgame.swf (referred as my_Game_Swf) with an amount that is taken from a variable in the Parent SWF.
This is just one way you can access information between two separate SWF files. Hope it helps out.
please help i am getting this error and could not solve with any of the other methods described in all previous posts with similar topic.
Actually here i am loading a swf myMap onto another swf.
The swf loading works fine, but when try to remove this from stage i get the above said error...
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at actions.classes::MapInteractionManager/unloadSWF()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
Here's my as3 code...
var _swfLoader:Loader;
var _swfContent:MovieClip;
loadSWF("myMap.swf"); //loading the swf file here
function loadSWF(path:String):void {
var _req:URLRequest = new URLRequest();
_req.url = path;
_swfLoader = new Loader();
setupListeners(_swfLoader.contentLoaderInfo);
_swfLoader.load(_req);
}
function setupListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, addSWF);
dispatcher.addEventListener(ProgressEvent.PROGRESS, preloadSWF);
}
function preloadSWF(event:ProgressEvent):void {
var _perc:int = (event.bytesLoaded / event.bytesTotal) * 100;
// swfPreloader.percentTF.text = _perc + "%";
}
function addSWF(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, addSWF);
event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
_swfContent = event.target.content;
_swfContent.addEventListener("close", unloadSWF);
main.stage.addChild(_swfContent);
}
function unloadSWF(event:Event):void {
_swfLoader.unloadAndStop();
main.stage.removeChild(_swfContent); //getting error when trying to remove swf
_swfContent = null;
}
and close event is as,
_swfContent.dispatchEvent(new Event("close"));
Please help, I'm stuck.
here with some update,
i updated code as,
function unloadSWF(event:Event):void
{
if(main.stage.contains(_swfContent))
main.stage.removeChild(_swfContent);
}
Now the error is gone as it is not entering the if loop!!!???
But still i can see that swf on stage:( plz help
GOT SOLVED...
Thanks everyone for helping...
ToddBFisher did solve it:)
Simply added the _swfLoader to the stage, loaded it, and attached the close listener to it instead of even having a _swfContent. Cut out the middle man and it worked.... Hope this helps...
As I recall .unloadAndStop(); does a bunch of cleanup type things, which you are calling right before. It is possible part of the cleanup is removing it from the display list.
Try calling the removeChild() before calling unlodaAndStop()
function unloadSWF(event:Event):void {
stage.removeChild(_swfContent); //getting error when trying to remove swf
_swfLoader.unloadAndStop();
_swfContent = null;
}
EDIT
Try simply adding the _swfLoader to the stage, load it, and attach the close listener to it instead of even having a _swfContent. Cut out the middle man and see what happens.
The error means the child is already removed (or never added)
try to comment out
_swfLoader.unloadAndStop();
to see if it works.
main.stage.removeChild is already setting _swfContent to null because removeChild was fired so removing the line _swfContent = null might solve it.
I need to have a swf load at the beginning, but I don't want to play about the last 3 seconds of the video.
Can someone help me out with a code that would basically have my swf play for "x" seconds?
Heres what I Have so far.. Currently it is set up to play the swf, but subtract "x" seconds from it but for some reason it doesn't seem to work
var mySwf:MovieClip;
var reducedTotalFrames:int;
var clipLoader:Loader = new Loader();
clipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
clipLoader.load(new URLRequest("schoolintro.swf"));
function loadComplete(e:Event):void
{
mySwf = LoaderInfo(e.currentTarget).content as MovieClip;
var totalFrameCount:int = mySwf.currentScene.numFrames;
var secondsToSubtract:int = 3;
var threeSecondFrameCount:int = (stage.frameRate * secondsToSubtract);
reducedTotalFrames = totalFrameCount - threeSecondFrameCount;
stage.addEventListener(Event.ENTER_FRAME, onRender);
stage.addChild(mySwf);
}
function onRender(e:Event):void
{
if(mySwf != null && mySwf.currentFrame >= reducedTotalFrames){
//This is the end of the SWF with 3 seconds trimmed off. Here we can stop play
stage.removeEventListener(Event.ENTER_FRAME, onRender);
mySwf.stop();
//doSomethingElse();
}
}
Try some basic debugging, like placing trace statements in the code inside onRender to ensure it's being called, to ensure the mySwf reference is working correctly (for example, tracing out mySwf.currentScene.numFrames, and if its 0, you know you're not referencing your swf properly or the swf is not frame-based). If it the SWF is not frame-based, then you're going to have real issues controlling or even doing anything about this since all the animation/action going on inside the loaded SWF is code-based.
I think you just need to change this line from
var threeSecondFrameCount:int = (stage.frameRate * secondsToSubtract);
to
var threeSecondFrameCount:int = (stage.frameRate / secondsToSubtract);
not sure is this what you want or not.