Forum Topic: Save Players x And y Coordinates

(135 views • 15 replies)

This topic is 1 page long.

<< < > >>
None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/16/09 09:10 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

This is an AS3 question.
Alright, so in this game I'm currently developing (which is basically 4 guys and a wall right now) you press P to go into the pause menu. Easy enough. And to leave the pause menu you press the Resume button. Obviously. My problem is with when I go into the pause menu, after moving a bit away from my start position, and resume, it resets my characters x and y coordinates. I need some way to save his x and y coordinates when I go into the pause menu and load them when I leave. I don't know what code to put down so I'll just put down the Resume code and the Pause code.

Pause:

function checkKeys(event:KeyboardEvent):void {
        if (event.keyCode == 80) {
		gotoAndStop("PauseMenu");
	}
}

Resume:

function onIntroClick(evt:MouseEvent):void {
	gotoAndStop(1);
}
BBS Signature

None

52caliber

Reply To Post Reply & Quote

Posted at: 7/16/09 09:59 PM

52caliber LIGHT LEVEL 12

Sign-Up: 07/23/08

Posts: 20

Sorry I can't post any code right now to solve your problem but when you run the pause function also try removing any other event listeners or disabling any buttons that may change the player's x and y coordinates (maybe that will stop the player's positions from changing while paused.)

Another idea is when the game is paused, add the player's x and y coordinates to variables, and when the game is un-paused make the player's x and y positions equal to that of the variables.


None

Schmastalukas

Reply To Post Reply & Quote

Posted at: 7/16/09 10:23 PM

Schmastalukas NEUTRAL LEVEL 17

Sign-Up: 10/22/07

Posts: 394

Hmm... while saving the coordinates to variables, should work, I must question your technique of pausing the game. Would it not be easier to just make a pause menu movieclip, and, while paused, have it be visible, and while not paused, make it not be visible? Seems much more simple, for lack of a better word, to do it this way.

Winning = failing at failing
Failing at failing = failing
Therefore... Winning = failing

BBS Signature

None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/16/09 10:28 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

At 7/16/09 10:23 PM, Schmastalukas wrote: Hmm... while saving the coordinates to variables, should work, I must question your technique of pausing the game. Would it not be easier to just make a pause menu movieclip, and, while paused, have it be visible, and while not paused, make it not be visible? Seems much more simple, for lack of a better word, to do it this way.

Oh yeah, that's a great idea. That way I'll never have to save x, y coordinates, they'll be there all along. Thanks a lot! Time to test this out.

BBS Signature

None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 7/16/09 10:46 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

I agree with Schmastalukas. You could alternatively attach the menu movieclip (rather than change whether it is visible).

For the original question, though, all you'd have to do is save the values in a variable:

mySavedX = character._x;
mySavedY = character._y;

And on resume, place the character back.

character._x = mySavedX;
character._y = mySavedY;

You might not need that now, but you will probably need the concept for other things later on in game development.

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 7/16/09 10:47 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

^ BTW that's AS2. I'm not sure if AS3 variable setting and such has different syntax rules or not.

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/16/09 10:48 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

At 7/16/09 10:46 PM, ShirkDeio wrote: I agree with Schmastalukas. You could alternatively attach the menu movieclip (rather than change whether it is visible).

For the original question, though, all you'd have to do is save the values in a variable:

mySavedX = character._x;
mySavedY = character._y;

And on resume, place the character back.

character._x = mySavedX;
character._y = mySavedY;

You might not need that now, but you will probably need the concept for other things later on in game development.

I actually did try this and we figured it should have worked but it didn't for some odd reason. Next time i need this I'll try it with the underscore, because we didn't have that before.

BBS Signature

None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/16/09 10:51 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

Alright sorry for the double post but I have a question about the visible thing. I put this code in assuming it would work, but it slowed down the script a lot and actually timed out.

menu.visible = false;
function checkKeys(event:KeyboardEvent):void {
        if (event.keyCode == 80) {
		menu.visible = true;
	}
	if (event.keyCode == 80) {
		while (menu.visible = true);
		menu.visible = false;
	}
}
BBS Signature

None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 7/16/09 10:54 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

Again, I use AS2 so I may be ignorant about some of the changes... but the while function is a loop, and you only want to call the code one time (while looping is used to execute a block of code several times). Change that to "if" and see what you get.

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


None

ShirkDeio

Reply To Post Reply & Quote

Posted at: 7/16/09 10:56 PM

ShirkDeio LIGHT LEVEL 16

Sign-Up: 03/29/07

Posts: 989

Plus, you are setting it to true then telling it to change it to false. Here's what it should be according to my AS2 knowledge:

if (event.keyCode == 80 && !menu.visible) {
     menu.visible = true;
} else {
     menu.visible = false;
}

I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)


