Entity being created, but not added to world


(Alice Davies) #1

So I’m new to flashpunk and I’ve been following the official tutorials. Everything was fine until I got to using spritemaps. When I use add(new Player()); the player gets created and updates, but is not actually added to the world. It’s nothing to do with the change to a using a spritemap, as I changed it back to using a single image and it still happened.

Main.as:

package
{
    import net.flashpunk.Engine;
    import net.flashpunk.FP;

    /**
     * ...
     * @author Alice Davies
     */

    public class Main extends Engine 
    {
	public function Main():void
	{
		super(800, 600, 60, false);
		FP.world = new MainMenu;
	}
	
	override public function init():void
	{
		
	}
    }
}

MainMenu.as:

package 
{
    import net.flashpunk.World;

    /**
     * ...
     * @author Alice Davies
     */

    public class MainMenu extends World
    {
	public function MainMenu() 
	{
		add(new Player());
	}
    }
}

Player.as:

package 
{
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.graphics.Spritemap;
    import net.flashpunk.utils.Input;

    /**
     * ...
     * @author Alice Davies
     */

    public class Player extends Entity
    {
	[Embed(source = "assets/player.png")]
	private const SPRITE_SHEET:Class;
	
	public var sprPlayer:Spritemap = new Spritemap(SPRITE_SHEET, 128, 128);
	
	private var speed:Number;
	
	private var targetX:Number;
	private var targetY:Number;
	
	public function Player() 
	{
		sprPlayer.add("idle", [0, 1], 2, true);
		sprPlayer.add("runLeft", [2, 3], 2, true);
		sprPlayer.add("runRight", [4, 5], 2, true);
		graphic = sprPlayer;
		sprPlayer.play("idle");
		trace("Player constructor");
		speed = 5;
		name = "player";
		type = "player";
	}
	
	override public function update():void
	{
		if (Input.mousePressed)
		{
			trace("move command");
			targetX = Input.mouseX
			targetY = Input.mouseY;
		}
		
		moveTowards(targetX, targetY, speed, null, false);
	}
    }
}

Maybe I’m missing something but looking at the tutorials it doesn’t seem like I’m doing anything wrong.


#2

sure, the entity dont added to the stage???

look for the graphic in the spritemap, is it perhaps a little bit greater than 128x128? then it will not displayed… try in function Main() of the main.as:

FP.console.enable();

look then in the top-right corner… is there “1 Entity” ??


#3

for what do you overriding the superclass-function init in the main.as???


(Martí Angelats i Ribera) #5

Be carefull with the constructors.

Main.as

package
{
    import net.flashpunk.Engine;
    import net.flashpunk.FP;

    public class Main extends Engine 
    {
	public function Main():void
	{
		super(800, 600, 60, false);
	}
	
	override public function init():void
	{
		FP.world = new MainMenu;
	}
    }
}

MainMenu.as:

package 
{
    import net.flashpunk.World;

    public class MainMenu extends World
    {
	override public function begin():void 
	{
		add(new Player());
	}
    }
}

Player.as:

package 
{
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Spritemap;

    public class Player extends Entity
    {
	[Embed(source = "assets/player.png")]
	private const SPRITE_SHEET:Class;
	
	public var sprPlayer:Spritemap;
	
	private var speed:Number;
	
	private var targetX:Number;
	private var targetY:Number;
	
	public function Player() 
	{
		speed = 5;
		name = "player";
		type = "player";
		
		sprPlayer = new Spritemap(SPRITE_SHEET, 128, 128);
		sprPlayer.add("idle", [0, 1], 2, true);
		sprPlayer.add("runLeft", [2, 3], 2, true);
		sprPlayer.add("runRight", [4, 5], 2, true);
		
		super(0, 0, sprPlayer);
		
		sprPlayer.play("idle");
		trace("Player constructor");
	}
	
	//[...]
    }
}

PD: If this doesn’t work, download the lastest version of FlashPunk from GitHub and try using it.


(Alice Davies) #7

When I use the console it says there is 1 entity but when I try to access it (For example with getInstance), it doesn’t get returned, which is why I initially thought it wasn’t being added.

I don’t know what you mean by “look for the graphic in the spritemap” @kolibri250


(Martí Angelats i Ribera) #8

Have a look to the Player class i wrote.

Also (if it doesn’t work) update FP.


(Alice Davies) #9

I already have the latest version of FP as I only downloaded it today. I changed the code to what you put but it still didn’t work. The player doesn’t show on the screen and when I try to trace the x and y position of the entity they come up as “NaN”. I’m not so much looking for a fix as I am wanting to know why it’s not working/what I’ve done wrong. The code for creating the world, adding the entity and creating the spritemap are taken straight from the tutorial so there’s no reason for it not to work, and it was in fact working earlier.


#10

try the same with an image or stamp…

then you can exclude, the error is in the handling of the spritemap!

public class Player extends Entity { [Embed(source = “assets/player.png”)] private const SPRITE_SHEET:Class;

public var sprPlayer:Stamp = new Stamp(SPRITE_SHEET);

private var speed:Number;

private var targetX:Number;
private var targetY:Number;

public function Player() 
{
	/*sprPlayer.add("idle", [0, 1], 2, true);
	sprPlayer.add("runLeft", [2, 3], 2, true);
	sprPlayer.add("runRight", [4, 5], 2, true);*/

	graphic = sprPlayer;
	/*sprPlayer.play("idle");*/
	trace("Player constructor");
	speed = 5;
	name = "player";
	type = "player";
}

and cancel the whole line “moveTowards” in the update-function… if you see the graphic, then you can programm moving… if you dont see anything… you must in first solve the basic-problems…


(Martí Angelats i Ribera) #11

It seems an stupid question but you have the image you are embeding there? (Does assets/player.png exist?)


#12

hehehehe i thought the same :slight_smile:


(Alice Davies) #13

Still the same issue, regardless of whether I use Image, Stamp or Spritemap. And yes, the image exists.

The thing that’s confusing me the most right now is the fact that the x and y value of the entity are never being set (Always NaN) despite my attempts to manually set them.


#14

and whats are the positions of x & y of the sprite itself???

try in player:

this.x=100;
this.y=100;
sprPlayer.x=100;
sprPlayer.y=100;

and see, what happenz…


(Alice Davies) #15

Thanks for the help everyone, it’s working now. I think the problem was targetX and targetY were never set, so the moveTowards call messed up the values of this.x and this.y, causing them to be NaN


#16

and cancel the whole line “moveTowards” in the update-function… if you see the graphic, then you can programm moving… if you dont see anything… you must in first solve the basic-problems…

:wink:


(Martí Angelats i Ribera) #17

Could you post an image of your project file tree structure?