Can't get entity to move


(Dan G) #1

Hi, I am new to flashpunk and am having trouble getting my entity to move across the screen. I’ve followed a few different tutorials and replicated their code, but it does not seem to move. These are my classes:

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

public class Main extends Engine 
{
	private var gameWorld:MyWorld;
	public function Main() 
	{
		super(800, 600, 60, false);
		gameWorld = new MyWorld;
	}
	
	override public function init():void
	{
		FP.world = gameWorld;
		trace("FlashPunk has started successfully!");
	}
	
}

}

package { import net.flashpunk.World;

public class MyWorld extends World
{
	private var gameEntity:MyEntity;
	public function MyWorld()
	{
		gameEntity = new MyEntity();
		add(gameEntity);
		
	}
	
	override public function update():void
	{
		//trace("MyEntity updates");
		
	}
}

}

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

public class MyEntity extends Entity
{
	[Embed(source = "dragon.png")] private const IMAGE:Class;
	
	public function MyEntity() 
	{
			graphic = new Image(IMAGE);
			setHitbox(50, 50);
			width = 50;
			height = 50;
	}
	
	override public function update():void
	{
		x += 100;
		y += 50;
		
		if (Input.check(Key.LEFT)) { x -= 5; }
		if (Input.check(Key.RIGHT)) { x += 5; }
		if (Input.check(Key.UP)) { y -= 5; }
		if (Input.check(Key.DOWN)) { y += 5; }
	}
	
	override public function added():void
	{
		trace("Entity added");
	}
	
}

}

Any help?


(christopf) #2

hi dan. try to add this

this.x = 100; this.y = 50;

into the constructor (public function MyEntity() ) and remove the x and y variables from the update function.
the update function applies whats written in there every frame and what you got now should move your entity automatically every frame by 100 on the x axis to right and by 50 on the y axis to south.


(Dan G) #4

First of all, thanks for the reply. I am confused, however. If the update function applies whats written in the constructor every frame, wouldn’t it be creating a new entity every frame? Shouldn’t setting x and y in the update function move the entity?


(christopf) #5

The update function applies only everything written in itself every frame. In your original post you had the x and y value in there. but if you put it to the constructor (that got only called in the very beginning) you have a fixed beginning value for x and y and with the update function you check every frame if it changed.


(Dan G) #6

I tried to do what you suggested in your original reply. It sets the x and y coordinates, but it does no movement. I just don’t understand why my update function isn’t updating the x and y of my entity.


(Dan G) #7

I dont have a “lib” folder under my project where bin and src are, is that relevant?


(Bora Kasap) #8

your lib is your src already…

what kind of a movement do you wish? from the keys you’ve defined or a time based fixed movement? if you mean keys, you’ve the code, it should work… or if you wish a fixed timed movement, also add this

x += 100;
y += 50;

stuff to your update function back but use them like that

x += 100*FP.elapsed;
y += 50*FP.elapsed;

(Bora Kasap) #9

It’s better you read some tutorials first…

Whole Tutorials

Flashpunk Basics

Timin & Motion


(azrafe7) #10

… And don’t forget to add

super.update();

in your World class.


(Dan G) #11

This was the problem, thank you!