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.
Copy link to clipboard
Copied
if the images folder is in the same folder as your published animate files, that should be:
"./images/background"
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
As kglad said, except the dot in the beginning. Try "/images/background.jpg"
Copy link to clipboard
Copied
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.