Skip to main content
diegob96753824
Participating Frequently
February 1, 2018
Answered

LOADING SWF CODE SNIPPET NOT WORKING

  • February 1, 2018
  • 1 reply
  • 849 views

Hi!

I recently got started with animate cc and I am having a bit of trouble with this snippet code for AS3. I added my load/download .swf code snippet to a symbol and in the actions panel I added the location of my SWF file (this file is another animate cc project which I exported as .swf in the same folder). When I run the project and click to load the .swf file I get no response and some errors in the output panel:

Error #2044: Unhandled ioError:. text=Error #2035: URL Not Found. URL: file:///C:Usersdiego_000Desktopsave_the_refugeessnake_game.swf

Error #2044: Unhandled ioError:. text=Error #2035: URL Not Found. URL: file:///C:/Users/diego_000/Desktop/save_the_refugees/snake_game.html

Am I doing something wrong? Any help will be much appreciated.

Best regards

Diego

This is the snippet code I have:

btn_ayllan.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);

import fl.display.ProLoader;

var fl_ProLoader: ProLoader;

//Esta variable realiza un seguimiento de las veces que quiere cargar o descargar el archivo SWF

var fl_ToLoad: Boolean = true;

function fl_ClickToLoadUnloadSWF(event: MouseEvent): void {

if (fl_ToLoad) {

fl_ProLoader = new ProLoader();

fl_ProLoader.load(new URLRequest("C:\Users\diego_000\Desktop\save_the_refugees\snake_game.swf"));

addChild(fl_ProLoader);

} else {

fl_ProLoader.unload();

removeChild(fl_ProLoader);

fl_ProLoader = null;

}

// Cambia si quiere cargar o descargar el archivo SWF

fl_ToLoad = !fl_ToLoad;

}

/* Detener en este fotograma

La línea de tiempo de Animate se detendrá/pausará en el fotograma en el que se inserte este código.

También se puede utilizar para detener/pausar la línea de tiempo de clips de película.

*/

stop();

/* Hacer clic para cargar/descargar archivo SWF o imagen desde una dirección URL.

Al hacer clic en la instancia del símbolo, se carga y se visualiza la dirección URL especificada del archivo SWF o de la imagen. Al hacer clic en la instancia del símbolo una segunda vez, se descarga el archivo SWF o la imagen.

Instrucciones:

1. Reemplace "http://www.helpexamples.com/flash/images/image1.jpg" a continuación por la dirección URL que desee para el archivo SWF o la imagen. Conserve las comillas ("").

2. Los archivos de dominios de Internet distintos del dominio en el que reside el archivo SWF al que se llama no se pueden cargar sin una configuración especial.

*/

btn_ayllan.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_2);

import fl.display.ProLoader;

var fl_ProLoader_2:ProLoader;

//Esta variable realiza un seguimiento de las veces que quiere cargar o descargar el archivo SWF

var fl_ToLoad_2:Boolean = true;

function fl_ClickToLoadUnloadSWF_2(event:MouseEvent):void

{

if(fl_ToLoad_2)

{

fl_ProLoader_2 = new ProLoader();

fl_ProLoader_2.load(new URLRequest("file:///C:/Users/diego_000/Desktop/save_the_refugees/snake_game.html"));

addChild(fl_ProLoader_2);

}

else

{

fl_ProLoader_2.unload();

removeChild(fl_ProLoader_2);

fl_ProLoader_2 = null;

}

// Cambia si quiere cargar o descargar el archivo SWF

fl_ToLoad_2 = !fl_ToLoad_2;

}

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

The error indicates that there is a problem with the path to the SWF file.

- Make sure the path is correct and that the SWF is really in that folder.

- Use a path relative to your main SWF instead of a absolute path. For example:

fl_ProLoader.load(new URLRequest("swf/snake_game.swf"));

- Use forward slashes ("/") in your path.

Code example:

import fl.display.ProLoader;

import flash.events.MouseEvent;

import flash.net.URLRequest;

var fl_ProLoader:ProLoader;

var fl_ToLoad:Boolean = true;

function fl_ClickToLoadUnloadSWF(event:MouseEvent):void

{

    if (fl_ToLoad)

    {

        fl_ProLoader = new ProLoader();

        fl_ProLoader.load(new URLRequest("swf/snake_game.swf"));

        addChild(fl_ProLoader);

    }

    else

    {

        fl_ProLoader.unload();

        removeChild(fl_ProLoader);

        fl_ProLoader = null;

    }

    fl_ToLoad = !fl_ToLoad;

}

btn_ayllan.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);

I hope it helps.

Regards,

JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
February 1, 2018

Hi.

The error indicates that there is a problem with the path to the SWF file.

- Make sure the path is correct and that the SWF is really in that folder.

- Use a path relative to your main SWF instead of a absolute path. For example:

fl_ProLoader.load(new URLRequest("swf/snake_game.swf"));

- Use forward slashes ("/") in your path.

Code example:

import fl.display.ProLoader;

import flash.events.MouseEvent;

import flash.net.URLRequest;

var fl_ProLoader:ProLoader;

