Updating variables


(Zpiggle) #1

Sorry for what is probably a very silly question, I’ve just started using Flashpunk/AS3/coding in general. Currently I have written some code that moves my player about (not code below). I want the movement to be like Snake though, which I can’t seem to do.

So far, I have:

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

public class SnakePlayer extends Entity
{[Embedded images...]}

	
	
	public function SnakePlayer()
	{
		graphic = new Image(PLAYERUP);
		x = 300;
		y = 400;
	}
			var speedx:int;
			var speedy:int;
			

	override public function update():void
	{
		speedx = 0;
		speedy = -3;
		x += speedx
		y += speedy
		if (Input.check(Key.LEFT)) { speedx = 3; speedy = 0; graphic = new Image(PLAYERLEFT); } ...
		}
}

}

It starts off moving straight upwards.

Essentially, when I press LEFT I want speedx to be assigned a new value, i.e. speedx=3; likewise I want to change speedy to 0. However, it doesn’t do anything other than change the image (from the graphic=new… as it’s supposed to). I think I need to loop it somehow, or use an update function somewhere?

I tried using while loops, but I haven’t got the hang of them yet and they stop the flash player working.

Please explain as simply as possible if answering.


(TaylorAnderson) #2

Your code is kinda jumbled a bit at the end, but it’s fairly easy to see the problem–right before you add speedx to x, you set it to 0. you might want to put the x+=speedx and y+=speedy AFTER assigning your speed variables according to input.

Also, you can save on a variable by making speed into a Point and saying speed.x and speed.y (but thats just my own preference)


(Zpiggle) #3

(Note that I’d removed the update bit in the if function in my first post, as it froze the program) Thanks very much for the quick reply. I’ve changed it to:

	override public function update():void
	{
		speedx = 0;
		speedy = -3;
		
		if (Input.check(Key.LEFT)) { speedx = -3; speedy = 0; graphic = new Image(PLAYERLEFT); }
		if (Input.check(Key.RIGHT)) { speedx = 3 ; speedy = 0; graphic = new Image(PLAYERRIGHT); }
		if (Input.check(Key.UP)) { speedx = 0; speedy = -3; graphic = new Image(PLAYERUP); }
		if (Input.check(Key.DOWN)) { speedx = 0; speedy = 3; graphic = new Image(PLAYERDOWN); }
		
		x += speedx
		y += speedy

The movement is working perfectly as long as I am holding down the keys, but once I let go the image just keeps going upward no matter what key I was pressing. I would like it to continue in the direction the arrow is pointing.

And sorry, but what do you mean by making a variable into a Point?


(Zpiggle) #4

Yay! I removed speedx = 0; speedy = -3; at start of override and now it’s working fine. Thanks for your help.

Edit: Is there a way I could have it initially travelling upwards though?


(TaylorAnderson) #5

I mean like, you declare speedx and speedy as numbers right? Instead of saying var speedx:Number = 0 and var speedy:Number = 0, you’d say var speed:Point = new Point(0, 0)


(Zpiggle) #6

Ah right, of course, thanks.