• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

actionscript3 problem embedding images

Community Beginner ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

Hi all , i am following a tutorial. 

The code below should load two images into my scene.

I have an .as file and an .fla file in a folder called src and two images in a folder called images. I am compiling my .as file in Adobe Animate.

When I try to embed my images using:

 

[Embed(source="../images/background.jpg")]

 

it does not want to work.

Nothing is loaded and there are no errors compiled.

Here is my code. It is simple. It should load  background.jpg and  character.png from the images folder.In order to control the character onstage with keyboard controls.

 

package

{

import flash.display.Sprite;

import flash.display.DisplayObject;

import flash.events.KeyboardEvent; 

import flash.ui.Keyboard;

import flash.events.Event;

 

public class BasicScrolling extends Sprite

{

//Embed the background image

[Embed(source="../images/background.jpg")]

public var BackgroundImage:Class;

public var backgroundImage:DisplayObject = new BackgroundImage();

public var background:Sprite = new Sprite();

 

//Embed the character image

[Embed(source="../images/character.png")]

public var CharacterImage:Class;

public var characterImage:DisplayObject = new CharacterImage();

public var character:Sprite = new Sprite();

 

//Create and initialize the vx and vy variables

public var vx:int = 0;

public var vy:int = 0;

 

public function BasicScrolling()

{

 

//Add the background

background.addChild(backgroundImage);

stage.addChild(background);

background.x = -1325;

background.y = -961;

 

//Add the character

character.addChild(characterImage);

stage.addChild(character);

character.x = 225;

character.y = 150;

 

//Add the event listeners

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); 

stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

}

 

public function keyDownHandler(event:KeyboardEvent):void

{

if (event.keyCode == Keyboard.LEFT)

{

vx = -5;

}

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

{

vx = 5;

}

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

{

vy = -5;

}

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

{

vy = 5;

}

}

public function keyUpHandler(event:KeyboardEvent):void

{

if (event.keyCode == Keyboard.LEFT 

|| event.keyCode == Keyboard.RIGHT)

{

vx = 0;

else if (event.keyCode == Keyboard.DOWN 

|| event.keyCode == Keyboard.UP)

{

vy = 0;

}

public function enterFrameHandler(event:Event):void

{

//Move the background 

background.x -= vx; 

background.y -= vy; 

 

//Check the stage boundaries

if (background.x > 0)

{

background.x = 0;

}

else if (background.y > 0)

{

background.y = 0;

}

 

else if (background.x < stage.stageWidth - background.width)

{

background.x = stage.stageWidth - background.width;

}

 

else if (background.y < stage.stageHeight - background.height)

{

background.y = stage.stageHeight - background.height;

}

}

}

 

I pasted all of my code here. I hope that this is ok.

Any help is mot appreciated. 

Views

374

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

if the images folder is in the same folder as your published animate files, that should be:

 

"./images/background"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

No, unfortunately that did not work. Thanks for the input.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

whether it works or not depends on the image folder's relative (to your published file's) location

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

Nice one for your help. so I have a folder called BasicScrolling, inside here is a folder called src and an images folder. Inside of src is my .fla and .as files and inside of folder called images are my background.jpg and character.png.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

your html and as files and images folder are all in src? if so, "./images/background.jpg" and "./images/character.png" and the correct source references.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 16, 2019 Sep 16, 2019

Copy link to clipboard

Copied

As kglad said, except the dot in the beginning. Try "/images/background.jpg"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 17, 2019 Sep 17, 2019

Copy link to clipboard

Copied

LATEST

the correct format is:

[Embed(source="../images/background.jpg")]

Its now working.

I had a typo in my class name.

Thanks for all the help guys.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines