Hi, I’m creating one of those point and click adventure games where you point to where you want the character to go, and he walks to the position.
The code I have so far is very simple, in an enterFrame is works out how far your character is away from the target X and Y point, and then moves it a certain number of pixels closer every frame.
I have split it up so that it moves horizontally first, then when it has finished, move vertically because I don’t want to have to create sprites for diagonal movement.
My code is below. My problem is that I want the character to walk around objects in the middle of the stage, but my current method the character would just walk straight through them. Would anyone be able to help me with any reasonably simple pathfinding algorithms or a method of walking around objects?
My current code:
//--setup where we're walking to
var targetX = _xmouse;
var targetY = _ymouse;
var distanceX = targetX-character._x;
var distanceY = targetY-character._y;
var doneWalkingX = false;
var doneWalkingY = false;
var walkSpeed = 5
//--do the walk!
character.onEnterFrame = function() {
//---walk horizontally first
if (doneWalkingX == false) {
//--do the walk
//--are you in front or behind?
if (targetX>this._x) {
distanceX = targetX-this._x;
if (distanceX>walkSpeed) {
this._x += walkSpeed;
} else {
doneWalkingX = true;
}
} else if (targetX<this._x) {
distanceX = this._x-targetX;
if (distanceX>walkSpeed) {
this._x -= walkSpeed;
} else {
doneWalkingX = true;
}
}
}
if (doneWalkingX == true and doneWalkingY == false) {
//--walk vertically now
//--are you in front or behind?
if (targetY>this._y) {
distanceY = targetY-this._y;
if (distanceY>walkSpeed) {
this._y += walkSpeed;
} else {
doneWalkingY = true;
}
} else if (targetY<this._y) {
distanceY = this._y-targetY;
if (distanceY>walkSpeed) {
this._y -= walkSpeed;
} else {
doneWalkingY = true;
}
}
}
if (doneWalkingX == true and doneWalkingY == true) {
//--finished walking
}
};
thanks to anyone who replies!