Copy link to clipboard
Copied
Alright so I have posted a similar question before, but it was never truly answered, only fixed. I mean, I recieved incredible amounts of help and a solution that works, but not one that makes sense to me.
After adding a preloader with this code:
stop();
import flash.events.ProgressEvent;
import flash.events.Event;
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(evt:ProgressEvent):void {
//trace("Do Not Disturb");
var loaded:Number = evt.bytesLoaded / evt.bytesTotal;
percent_txt.text = (loaded*100).toFixed(0) + "%";
}
function onComplete(event:Event):void {
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
gotoAndStop(2);
}
package cbhCode
{
import flash.display.MovieClip;
import flash.events.Event;
import com.rw.GameClock
import flash.events.MouseEvent;
public class Main extends MovieClip
{
//TOYS
public var bouncyBall:BouncyBall = new BouncyBall();
//BROKE TOYS
public var brokeBouncyBall:BrokeBouncyBall = new BrokeBouncyBall();
//This array will hold the names of all the safe toys so when they spawn, I can simply reference a number of this array
public var safeToys:Array = new Array(bouncyBall);
//This is the same as the safeToys array, but for broken toys
public var brokenToys:Array = new Array(brokeBouncyBall);
//This array will hold any toys, safe or broken, that are on the stage
public var stageToys:Array = new Array();
//This variable will be used to hold the object in the array chosen at random
public var chooseToy:int;
//These two variables hold the length of safeToys and brokenToys
public var brokeToysNum:int = (brokenToys.length -1);
public var safeToysNum:int = (safeToys.length -1);
public function Main()
{
// constructor code
initGame();
}
public function initGame() {
trace("hello");
safeToys[0].x = 0;
safeToys[0].y = -50;
stage.addChild(safeToys[0]);
stageToys.push(safeToys[0]);
brokenToys[0].x = 0;
brokenToys[0].y = -1000;
stage.addChild(brokenToys[0]);
stageToys.push(brokenToys[0]);
addEventListener(Event.ENTER_FRAME, checkToyLocation);
}
public function checkToyLocation(e:Event)
{
trace(currentFrame);
for (var i:int; i < stageToys.length; i++)
{
//once a toy reaches the end of the stage, it needs to be removed
if (stageToys.x > stage.stageWidth)
{
trace("later");
stage.removeChild(stageToys);
stageToys.splice(i,1);
}
}
}
}
}
Copy link to clipboard
Copied
your point being...?
Copy link to clipboard
Copied
ugh sorry, I accidently posted this and so I went back to edit it but after spending 10 minutes writing the rest, I was informed that I am not allowed to edit.
The point is that ever since I added something to the timeline, I can't add anything to the stage using Main.as, even though the .swf still reads through it.
For example, when I test the game, output displays "hello," then "2" a bunch of times. Then a few seconds later it displays "later" twice, since the BrokeBouncyBall class and BouncyBall class automatically move to the right every frame. Meanwhile, nothing appears on the stage, it remains blank.
I just don't understand why this is happening or how to bypass it. It seems like, from what I have read so far, it is normal to leave the timeline empty and put everything in Main doc class. Now I am beginning to think that is impossible if you want to include a preloader and everything in one .swf or anything else is on the timeline. It's frustrating because if that's the case, I have to relearn loads of stuff I already know.
Copy link to clipboard
Copied
the constructor of your document class is processed before the main timeline (usually root) is added to the stage. That is the reason why most programmers I know of avoid putting any code on the timeline. The moment you are having a document class and timelinecode, things can get really ugly.
The easiest way is imo to separate the preloader and your main.swf completely otherwise you will have to uncheck all the MovieClips export in first frame checkbox in your Library you link with actionscript and place them on stage before your preloader ends.
There are thousand of tutorials how to do this (separating your preloader from your main.swf), the only thing you have to consider that in the constructor of your document class you will have to wait for the main.swf to be added to the stage to access all your Library Symbols
which can be done with these LOC:
public fúnction Main(){
if(stage!=null){
initGame();
}
else{
addEventListener(Event.ADDED_TO_STAGE,initGame);
}
}
public function initGame(e:Event = null) {...
Copy link to clipboard
Copied
Alright I got rid of all code on my timeline and put this in my Main class
public var progress_txt:TextField = new TextField();
public function Main()
{
// constructor code
stop();
progress_txt.x = stage.stageWidth / 2;
progress_txt.y = stage.stageHeight / 2;
addChild(progress_txt);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
loaderInfo.addEventListener(Event.COMPLETE, onComplete);
}
function onProgress(e:ProgressEvent):void
{
progress_txt.text = String(Math.floor((e.bytesLoaded/e.bytesTotal)*100));
}
function onComplete(e:Event):void
{
trace("Fully loaded, starting Movie");
//now when movie is loaded, we can instantiate the image and add it on the stage
initGame();
//removing stuff we won't use anymore
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
removeChild(progress_txt);
}
public function initGame()
{
trace("hello");
gotoAndStop(2);
safeToys[0].x = 0;
safeToys[0].y = 0;
stage.addChild(safeToys[0]);
stageToys.push(safeToys[0]);
brokenToys[0].x = 0;
brokenToys[0].y = -1000;
stage.addChild(brokenToys[0]);
stageToys.push(brokenToys[0]);
addEventListener(Event.ENTER_FRAME, checkToyLocation);
}
Now my timeline has just one blank keyframe in it.
The code runs fine with no errors, however nothing is added to the stage. "Fully loaded, starting movie" is output first, then "hello," then "1" every frame. A few seconds after, "later" is output twice, since the two toys added to the screen trace "later" once they leave the boundaries.
It's like Flash is picking and choosing which lines to read and which lines to ignore.
It would be nice if I could include two .swf files, however right now it is too inconvenient for me to host my own .swf gallery and instead am uploading my work via 3rd parties that only allow 1 file.
Thanks for the advice mocca
Copy link to clipboard
Copied
If you absolutely must use one swf, use this method(1)
Copy link to clipboard
Copied
Try to add objects to the Main class dispaly list - not stage.
Do you see anything when you use the following class?
package cbhCode
{
import flash.display.MovieClip;
import flash.events.Event;
import com.rw.GameClock
import flash.events.MouseEvent;
public class Main extends MovieClip
{
//TOYS
public var bouncyBall:BouncyBall = new BouncyBall();
//BROKE TOYS
public var brokeBouncyBall:BrokeBouncyBall = new BrokeBouncyBall();
//This array will hold the names of all the safe toys so when they spawn, I can simply reference a number of this array
public var safeToys:Array = new Array(bouncyBall);
//This is the same as the safeToys array, but for broken toys
public var brokenToys:Array = new Array(brokeBouncyBall);
//This array will hold any toys, safe or broken, that are on the stage
public var stageToys:Array = new Array();
//This variable will be used to hold the object in the array chosen at random
public var chooseToy:int;
//These two variables hold the length of safeToys and brokenToys
public var brokeToysNum:int = (brokenToys.length - 1);
public var safeToysNum:int = (safeToys.length - 1);
public function Main()
{
// constructor code
initGame();
}
public function initGame():void
{
trace("hello");
safeToys[0].x = 0;
safeToys[0].y = -50;
addChild(safeToys[0]);
stageToys.push(safeToys[0]);
brokenToys[0].x = 0;
brokenToys[0].y = -1000;
addChild(brokenToys[0]);
stageToys.push(brokenToys[0]);
addEventListener(Event.ENTER_FRAME, checkToyLocation);
}
public function checkToyLocation(e:Event):void
{
trace(currentFrame);
for (var i:int; i < stageToys.length; i++)
{
//once a toy reaches the end of the stage, it needs to be removed
if (stageToys.x > stage.stageWidth)
{
trace("later");
removeChild(stageToys);
stageToys.splice(i, 1);
}
}
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now