None

Schmastalukas

Reply To Post Reply & Quote

Posted at: 7/16/09 10:59 PM

Schmastalukas NEUTRAL LEVEL 17

Sign-Up: 10/22/07

Posts: 394

At 7/16/09 10:51 PM, Alpharius120 wrote: Alright sorry for the double post but I have a question about the visible thing. I put this code in assuming it would work, but it slowed down the script a lot and actually timed out.

menu.visible = false;
function checkKeys(event:KeyboardEvent):void {
if (event.keyCode == 80) {
menu.visible = true;
}
if (event.keyCode == 80) {
while (menu.visible = true);
menu.visible = false;
}
}

You could just say menu.visible = !menu.visible, and only have one if statement. Or, if that doesn't work, and i'm not sure if it will in AS3, you could do something like...

if (event.keyCode == 80) {
     if (menu.visible) {
          menu.visible = false;
     } else {
          menu.visible = true;
     }
}

Winning = failing at failing
Failing at failing = failing
Therefore... Winning = failing

BBS Signature

None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/16/09 11:00 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

At 7/16/09 10:56 PM, ShirkDeio wrote: Plus, you are setting it to true then telling it to change it to false. Here's what it should be according to my AS2 knowledge:

if (event.keyCode == 80 && !menu.visible) {
menu.visible = true;
} else {
menu.visible = false;
}

Well, I'm using AS3, but this is what it should look like I believe I'm going to test it out.
And it works, thanks a whole lot!
That should be the extent of my problems with this, thanks for your help guys.

BBS Signature

None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/16/09 11:27 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

Alright, one more problem actually. When the menu becomes visible I can't press the buttons to perform the actions such as going to the Items menu, the Stats menu, and the Save menu, but it will resume.
Here's my code.

function onIntroClick(evt:MouseEvent):void {
	menu.visible = false;
}

function onOutroClick(evt:MouseEvent):void {
	ItemMenu.visible = true;
}

function onClick(evt:MouseEvent):void {
	StatMenu.visible = true;
}

function onClack(evt:MouseEvent):void {
	SaveMenu.visible = true;
}

menu.addEventListener(MouseEvent.CLICK, onIntroClick);
ItemMenu.addEventListener(MouseEvent.CLICK, onOutroClick);
StatMenu.addEventListener(MouseEvent.CLICK, onClick);
SaveMenu.addEventListener(MouseEvent.CLICK, onClack);
BBS Signature

None

52caliber

Reply To Post Reply & Quote

Posted at: 7/17/09 11:45 AM

52caliber LIGHT LEVEL 12

Sign-Up: 07/23/08

Posts: 20

Instead of making your pause menu movie clip become visible and invisible, just use addChild(); and removeChild(); instead - this way the menu won't be on the stage while the game is un-paused, and therefore won't be slowing down the game as much.

The buttons may not be working because they might be underneath the pause menu movie clip. You should just put menu items inside the menu movie clip.


None

Alpharius120

Reply To Post Reply & Quote

Posted at: 7/17/09 01:18 PM

Alpharius120 FAB LEVEL 14

Sign-Up: 07/08/07

Posts: 2,985

At 7/17/09 11:45 AM, 52caliber wrote: Instead of making your pause menu movie clip become visible and invisible, just use addChild(); and removeChild(); instead - this way the menu won't be on the stage while the game is un-paused, and therefore won't be slowing down the game as much.

The buttons may not be working because they might be underneath the pause menu movie clip. You should just put menu items inside the menu movie clip.

I didn't even think of using addChild();. Now it works perfectly, thanks for all your help everyone.

BBS Signature

None

52caliber

Reply To Post Reply & Quote

Posted at: 7/17/09 03:43 PM

52caliber LIGHT LEVEL 12

Sign-Up: 07/23/08

Posts: 20

At 7/16/09 10:48 PM, Alpharius120 wrote:
At 7/16/09 10:46 PM, ShirkDeio wrote: I agree with Schmastalukas. You could alternatively attach the menu movieclip (rather than change whether it is visible).

For the original question, though, all you'd have to do is save the values in a variable:

mySavedX = character._x;
mySavedY = character._y;

And on resume, place the character back.

character._x = mySavedX;
character._y = mySavedY;

You might not need that now, but you will probably need the concept for other things later on in game development.
I actually did try this and we figured it should have worked but it didn't for some odd reason. Next time i need this I'll try it with the underscore, because we didn't have that before.

Just one more thing I'd like to add. It probably didn't work because you have to put the code with the variables on a different layer, so it doesn't refresh when you un-pause the game and go back to the original frame.


All times are Eastern Standard Time (GMT -5) | Current Time: 02:04 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!