PDA

View Full Version : walls


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.

majicassassin
12-05-2007, 06:20 AM
The problem is that if you have multiple buttons down your character basically moves, saves the position, moves again, and saves the position before any input from the wall. This means that x2 and y2 are set to bad positions because of the second save.

Walls are usually better (in my experience) if you make them absolutely repositioning. So instead your wall code should look like this for the right wall:
onClipEvent (enterFrame) {
if (this.hitTest(_root.qw)) {
_root.wall = false;
_root.qw._x = this._x -= _root.qw._width;

_root.wall = true;
}
}

This will set the character one length back to the left of the wall. It kind of sucks that you have to do this for each type of wall, but with some crafty OOP'ing you can make it so you only do each one once and then you can reuse the code on any object.

Good luck!
-maji