Home  |  About  | Last |  Submit  |  Contact
AllQuests.com




Previous Question:  How to do this  Flash ActionScriptNext Question:  duplicating mcs based on testfeild height  Flash ActionScript
Question getting sprite to move continuously after single keypress ( Flash Kit Community Forums Flash ActionScript )
Updated: 2010-07-21 05:55:08 (4)
getting sprite to move continuously after single keypress

I was following a tutorial on moving clips with the keypad - I got the character moving around fine, but just using 'if (Key.isDown(Key.RIGHT)' type statements, which means the sprite just moves while the key is down. So I tried to come up with a way to make the sprite move continuously using this code attached to an mc:

Code:
onClipEvent (load) {
	step = 5;
	movieWith = 300;
	movieHeight = 300;
	this.stop();
	_root.dir = "";
}
onClipEvent (keyDown) {
	if (Key.getCode() == Key.UP) {
		_root.dir = "up";
	} else if (Key.getCode() == Key.RIGHT) {
		_root.dir = "right";
	} else if (Key.getCode() == Key.DOWN) {
		_root.dir = "down";
	} else if (Key.getCode() == Key.LEFT) {
		_root.dir = "left";
	}
}
onClipEvent (enterFrame) {
	if (_root.dir="right" and this._x<300) {
		this.attachMovie("sprite", "bug", 10);
		this._x = this._x+step;
		this._rotation = 45;
		this._xscale = -100;
	} else if (_root.dir="left" and this._x>0) {
		this.attachMovie("sprite", "bug", 10);
		this._x = this._x-step;
		this._rotation = -45;
		this._xscale = 100;
	} else if (_root.dir="up" and this._y>0) {
		this.attachMovie("sprite", "bug", 10);
		this._y = this._y-step;
		this._rotation = 0;
		this._xscale = 100;
	} else if (_root.dir="down" and this._y<300) {
		this.attachMovie("sprite", "bug", 10);
		this._y = this._y+step;
		this._rotation = 180;
		this._xscale = 100;
	} else {
		this.attachMovie("sprite", "bug", 10);
		_root.dir = "";
	}
}
How can I fix it? Am I on the right track? Is there a different appraoch?

Answers: getting sprite to move continuously after single keypress ( Flash Kit Community Forums Flash ActionScript )
getting sprite to move continuously after single keypress

Code:
onClipEvent (load) {
	step = 5;
	movieWith = 300;
	movieHeight = 300;
	this.stop();
	_root.dir = "";
                this.attachMovie("sprite", "bug", 10);
}
onClipEvent (keyDown) {
	if (Key.isDown(Key.UP)) {
		_root.dir = "up";
	} else if (Key.isDown(Key.RIGHT)) {
		_root.dir = "right";
	} else if (Key.isDown(Key.DOWN)) {
		_root.dir = "down";
	} else if (Key.isDown(Key.LEFT)) {
		_root.dir = "left";
	}
}
onClipEvent (enterFrame) {
	if (_root.dir eq "right" && this._x < 300) {
		this._x += step;
		this._rotation = 45;
		this._xscale = -100;  //  what s this for??
	} else if (_root.dir eq "left" && this._x > 0) {
		this._x -= step;
		this._rotation = -45;
		this._xscale = 100;
	} else if (_root.dir eq "up" && this._y > 0) {
		this._y -= step;
		this._rotation = 0;
		this._xscale = 100;
	} else if (_root.dir eq "down" && this._y < 300) {
		this._y += step;
		this._rotation = 180;
		this._xscale = 100;
	} else {
		_root.dir = "";
	}
}
That should work a little better. Actually, it should work a lot better, since your original code didn't work!

garbage

getting sprite to move continuously after single keypress

wicked! cheers bro - that's better (the -xscale thing is 2 flip sprite horizontal)

onDelivery

getting sprite to move continuously after single keypress

Oh right ... neat trick! Glad the code worked out for you ... make sure you click the 'Mark Thread Resolved' link at the bottom of this page

garbage

getting sprite to move continuously after single keypress

ok, will do - could u check out my pacman problem in the games section if u've got time? I'm getting there slowly by myself - sussed out how to make mazes with arrays/strings - can understand how the level up thing works... but ... pleeeez have a look, it's another movement issue. It's a bit of a big ask, but u might enjoy trying to read the spanish comments ... cough

onDelivery

Previous Question:  How to do this  Flash Kit Community Forums  Flash ActionScriptNext Question:  duplicating mcs based on testfeild height  Flash Kit Community Forums  Flash ActionScript

- Source: getting sprite to move continuously after single keypress Flash Kit Community Forums Flash ActionScript
- Previous Question: How to do this Flash Kit Community Forums Flash ActionScript
- Next Question: duplicating mcs based on testfeild height Flash Kit Community Forums Flash ActionScript