PDA

View Full Version : RPG game navigation help


psychommunity0
05-06-2002, 01:36 AM
Hello, I'm making a rpg game in flash. THe code below is for the navigation. I'm trying to create the game so that it uses the grid-like motion in the classical rpg games. The grid-like motion prevents the characters from walking diagonally. So the character must walk forward 2 blocks, left 3 blocks, forward 1 block, etc. The code below compares the coordinate where the character is currently standing, to the coordinate that your mouse clicks (i'm not using the keyboard for navigation).

MY QUESTION: How would I change the code so that the character will first move on the x axis, THEN, the y-axis? For example, If I were to click a location diagonal to the character, I want to see him move to left/right first, THEN, up/down.

The code below is put on the character m/c that i'm moving.

onClipEvent(load){
nPosOldX = this._x
nPosOldY = this._y
}
onClipEvent(mouseDown){
nPosOldX = nPosNewX;
nPosOldY = nPosNewY;
nPosNewX = _root._xmouse;
nPosNewY = _root._ymouse;
if (nPosOldX != null && nPosOldY != null){
if (nPosOldX != nPosNewX || nPosOldY != nPosNewY){
this._x = nPosNewX
this._y = nPosNewY
}
}
}

Ricod
05-06-2002, 10:35 AM
Arghhh ! My reply didn't come through !

ehmr ... the basic thing was :
U'll need to use the current x and y, and compare them to the destination x and y. Now, using a little trigonometry u can find out wether or not the direction is diagonal (or you could just check if there's only movement in either the x or y, but when there's movement in both, its diagonal.)

If its indeed diagonal, you can start a loop which will move your character horizontal by the difference between the destination x and the current x, and when its finished (if currentX == destinationX) it starts the vertical loop. If u don't know how to make loops, take a look at the tutorial.

Cuz of my huge interest in gaming I'm kinda curious what yer making here, can I have a look ? :)

psychommunity0
05-06-2002, 03:14 PM
Yes. What I've been doing is getting both the current and new X distance AND the current and new Y distance. This way I won't have to use any trig stuff. The code below does compare the old and the new coordinates. However, I can't seem to get the character to move on the x-axis first, THEN move on the y-axis. Also, i've tried:
if(this._x = nPosNewX){
this._y = nPosNewY
}
I'm sure this means the character's horizontal position MUST equal that of the new horizontal coordinate in order for the vertical movement to engage. However, it doesn't work. So maybe I didn't explain correctly. How would I change the code below so that it gets the character to move horizontally first, THEN vertically?

Hey Ricod, i'll be happy to show you the end product(if i can finish it :)). I'm trying to create a little rpg(final fantasy) game and implement it into a site.

thanks!!!!

Ricod
05-06-2002, 03:21 PM
I usually use a three frame mc that loops until the destined position is reached , with a piece that tells it, when the maximum value is crossed to set the position to the max value, in this case the destination _x. (for a smooth transition, but your moving it instantly ?)

anyway, to compare data, use :
if(this._x == nPosNewX){
this._y = nPosNewY
}

== compares a value, while = sets a value.

How do you think to make it small, if its final fantasy like :) !

psychommunity0
05-06-2002, 11:13 PM
woops~
i mean the put == instead of a single =
Even with ==, the code still doesn't work. Any ideas? One more thing. I didn't quite get the part about having 3 frames. Can you explain it again? hehe
thanks!!!!
P.S. I'm starting off small with the game. Maybe just one town that allows the character to walk around and talk to people. :)

Ricod
05-07-2002, 11:27 AM
The 3 frames mc is what I use for loops ... but your doing instant move or sumthing ? Anyway, I use them to let something increment (like with movement). Basically, the 1st frame has a stop(). The 2nd has actions that check the current position, opposed to the destination position, and then moves according to the speed that's appropriate. The 3rd also has this, but also a gotoAndPlay(2);

let's see what we can do with the current setup ...
onClipEvent(mouseDown){
nPosOldX = nPosNewX;
nPosOldY = nPosNewY;
nPosNewX = _root._xmouse;
nPosNewY = _root._ymouse;
if (nPosOldX != null && nPosOldY != null){
if (nPosOldX != nPosNewX || nPosOldY != nPosNewY){
_root.movementControl.gotoAndPlay(2);
}
}
}

Now we let movementControl ... well, control the movement :).


characterSpeed = 10;
var currentX = _root.myCharacter._x;
var destinationX = _root.myCharacter.nPosNewX;
if (currentX != destinationX){
XDiff = destinationX - currentX;
if (XDiff >= characterSpeed) {
_root.myCharacter._x += XDiff;
}else{
_root.myCharacter._x = destinationX;
}
}else{
if (currentY != destinationY) {

etc ... repeat everything for X with Y ... now, with this construction, if everything is set appr. it should always check wether or not there's a difference in _X ,and if not, then check wether or not there's a difference in _y.

psychommunity0
05-08-2002, 05:01 AM
hey ricod
I really appreciated helping me with this. I'm not totally sure where to put the code you just wrote down there :p. What I did was:
A) in an empty mc called movementControl:
frame 1:

stop()

frame 2:

characterSpeed = 10;
var currentX = _root.myCharacter._x;
var currentY = _root.myCharacter._y;
var destinationX = _root.myCharacter.nPosNewX;
var destinationY = _root.myCharacter.nPosNewY;
if (currentX != destinationX){
XDiff = destinationX - currentX;
if (XDiff >= characterSpeed) {
_root.myCharacter._x += XDiff;
}else{
_root.myCharacter._x = destinationX;
}
}
if (currentY != destinationY){
YDiff = destinationY - currentY;
if (YDiff >= characterSpeed) {
_root.myCharacter._y += YDiff;
}else{
_root.myCharacter._y = destinationY;
}
}

frame 3:

characterSpeed = 10;
var currentX = _root.myCharacter._x;
var currentY = _root.myCharacter._y;
var destinationX = _root.myCharacter.nPosNewX;
var destinationY = _root.myCharacter.nPosNewY;
if (currentX != destinationX){
XDiff = destinationX - currentX;
if (XDiff >= characterSpeed) {
_root.myCharacter._x += XDiff;
}else{
_root.myCharacter._x = destinationX;
}
}
if (currentY != destinationY){
YDiff = destinationY - currentY;
if (YDiff >= characterSpeed) {
_root.myCharacter._y += YDiff;
}else{
_root.myCharacter._y = destinationY;
}
}
gotoAndPlay(2)

I'm not exactly sure where to put the other code. Sorry to bother you with this simple question~:o
thank you soooo much

psychommunity0
05-11-2002, 01:12 AM
ricod?

Ricod
05-12-2002, 02:37 PM
Sorry, I'm not around as often as I used to be ... (u can all thank my 2 bossess)

The other code would be where u put it right ? I believe u put it on your character.

But, eh , do u understand what the code is saying ? (it's more important ...)