gabrielHerrera
11-29-2007, 01:18 PM
I am making a non tile based rpg game, but I am having a problem with making the character stop when he hits a wall. I've searched in google but I couldn't find a good script so I made my own. It basically saves the x and the y variables of the character and if he hits a wall, it loads the saved x and y making he go back.
Works fine so far but when you have two arrows pressed and hit a wall, you can walk through it.
Here's the AS for the character (his instance name is qw):
onClipEvent (enterFrame) {
if (_root.wall) {
if (_x<386) {
if (Key.isDown(Key.RIGHT)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._x += 5;
_root.qw.gotoAndStop(2);
}
}
if (_x>14) {
if (Key.isDown(Key.LEFT)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._x -= 5;
_root.qw.gotoAndStop(3);
}
}
if (_y>27.0) {
if (Key.isDown(Key.UP)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._y -= 5;
_root.qw.gotoAndStop(4);
}
}
if (_y<293.0) {
if (Key.isDown(Key.DOWN)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._y += 5;
_root.qw.gotoAndStop(1);
}
}
}
}
And here's the AS for the walls (no instance name):
onClipEvent (enterFrame) {
if (this.hitTest(_root.qw)) {
_root.wall = false;
_root.qw._x = _root.x2;
_root.qw._y = _root.y2;
_root.wall = true;
}
}
I know I can possibily fix it not letting hit two keys at once but I would like to keep that.
I would like to know if there's another way to fix this problem.
Thank you all in advance.
Works fine so far but when you have two arrows pressed and hit a wall, you can walk through it.
Here's the AS for the character (his instance name is qw):
onClipEvent (enterFrame) {
if (_root.wall) {
if (_x<386) {
if (Key.isDown(Key.RIGHT)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._x += 5;
_root.qw.gotoAndStop(2);
}
}
if (_x>14) {
if (Key.isDown(Key.LEFT)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._x -= 5;
_root.qw.gotoAndStop(3);
}
}
if (_y>27.0) {
if (Key.isDown(Key.UP)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._y -= 5;
_root.qw.gotoAndStop(4);
}
}
if (_y<293.0) {
if (Key.isDown(Key.DOWN)) {
_root.x2 = _x;
_root.y2 = _y;
_root.qw._y += 5;
_root.qw.gotoAndStop(1);
}
}
}
}
And here's the AS for the walls (no instance name):
onClipEvent (enterFrame) {
if (this.hitTest(_root.qw)) {
_root.wall = false;
_root.qw._x = _root.x2;
_root.qw._y = _root.y2;
_root.wall = true;
}
}
I know I can possibily fix it not letting hit two keys at once but I would like to keep that.
I would like to know if there's another way to fix this problem.
Thank you all in advance.