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

problem embedding image files with actionscript3

Community Beginner ,
Sep 16, 2019 Sep 16, 2019

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.

I am using an .as file to control an .fla file compiling with Adobe Animate. The tutorial is using Flash Builder. Im not sure if I can use this format:

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


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;

//[SWF(width="550", height="400",
//backgroundColor="#FFFFFF", frameRate="60")]

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 TRACE():void{trace();}
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 most appreciated.

180
Translate
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
no replies

Have something to add?

Join the conversation