Copy link to clipboard
Copied
I am creating a game that consist of multiple external SWF files that I connected using the loader class. I have 3 levels. At first the connection of the swf is woking well. But after finishing level 2, when the congratulatory screen appears and then pressed the button so that I can proceed to the next level, it goes back to level 2 instead of proceeding to Level 3. >.< When I check the URL, it is correct.
assuming the top snippet is stage2.swf and it loads a congratuloary.swf that uses that 2nd snippet, your code shows stage2.swf loading congratulatory.swf and congratulotory.swf loading stage2.swf and that's a problem. fix that.
Copy link to clipboard
Copied
copy and paste the relevant code.
Copy link to clipboard
Copied
Here is the code.
On the Congratulatory Screen where there is a button connected to Stage3:
next_stage.addEventListener(MouseEvent.CLICK, loadStage_2);
var music_1:Sound = new Sound(new URLRequest("Congrats_Theme.mp3"));
music_1.play();
function loadStage_2(event:MouseEvent):void
{
var myrequest:URLRequest=new URLRequest("Stage3.swf");
var myloader:Loader=new Loader();
myloader.addEventListener("UnloadMe", unloadFunction);
myloader.load(myrequest);
stage.addChild(myloader);
removeChildAt(0);
removeChild(next_stage);
SoundMixer.stopAll();
}
function unloadFunction(event:Event):void {
Loader(event.currentTarget).unload();
}
Level 2 code:
if (fl_SecondsElapsed == 0 && score >= 70)
{
stopGame();
var myrequest:URLRequest = new URLRequest("Congratulatory_Screen_2.swf");
var myloader:Loader = new Loader();
myloader.addEventListener("UnloadMe", unloadFunction);
myloader.load(myrequest);
SoundMixer.stopAll();
stage.addChild(myloader);
removeChild(Score_board);
removeChild(Timer_board);
removeChild(ScoreDisplay);
removeChild(TimerDisplay);
}
else if (fl_SecondsElapsed == 0 && score < 70)
{
stopGame();
var myrequest1:URLRequest = new URLRequest("Game_Over_2.swf");
var myloader1:Loader = new Loader();
myloader1.addEventListener("UnloadMe", unloadFunction_1);
myloader1.load(myrequest1);
SoundMixer.stopAll();
stage.addChild(myloader1);
removeChild(Score_board);
removeChild(Timer_board);
removeChild(ScoreDisplay);
removeChild(TimerDisplay);
}
}
public function unloadFunction(event:Event) {
Loader(event.currentTarget).unloadAndStop();
}
public function unloadFunction_1(event:Event) {
Loader(event.currentTarget).unloadAndStop();
}
Copy link to clipboard
Copied
One more thing, when I play the swf file of Congratulatory Screen, it works well. It proceeds to Level 3 when I pressed the button. But when I start on Level 1, the Congratulatory Screen doesn't proceed to Level 3 but instead goes back to Level 2.
Copy link to clipboard
Copied
is that first code snippet from Congratulatory_Screen_2.swf? is level 3, Stage3.swf?
and, the first errors i see are related to an undefined event "Unload Me":
myloader.addEventListener("UnloadMe", unloadFunction); |
you should fix that.
Copy link to clipboard
Copied
Yes. The frst is from Congratulatory_Screen_2 and Level 3 is stage3.swf.
Regarding the UnloadMe, it is working well for me. >.< Here is my full code for Level2:
package
{
import flash.automation.StageCaptureEvent;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.display.Stage;
import flash.utils.getDefinitionByName;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundMixer;
public class Stage2 extends MovieClip {
var catcher:Catcher;
var nextObject:Timer;
var objects:Array = new Array();
var ObjectHalfWidth: Number;
const speed: Number = 6.0;
var score: int = 0;
var fl_TimerInstance:Timer = new Timer(1000, 60);
var fl_SecondsElapsed:Number = 60;
var music_1:Sound = new Sound(new URLRequest("Cheerful_Theme.mp3"));
var soundfx_1:Sound = new Sound(new URLRequest("Catch_Good.mp3"));
var soundfx_2:Sound = new Sound(new URLRequest("Catch_Bad.mp3"));
var soundfx_3:Sound = new Sound(new URLRequest("Ticking_Clock.mp3"));
public function Stage2() {
catcher = new Catcher ();
catcher.y = 768;
catcher.x = 120;
addChild(catcher);
catcher.mouth_mc.gotoAndStop(1);
setNextObject();
addEventListener(Event.ENTER_FRAME, moveObjects);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();
music_1.play();
}
public function setNextObject() {
nextObject = new Timer(1000 + Math.random() * 1500, 1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);
nextObject.start();
if (fl_SecondsElapsed == 0)
{
nextObject.stop();
}
}
public function newObject(e:TimerEvent) {
var solidObjects:Array = ["Chocolate","Apple", "Laptop", "USB"];
var liquidObjects:Array = ["Coffee", "CookingOil", "Milk", "Water"];
var badObjects:Array = ["Fire"];
var randomNum:Number = Math.random();
if ((randomNum >= .1) && (randomNum <= .4)) {
var r:int = Math.floor(Math.random() * solidObjects.length);
var classRef:Class = getDefinitionByName(solidObjects
var newObject:MovieClip = new classRef();
newObject.typestr = "solid";
}
else if ((randomNum >= .5) && (randomNum <= 1)) {
r = Math.floor(Math.random() * liquidObjects.length);
classRef = getDefinitionByName(liquidObjects
newObject = new classRef ();
newObject.typestr = "liquid";
}
else {
r = Math.floor(Math.random() * badObjects.length);
classRef = getDefinitionByName(badObjects
newObject = new classRef ();
newObject.typestr = "bad";
}
newObject.y = -300;
newObject.x = 1024;
newObject.x = (Math.random() * 400) + (Math.random()*400);
addChild(newObject);
objects.push(newObject);
setNextObject();
}
public function moveObjects(e:Event) {
for (var i: int = objects.length -1; i >= 0; i--) {
objects.y += speed;
addChild(objects);
if (objects.y > 750) {
removeChild(objects);
objects.splice(i, 1);
}
if (objects.hitTestObject(catcher.mouth_mc)) {
if (objects.typestr == "liquid") {
score += 5;
soundfx_1.play();
catcher.mouth_mc.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
if (fl_SecondsElapsed == 0)
{
removeChild(objects);
objects.splice(i, 1);
}
catcher.x = mouseX;
checkStageBorder();
}
}
public function fl_TimerHandler(Event:TimerEvent):void
{
fl_SecondsElapsed--;
TimerDisplay.text = String(fl_SecondsElapsed);
if(fl_SecondsElapsed == 10)
{
soundfx_3.play();
}
if (fl_SecondsElapsed == 0 && score >= 70)
{
stopGame();
var myrequest:URLRequest = new URLRequest("Congratulatory_Screen_2.swf");
var myloader:Loader = new Loader();
myloader.addEventListener("UnloadMe", unloadFunction);
myloader.load(myrequest);
SoundMixer.stopAll();
stage.addChild(myloader);
removeChild(Score_board);
removeChild(Timer_board);
removeChild(ScoreDisplay);
removeChild(TimerDisplay);
}
else if (fl_SecondsElapsed == 0 && score < 70)
{
stopGame();
var myrequest1:URLRequest = new URLRequest("Game_Over_2.swf");
var myloader1:Loader = new Loader();
myloader1.addEventListener("UnloadMe", unloadFunction_1);
myloader1.load(myrequest1);
SoundMixer.stopAll();
stage.addChild(myloader1);
removeChild(Score_board);
removeChild(Timer_board);
removeChild(ScoreDisplay);
removeChild(TimerDisplay);
}
}
public function unloadFunction(event:Event) {
Loader(event.currentTarget).unloadAndStop();
}
public function unloadFunction_1(event:Event) {
Loader(event.currentTarget).unloadAndStop();
}
public function checkStageBorder()
{
ObjectHalfWidth = catcher.width / 2;
if (catcher.x - ObjectHalfWidth < 0)
{
catcher.x = 18 + ObjectHalfWidth;
}
else if (catcher.x + ObjectHalfWidth > stage.stageWidth)
{
catcher.x = stage.stageWidth - ObjectHalfWidth + 17;
}
}
public function stopGame()
{
for (var i:int = 0; i < objects.length; i++)
{
this.removeChild(objects);
objects.splice(i, 1);
nextObject.stop();
}
this.removeChild(catcher);
}
}
}
Copy link to clipboard
Copied
i think it is doing nothing except making you think it is doing something.
to confirm it's doing nothing, put a trace() in your unloadme listener function and see if it's ever traced.
Copy link to clipboard
Copied
you're right it's not tracing anything. is my way of loading and unloading swf files correct? if not, how can i correct them?
Copy link to clipboard
Copied
is my way of loading and unloading swf files correct? *
Copy link to clipboard
Copied
1. remove all that unload code.
2. it looks like you only need to load, at most, one swf at any given time. if that's true, you should only use one loader. that way you don't have to unload anything, just reuse the same loader repeatedly.
Copy link to clipboard
Copied
i removed the unload code, and only use one loader but still it keeps going back to stage 2. maybe i did the one loader thing wrong. how do you manage to do that?
Copy link to clipboard
Copied
copy and paste your updated code.
Copy link to clipboard
Copied
//stage2
package
{
import flash.automation.StageCaptureEvent;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.display.Stage;
import flash.utils.getDefinitionByName;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundMixer;
public class Stage2 extends MovieClip {
var catcher:Catcher;
var nextObject:Timer;
var objects:Array = new Array();
var ObjectHalfWidth: Number;
const speed: Number = 8.0;
var score: int = 0;
var fl_TimerInstance:Timer = new Timer(1000, 60);
var fl_SecondsElapsed:Number = 60;
var music_1:Sound = new Sound(new URLRequest("Cheerful_Theme.mp3"));
var soundfx_1:Sound = new Sound(new URLRequest("Catch_Good.mp3"));
var soundfx_2:Sound = new Sound(new URLRequest("Catch_Bad.mp3"));
var soundfx_3:Sound = new Sound(new URLRequest("Ticking_Clock.mp3"));
var myloader:Loader = new Loader();
var myloader1:Loader = new Loader();
var myrequest:URLRequest = new URLRequest("Congratulatory.swf");
public function Stage2 () {
catcher = new Catcher ();
catcher.y = 768;
catcher.x = 120;
addChild(catcher);
catcher.mouth_mc.gotoAndStop(1);
setNextObject();
addEventListener(Event.ENTER_FRAME, moveObjects);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();
music_1.play();
}
public function setNextObject() {
nextObject = new Timer(1000 + Math.random() * 1500, 1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);
nextObject.start();
if (fl_SecondsElapsed == 0)
{
nextObject.stop();
}
}
public function newObject(e:TimerEvent) {
var solidObjects:Array = ["Chocolate","Apple", "Laptop", "USB"];
var liquidObjects:Array = ["Coffee", "CookingOil", "Milk", "Water"];
var badObjects:Array = ["Fire"];
var randomNum:Number = Math.random();
if ((randomNum >= .1) && (randomNum <= .4)) {
var r:int = Math.floor(Math.random() * solidObjects.length);
var classRef:Class = getDefinitionByName(solidObjects
var newObject:MovieClip = new classRef();
newObject.typestr = "solid";
}
else if ((randomNum >= .5) && (randomNum <= 1)) {
r = Math.floor(Math.random() * liquidObjects.length);
classRef = getDefinitionByName(liquidObjects
newObject = new classRef ();
newObject.typestr = "liquid";
}
else {
r = Math.floor(Math.random() * badObjects.length);
classRef = getDefinitionByName(badObjects
newObject = new classRef ();
newObject.typestr = "bad";
}
newObject.y = -300;
newObject.x = 1024;
newObject.x = (Math.random() * 400) + (Math.random()*400);
addChild(newObject);
objects.push(newObject);
setNextObject();
}
public function moveObjects(e:Event) {
for (var i: int = objects.length -1; i >= 0; i--) {
objects.y += speed;
addChild(objects);
if (objects.y > 750) {
removeChild(objects);
objects.splice(i, 1);
}
if (objects.hitTestObject(catcher.mouth_mc)) {
if (objects.typestr == "liquid") {
score += 5;
soundfx_1.play();
catcher.mouth_mc.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
if (fl_SecondsElapsed == 0)
{
removeChild(objects);
objects.splice(i, 1);
}
catcher.x = mouseX;
checkStageBorder();
}
}
public function fl_TimerHandler(Event:TimerEvent):void
{
fl_SecondsElapsed--;
TimerDisplay.text = String(fl_SecondsElapsed);
if(fl_SecondsElapsed == 10)
{
soundfx_3.play();
}
if (fl_SecondsElapsed == 0 && score >= 80)
{
stopGame();
myloader.load(myrequest);
SoundMixer.stopAll();
stage.addChild(myloader);
removeChild(Score_board);
removeChild(Timer_board);
removeChild(ScoreDisplay);
removeChild(TimerDisplay);
}
else if (fl_SecondsElapsed == 0 && score < 60)
{
stopGame();
var myrequest1:URLRequest = new URLRequest("Game_Over_2.swf");
myloader1.load(myrequest1);
SoundMixer.stopAll();
stage.addChild(myloader1);
removeChild(Score_board);
removeChild(Timer_board);
removeChild(ScoreDisplay);
removeChild(TimerDisplay);
}
}
public function checkStageBorder()
{
ObjectHalfWidth = catcher.width / 2;
if (catcher.x - ObjectHalfWidth < 0)
{
catcher.x = 18 + ObjectHalfWidth;
}
else if (catcher.x + ObjectHalfWidth > stage.stageWidth)
{
catcher.x = stage.stageWidth - ObjectHalfWidth + 17;
}
}
public function stopGame()
{
for (var i:int = 0; i < objects.length; i++)
{
this.removeChild(objects);
objects.splice(i, 1);
nextObject.stop();
}
this.removeChild(catcher);
}
}
}
//Congratulatory_Screen_1
next_stage.addEventListener(MouseEvent.CLICK, loadStage_2);
var music_1:Sound = new Sound(new URLRequest("Congrats_Theme.mp3"));
music_1.play();
var myrequest:URLRequest=new URLRequest("Stage2.swf");
var myloader:Loader=new Loader();
function loadStage_2(event:MouseEvent):void
{
myloader.load(myrequest);
stage.addChild(myloader);
removeChildAt(0);
removeChild(next_stage);
SoundMixer.stopAll();
}
Copy link to clipboard
Copied
i have deleted the unload code and used only one loader but still the stage2.swf loads the congratulatory_screen_1.swf even though the url i used is supposed to load the congratulatory_screen_2.swf. this happens when i load it from the first level but when i load it from stage2.swf it correctly loads the next swf. why is that?
Copy link to clipboard
Copied
assuming the top snippet is stage2.swf and it loads a congratuloary.swf that uses that 2nd snippet, your code shows stage2.swf loading congratulatory.swf and congratulotory.swf loading stage2.swf and that's a problem. fix that.
Copy link to clipboard
Copied
I've finallly figured out what the problem is. It was the button that I copied and paste from a different swf. Thank you for all the help.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now