var fl_ToLoad:Boolean = true;

function fl_ClickToLoadUnloadSWF(event:MouseEvent):void

{

    if (fl_ToLoad)

    {

        fl_ProLoader = new ProLoader();

        fl_ProLoader.load(new URLRequest("swf/snake_game.swf"));

        addChild(fl_ProLoader);

    }

    else

    {

        fl_ProLoader.unload();

        removeChild(fl_ProLoader);

        fl_ProLoader = null;

    }

    fl_ToLoad = !fl_ToLoad;

}

btn_ayllan.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);

I hope it helps.

Regards,

JC

diegob96753824
Participating Frequently
February 1, 2018

Hi JC!

The error seems to have been solved. Thanks! But now I get another error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at Main/addFood()

at Main/init()

at Main()

Do you know how could I solve this?

Thank you once again!

This is the code of the .swf I am trying to load on my project:

package {

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event; //used for ENTER_FRAME event

public class Main extends MovieClip {

const speed: int = 10; //speed of the snake

var score: int;

var vx: int;

var vy: int;

var gFood: Food;

var head: SnakePart;

var SnakeDirection: String;

var snake: Array;

public function Main() {

init();

}

function init(): void {

//Initialize everything!

vx = 1;

vy = 0;

score = 0;

snake = new Array();

SnakeDirection = "";

//add food to the stage

addFood();

//add snakes head to the stage

head = new SnakePart();

head.x = stage.stageWidth / 2;

head.y = stage.stageHeight / 2;

snake.push(head);

addChild(head);

stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);

addEventListener(Event.ENTER_FRAME, onEnterFrame);

//ENTER_FRAME listener is attached to main class and not to the stage directly

}

//This function will add food to the stage

function addFood(): void {

gFood = new Food();

gFood.x = 50 + Math.random() * (stage.stageWidth - 100);

gFood.y = 50 + Math.random() * (stage.stageHeight - 100);

addChild(gFood);

}

//this function will reset the game

function reset(): void {

removeChild(gFood);

addFood();

head.x = stage.stageWidth / 2;

head.y = stage.stageHeight / 2;

vx = 1;

vy = 0;

for (var i = snake.length - 1; i > 0; --i) {

removeChild(snake);

snake.splice(i, 1);

}

}

function onKeyDownHandler(event: KeyboardEvent): void {

if (event.keyCode == Keyboard.LEFT) {

SnakeDirection = "left";

} else if (event.keyCode == Keyboard.RIGHT) {

SnakeDirection = "right";

} else if (event.keyCode == Keyboard.UP) {

SnakeDirection = "up";

} else if (event.keyCode == Keyboard.DOWN) {

SnakeDirection = "down";

}

}

function onKeyUpHandler(event: KeyboardEvent): void {

if (event.keyCode == Keyboard.LEFT) {

SnakeDirection = "";

} else if (event.keyCode == Keyboard.RIGHT) {

SnakeDirection = "";

} else if (event.keyCode == Keyboard.UP) {

SnakeDirection = "";

} else if (event.keyCode == Keyboard.DOWN) {

SnakeDirection = "";

}

}

function onEnterFrame(event: Event): void {

//setting direction of velocity

if (SnakeDirection == "left" && vx != 1) {

vx = -1;

vy = 0;

} else if (SnakeDirection == "right" && vx != -1) {

vx = 1;

vy = 0;

} else if (SnakeDirection == "up" && vy != 1) {

vx = 0;

vy = -1;

} else if (SnakeDirection == "down" && vy != -1) {

vx = 0;

vy = 1;

}

//collison with stage

if (head.x - head.width / 2 <= 0) {

score = 0;

reset();

}

if (head.x + head.width / 2 >= stage.stageWidth) {

score = 0;

reset();

}

if (head.y - head.height / 2 <= 0) {

score = 0;

reset();

}

if (head.y + head.height / 2 >= stage.stageHeight) {

score = 0;

reset();

}

//move body of the snake

for (var i = snake.length - 1; i > 0; --i) {

snake.x = snake[i - 1].x;

snake.y = snake[i - 1].y;

}

//changing the position of snake's head

head.x += vx * speed;

head.y += vy * speed;

//collision with tail

for (i = snake.length - 1; i >= 1; --i) {

if (snake[0].x == snake.x && snake[0].y == snake.y) {

reset();

break;

}

}

//collision with food

if (head.hitTestObject(gFood)) {

score += 1;

removeChild(gFood);

addFood();

var bodyPart = new SnakePart();

bodyPart.x = snake[snake.length - 1].x;

bodyPart.y = snake[snake.length - 1].y;

snake.push(bodyPart);

addChild(bodyPart);

}

//display scores

txtScore.text = String(score);

}

}

}

JoãoCésar17023019
Community Expert
Community Expert
February 1, 2018

Great!

You have to make sure your swf is in the stage. Change your constructor function and add an addedToStage handler like this:

public function Main()

{

    addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

}

public function addedToStageHandler(e:Event):void

{

    removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

    init();

}

function init():void

{

    // same code as before

}