Harry Hutchinson
Harry Hutchinson

Reputation: 13

Action Script 3, Flash Develop. Error I don't know how to resolve


The error I am getting is:

F:\Game Techniques\FireBoy&WaterGirl\src\Hero.as(11): col: 3: Error:
An Embed variable must not have an existing value.

[Embed(source="../assets/FireBoy.jpg")]

Please can you explain what this means and how I resolve it.

Here is the full code

package  
{
    import flash.display.Bitmap;
    import flash.display.Sprite;

    /**
     * ...
     * @author Harry
     */
    public class Hero extends Sprite
    {
        [Embed(source="../assets/FireBoy.jpg")]

        public var grav:int = 0;
        public var floor:int = 580;

        private static const HeroFireBoy:Class;
        private var FireBoy:Bitmap;

        public function Hero() 
        {
            FireBoy = new Hero.HeroFireBoy();
            scaleX = 0.1;
            scaleY = 0.1;

            addChild(FireBoy);
        }
        public function adjust():void 
        {
            FireBoy.y += grav;
            if(FireBoy.y+FireBoy.height/2<floor)
                grav++;
            else 
            {
                grav = 0;
                FireBoy.y = floor - FireBoy.height / 2;
            }
            if (FireBoy.x - FireBoy.width / 2 < 0)
                FireBoy.x = FireBoy.width / 2;
            if (FireBoy.x + FireBoy.width / 2 > 800)
                FireBoy.x = 800 - FireBoy.width / 2;
        }

    }

}

Upvotes: 0

Views: 278

Answers (2)

Kir
Kir

Reputation: 21

It can happen if you after line:

[Embed(source="../assets/FireBoy.jpg")]

declare some variable with value. For example:

[Embed(source="../assets/FireBoy.jpg")] private var fireBoyClass:Class = null;

Generally, after [Embed(source="../assets/FireBoy.jpg")] next line should be declaration of variable that associated with embedded source.

public class SomeClass 
{
    [Embed(source = "../assets/FireBoy.jpg")]
    private var fireBoyClass:Class;

In your case just move line private static const HeroFireBoy:Class; under [Embed(source = "../assets/FireBoy.jpg")]

Finaly it will looks like:

public class Hero extends Sprite
{
    [Embed(source = "../assets/FireBoy.jpg")]
    private static const HeroFireBoy:Class;

    public var grav:int = 0;
    public var floor:int = 580;

Upvotes: 2

divillysausages
divillysausages

Reputation: 8033

To give some more info on @Kir's answer; when you embed something, you need to associate it with with a variable of type Class, so that you can create it later. This is done via:

[Embed(source = "../assets/FireBoy.jpg")]
private var fireBoyClass:Class;

Because, in your example, the line with fireBoyClass doesn't exist, Flash thinks that you're trying to associate it with the line

public var grav:int = 0;

Which is what's giving you the error.

Once you have your Class property declared, you can then create an instance of your embedded data:

public var fireBoyBitmap:Bitmap = ( new fireBoyClass ) as Bitmap;

This post (http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf60546-7ff2.html) gives a good overview of the [Embed] metatag - just concentrate on the code between the <fx:Script> and ignore the [Bindable] metatag - that's only useful for flex.

Upvotes: 0

Related Questions