Adding a background for noob


(Oli) #1

here is what I have

This is my World:

package worlds { import entities.Background; import entities.Player; import net.flashpunk.graphics.Backdrop; import net.flashpunk.graphics.Image; import net.flashpunk.FP

import net.flashpunk.World;

public class GameWorld extends World
{
	public var playerEntity:Player;
	
	public function GameWorld()
	{
		super();
	}
	
	public override function begin():void
	{
		add(new Background)
		
		playerEntity = new Player();
		add(playerEntity);
	}
}

}

This is my background class:

package entities { import net.flashpunk.Entity; import net.flashpunk.FP; import net.flashpunk.graphics.Backdrop import flash.display.Graphics import net.flashpunk.graphics.Image;

public class 

Background extends Entity { [Embed(source = “…/…/assets/graphics/map layout togheter2.png”)] private const BACKGROUND:Class;

	public function Background() 
	{
		BACKGROUND = new Backdrop
	}
	
}

}

Does anyone know what I do wrong


(Jacob Albano) #2

First of all:

package entities
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Backdrop;
	import flash.display.Graphics;
	import net.flashpunk.graphics.Image;

	bublic class Background extends Entity
	{
		[Embed(source = "../../assets/graphics/map layout togheter2.png")]
		private const BACKGROUND:Class;

		public function Background() 
		{
			BACKGROUND = new Backdrop
		}

	}
}

Much better.

Does any of this even compile?

bublic class ...

Should be “public”.

BACKGROUND = new Backdrop

You’re assigning to a constant with an incompatible conversion (Backdrop to Class). The computer should have given you some sort of helpful error.

In any case, here’s what you need to do:

    public function Background()
    {
        graphic = new Backdrop(new BACKGROUND);
    }


(Oli) #3

I got it by simply putting it into the world. not by making another class just for it. I feel so dumb now :frowning: whatever thx I really apreciate the quick help


(Jacob Albano) #4

There’s no need to feel dumb; I’m just surprised that you weren’t able to fix the problem based on the information in the compiler errors. There were a few serious mistakes in there that should have been caught at compile time.


(Ultima2876) #5

Minor correction:

public function Background()
{
    graphic = new Backdrop(BACKGROUND);
}

FlashPunk creates instances of graphic types from the raw class reference; you don’t need to instantiate the class before passing it to the Backdrop constructor. It’s probably not a big deal, but it might cause a bit of garbage shrugs.


(Jacob Albano) #6

Huh, good to know. I haven’t used embedded assets directly for a while now, so my best-practices aren’t entirely up to date. :smile: