Pausing my game
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Pausing my game
I have a pause button but i'm having trouble pausing elements of my game. Here's how it's structured:
I have my main gameplay card called "Gameplay". There is a pause button on that card. Once the user clicks on pause, it's takes them to the card "PauseScreen" which has a Resume Game button and a Quit Game button. Right now, the Quit Game button allows the user to go back to the GamePlay card before it chooses the level it wants to play on. It's pretty much like resetting everything. The trouble i'm having is the Resume Game button. What I'm trying to do is save all the elements on the screen when the user pushes the pause button so when the Resume Game button is pushed, it's as if the game just keeps continuing. There's an animation that is going down the screen and a timer that I want to pause. What would be the best scripting method that would allow the animation and timer to pause and when Resume Game is pushed, they continue on?
Much appreciated from anybody that can give me some input
I have my main gameplay card called "Gameplay". There is a pause button on that card. Once the user clicks on pause, it's takes them to the card "PauseScreen" which has a Resume Game button and a Quit Game button. Right now, the Quit Game button allows the user to go back to the GamePlay card before it chooses the level it wants to play on. It's pretty much like resetting everything. The trouble i'm having is the Resume Game button. What I'm trying to do is save all the elements on the screen when the user pushes the pause button so when the Resume Game button is pushed, it's as if the game just keeps continuing. There's an animation that is going down the screen and a timer that I want to pause. What would be the best scripting method that would allow the animation and timer to pause and when Resume Game is pushed, they continue on?
Much appreciated from anybody that can give me some input
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Pausing my game
Hi,
Are you using a send <in time> in your game loop?
Or lets cut to the chase, how are you getting the objects to move?
Simon
Are you using a send <in time> in your game loop?
Or lets cut to the chase, how are you getting the objects to move?
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Pausing my game
I am not. Currently my code is just using move.
move field "FirstWord" to the points of graphic "Line1" in 5 seconds without waiting
It goes from the top of the screen to the bottom. I'm guessing using send would be better?
move field "FirstWord" to the points of graphic "Line1" in 5 seconds without waiting
It goes from the top of the screen to the bottom. I'm guessing using send would be better?
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Pausing my game
Yeah, I think so.I'm guessing using send would be better?
Code: Select all
on moveMe
set the bottom of me to the bottom of me +1
send moveMe to me in 1 ticks
end moveMe
Code: Select all
on mouseUp
put the pendingMessages into tPend
repeat with x = 1 to the number of lines in tPend
cancel item 1 of tPend
end repeat
end mouseUp
moveMe
Does that make sense?
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Pausing my game
Yes this does make sense and i've seen it work but I haven't quite perfected it yet. What I want to happen is when a button is pushed the gameplay starts. A timer begins and the moveMe handler is now associated with this and it's on the card script. This is how I modified it. I put the other code with the pendingMessages on the pause button but for some reason, every time I push the pause button, an error message comes up and says that object of "FirstWord" is recognized. No such object near "FirstWord" is what it says. Any reason why that might be coming up?
on moveMe
set the bottom of field "FirstWord" to the bottom of field "FirstWord" +1
send moveMe to field "FirstWord" in 1 ticks
end moveMe
You have been most helpful so far btw.
on moveMe
set the bottom of field "FirstWord" to the bottom of field "FirstWord" +1
send moveMe to field "FirstWord" in 1 ticks
end moveMe
You have been most helpful so far btw.
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Pausing my game
You didn't show me your cancel command
I'm guessing because you have "FirstWord" in there?
If you want to specify which items to cancel then grab the code from "pendingMessages" in the dictionary (in the notes) and put moveMe into tMsg.
You don't want to cancel the object you want to cancel the message, which is "moveMe"
How is that?
Simon
I'm guessing because you have "FirstWord" in there?
If you want to specify which items to cancel then grab the code from "pendingMessages" in the dictionary (in the notes) and put moveMe into tMsg.
You don't want to cancel the object you want to cancel the message, which is "moveMe"
How is that?
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
-
- VIP Livecode Opensource Backer
- Posts: 327
- Joined: Sun Apr 15, 2012 1:17 am
- Location: USA
- Contact:
Re: Pausing my game
You might find this explanation helpful in understanding the process.
https://sites.google.com/a/pgcps.org/li ... ng-targets
https://sites.google.com/a/pgcps.org/li ... ng-targets
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/
Re: Pausing my game
I don't really know how to structure this code. Bit of a noob. Do you put moveme into tMsg when I push the button or just put it on the card script? Probably an even dumber question but is item 1 supposed to one of my variables?
ON cancelThisMsg tMsg
put the pendingmessages into tPendingMsgs
IF tMsg is in tPendingMsgs THEN
repeat FOR each line x in tPendingMsgs
if tMsg is in x THEN cancel item 1 of x
end REPEAT
END IF
END cancelThisMsg
The main thing is getting this cancel code to work.
ON cancelThisMsg tMsg
put the pendingmessages into tPendingMsgs
IF tMsg is in tPendingMsgs THEN
repeat FOR each line x in tPendingMsgs
if tMsg is in x THEN cancel item 1 of x
end REPEAT
END IF
END cancelThisMsg
The main thing is getting this cancel code to work.
Re: Pausing my game
on cancelThisMessage tMsg begins a handler that listens for a "cancelThisMessage" message and collects the parameter tMsg passed with the message.
How you generate the message is one of a variety of ways. If you want to cancel the message when you click a button then in the button script you can:
on mouseUp
cancelThisMessage "moveme"
end mouseUp
If you want to cancel all outstanding moveme messages when you close the stack then you could put it in one of several places, such as in the stack script you could catch the closeStackRequest message and fire the cancelThisMessage message of from there:
on closeStackRequest
cancelThisMessage "moveme"
end closeStackRequest
you see, you can have handlers (mouseUp / closeStackRequest / many, many others) produce messages as part of their script. The message is generated by your statement issuing the command (in this case cancelThisMessage) for which you have created a handler to deal with that command.
Is that any clearer? If not, just say and we can try and clear the mud.
HTH
How you generate the message is one of a variety of ways. If you want to cancel the message when you click a button then in the button script you can:
on mouseUp
cancelThisMessage "moveme"
end mouseUp
If you want to cancel all outstanding moveme messages when you close the stack then you could put it in one of several places, such as in the stack script you could catch the closeStackRequest message and fire the cancelThisMessage message of from there:
on closeStackRequest
cancelThisMessage "moveme"
end closeStackRequest
you see, you can have handlers (mouseUp / closeStackRequest / many, many others) produce messages as part of their script. The message is generated by your statement issuing the command (in this case cancelThisMessage) for which you have created a handler to deal with that command.
Is that any clearer? If not, just say and we can try and clear the mud.
HTH
Re: Pausing my game
Yes that made a lot of sense. It seems to almost be there but I still get the error message about my field "FirstWord". It doesn't happen all the time, only sometimes. That's my moveObj command so that my field goes down. This is where the message keeps getting the error. It's on my mainstack. Below it is my cancel command. It's when I push my pause button that creates the error. On my pause button script, I have cancelThisMsg "moveObj".
command moveObj
if gamerunning is true then
move the field "FirstWord" relative 0,+10
if the top of field "FirstWord" > the bottom of the card "GameSelect" then
set the bottom of the field "FirstWord" to the top of the card "GameSelect"
end if
send moveObj to me in 2 ticks
put moveObj into tMsg
end if
end moveObj
ON cancelThisMsg tMsg
put the pendingmessages into tPendingMsgs
IF tMsg is in tPendingMsgs THEN
repeat FOR each line x in tPendingMsgs
if tMsg is in x THEN cancel item 1 of x
end REPEAT
END IF
END cancelThisMsg
command moveObj
if gamerunning is true then
move the field "FirstWord" relative 0,+10
if the top of field "FirstWord" > the bottom of the card "GameSelect" then
set the bottom of the field "FirstWord" to the top of the card "GameSelect"
end if
send moveObj to me in 2 ticks
put moveObj into tMsg
end if
end moveObj
ON cancelThisMsg tMsg
put the pendingmessages into tPendingMsgs
IF tMsg is in tPendingMsgs THEN
repeat FOR each line x in tPendingMsgs
if tMsg is in x THEN cancel item 1 of x
end REPEAT
END IF
END cancelThisMsg
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: Pausing my game
You are still using the move command???
not the same thing.
Edit:
added "to the" in code above
BTW This is not how you will probably end up designing your game. I'm only giving you some code to start working with, it's just for learning.
Simon
not the same thing.
Code: Select all
on moveMe
set the bottom of me to the bottom of me +1
send moveMe to me in 1 ticks
end moveMe
Code: Select all
on moveMe
set the bottom of field "FirstWord" to the bottom of field "FirstWord"+1
if the top of field "FirstWord" > the bottom of the card "GameSelect" then
set the bottom of the field "FirstWord" to the top of the card "GameSelect"
end if
send moveMe to me in 1 ticks
end moveMe
added "to the" in code above
BTW This is not how you will probably end up designing your game. I'm only giving you some code to start working with, it's just for learning.
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
-
- VIP Livecode Opensource Backer
- Posts: 7214
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Pausing my game
The error you're getting is probably because there is a stray pending message that is triggering after you've already moved to the gamePaused card. On that card there is no field but the pending message has been sent already. To clean this up, put the body of the handler that removes all pending messages into a closeCard handler in the script of the gameSelect card. Then it will stop all pending messages whenever you change cards.
If you look at pendingMessages in the dictionary you can see that each pending message is comprised of several items on a line. The first item is an ID number that identifies each message. The ID is required to cancel a message, so that's why the handler cancels "item 1" of each pending message.
That said, there may be a better way to handle this than using the "send" method. As you have it now, I don't see any harm in using "move" as opposed to setting the location of the object when using a "send" structure, but if we switch entirely to the "move" command then you can use built-in messages to handle everything without needing to worry about pending messages at all. If you issue a "move" command with the additional parameter "without waiting", the card will receive a "moveStopped" message after the move completes. You can trap that message, check the location of the field, and either issue another "move" command or bail out. There are no pending messages to worry about using this method. I've changed the amount of the vertical move to only a single pixel to prevent jerkiness:
Assuming your pause button sets gameRunning to false, the moveObj won't do anything after the pause button is clicked so calling it additional times doesn't matter. The moveStopped handler checks the location of the field and only calls moveObj again if the field meets the location criteria.
This seems a cleaner way to manage things than using the somewhat messy "send" method. We need "send" frequently for many things, but in this case there's a built-in way to do it.
If you look at pendingMessages in the dictionary you can see that each pending message is comprised of several items on a line. The first item is an ID number that identifies each message. The ID is required to cancel a message, so that's why the handler cancels "item 1" of each pending message.
That said, there may be a better way to handle this than using the "send" method. As you have it now, I don't see any harm in using "move" as opposed to setting the location of the object when using a "send" structure, but if we switch entirely to the "move" command then you can use built-in messages to handle everything without needing to worry about pending messages at all. If you issue a "move" command with the additional parameter "without waiting", the card will receive a "moveStopped" message after the move completes. You can trap that message, check the location of the field, and either issue another "move" command or bail out. There are no pending messages to worry about using this method. I've changed the amount of the vertical move to only a single pixel to prevent jerkiness:
Code: Select all
on moveObj
if gamerunning is true then
move field "FirstWord" relative 0,1 without waiting
end if
end moveObj
on moveStopped
if the top of field "FirstWord" > the bottom of the card "GameSelect" then
set the bottom of the field "FirstWord" to the top of the card "GameSelect"
else
moveObj
end if
end moveStopped
This seems a cleaner way to manage things than using the somewhat messy "send" method. We need "send" frequently for many things, but in this case there's a built-in way to do it.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com