My newly system fails to move ship [SOLVED]


(Granit Bajraktari) #1

So I had a working way to move the PlayerShip that was like this : if (playerclicksdown) y += 10 etc. But I wanted to do something more advanced so I made this :

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

/**
 * ...
 * @author Law
 */
public class PlayerShip extends Entity {
	
	[Embed(source = "../assets/PlayerShipImage.png")]
	private const IMAGE:Class;
	
	private const MAX_UP_SPEED:int = 3;
	private const MAX_DOWN_SPEED:uint = 2;
	private var  CURRENT_SPEED:int;
	private const ACCELRATION:uint = 0.5;
	
	public function PlayerShip() {
		
		graphic = new Image(IMAGE);
		
		x = 200;
		y = 400;
		
		setHitboxTo(graphic);
		
		Input.define("up", Key.W, Key.UP);
		Input.define("down", Key.S, Key.DOWN);
		Input.define("left", Key.A, Key.LEFT);
		Input.define("right", Key.D, Key.RIGHT);
		
	}
	
	override public function update():void {
		
		if (Input.check("up") && CURRENT_SPEED < MAX_UP_SPEED) {
			CURRENT_SPEED += ACCELRATION;
		}
		
		if (Input.check("down") && CURRENT_SPEED > MAX_DOWN_SPEED) {
			CURRENT_SPEED -= ACCELRATION;
		}
		
		if (Input.check("right") && x + width < FP.screen.width) {
			x += 2;
		}
		
		if (Input.check("left") && x > 0) {
			x -= 2;
		}
		
		if (y + height < FP.screen.height && y > 0) {
			y -= CURRENT_SPEED;
		}
		
		
	}
	
}

}

Soo could someone tell me what I am doing wrong , and no CURRENT_SPEED is not a const.


(Mike Evmm) #2

You’re not calling super.update() on the update loop.


(Jean) #3

Thats not really necessary. Entity update has nothing inside it.

@granit_bajraktari Your problem is here:

Input.check("up") && CURRENT_SPEED < MAX_UP_SPEED

I believe this is wrong, what it’s is doing is (Input.check("up") && CURRENT_SPEED) < MAX_UP_SPEED), what is a mess and even I don’t know what the result will be. All you need is some parenthesis and you’re good to go.

Input.check("up") && (CURRENT_SPEED < MAX_UP_SPEED)


(Granit Bajraktari) #4

It cleaned the mess my noob programming skills did , but it didn’t really fix the problem of not moving.


(Kyle) #5

The acceleration is stored as an unsigned ‘interger’. So the 0.5 becomes 0. You should store it as a number instead.

private const ACCELRATION:uint = 0.5;      // equal to 0, thus no movement
private const ACCELRATION:Number = 0.5;    // equal to 0.5

Same thing applies to the current speed variable, use a number for it.


(Jacob Albano) #6

There’s nothing wrong with that expression. It’ll evaluate to bool && bool without the parentheses.


(Granit Bajraktari) #7

Ohh I see.

The accelration variable was initially going to be 1 , but I forgot to change the type when I changed it to 0.5.

Many thanks.