How to start a timer when a certain scene begins?
britto24
Member Posts: 35
Hello,
I am creating a racing game and trying to create a timer that starts when the race begins and the cars move and ends when they cross(collide) with the finish line. I have watched all the timer tutorials, but I'm still super new to GameSalad and can't figure this out. Right now I just have the game.time attribute being displayed. I know I need to create more custom attributes, but I need some help. Any suggestions? I would really appreciate it!
Thanks so much!
I am creating a racing game and trying to create a timer that starts when the race begins and the cars move and ends when they cross(collide) with the finish line. I have watched all the timer tutorials, but I'm still super new to GameSalad and can't figure this out. Right now I just have the game.time attribute being displayed. I know I need to create more custom attributes, but I need some help. Any suggestions? I would really appreciate it!
Thanks so much!
Comments
game.running (boolean), inititally set to FALSE
game.raceTime (real), initially set to 0
When you want the timer to start (when the player presses the accelerator for example) have a rule that says:
change attribute game.running to TRUE
Have another rule somewhere that says:
When game.running = TRUE, every 0.1 seconds change game.raceTime to game.raceTime+0.1 (This starts a timer, but instead of using game.time, which starts as soon as the game starts, this will use your custom attribute to store the time).
On the car actor, have a rule that says:
When collides of overlaps with (finish line actor) change attribute game.running to FALSE
(This will stop the game.raceTime from incrementing any higher, so it will keep showing the final time until you tell it otherwise).
Hope that's good, I'm away from Gamesalad so did that with the power of thought alone :-) it might need a bit of tinkering but that's the logic of how I'd do it.
PS welcome to gamesalad!
I would say that as you are new to gamesalad, make sure that you try to learn the basics at a sensible pace, rather than trying to build an impressive game from day 1. The better grounding you have on the ins and outs of the software, the better equipped you'll be to tackle these sorts of problems.
However with the problem at hand, perhaps somebody else who has access to GSC might be able to chip in?... If not I can have a play around when I get home, but that won't be for hours yet.
just store the currentValues for those attributes in newAttributes
Example:
gameAttribute: real type … name raceTime
on your carActor:
selfAttribute: real type … name startTime
selfAttribute: real type … name endTime
Rule: when
Event: touch is pressed … or Attribute: whatever value you use to start race
-changeAttribute: self.startTime To: self.Time
Rule: when
Event: overlaps or collides with finishLine
-changeAttribute: self.endTime To: self.Time
-changeAttribute: game.raceTime To: self.endTime-self.startTime
MH
forgot that if you are running more than one race in game
… before you changeAttribute self.startTime behavior
… add changeAttribute: game.raceTime To: 0
also use that startTime attribute if you want to display the runningTime
on displayText actor … unlocked …
Rule: when
Attribute: scene.Background,car.startTime > 0
-displayText … Text: scene.Background,car.Time-scene.Background,car.startTime
of course if you only want to display 2 decimal points in that one:
-Functions: precision
… puts: prec(x,y) in the expressionEditor
… the x holds the value you select , for y you type in the number of decimals (2)
IMO … you have a strong grasp of what you want and what will work best
… and ask the right questions! … which means you are making great progress with GS!
@};- MH
I have a few problems though...
1. My display text shows the race time great, but when the car crosses the finish line the time keeps going. How do I stop the time and just display the final time when the car crosses the finish line?
Right now on the car actor I have:
Rule: when, Actor: overlaps or collides with finishLine
-changeAttribute: self.endTime To: self.Time
-changeAttribute: game.raceTime To: self.endTime-self.startTime
2. When the car actor crosses the finish line, it's supposed to spawn the youWin actor. However, right now when the car crosses the finish line nothing happens. Here's the rule for the car:
Rule: when, Actor: overlaps or collides with finishLine
-spawn actor youWin
But if I wrap x for the scene, it will spawn the youWin actor once the car goes back to the start (when it wraps back around). But neither one of these is right. I must be missing a small detail...Do I need to mess with the physics or motion on the finish line or car? How do I get the game to end when the car crosses the finish line?
Woo, that was a lot! Thanks again everyone for helping! I'm learning a ton and loving it!!!
as the youWin is spawning … to X,Y positions defined by the carActor
if the scene is wrapped …
set the spawn position X (240) and Y (160) relative to scene
or on the youWin actor: first behaviors
changeAttribute: self.Position.X To: 240 (middle of scene on X axis)
changeAttribute: self.Position.Y To: 160 (middle of scene on Y axis)
===
if not wrapped … then spawn Position > is sceneWidth - 240 and Y of course is still 160
relative to scene
@};- MH
Thank you!!! Yep, that was the problem...it was spawning off screen...duh, lol.
Eventually, I will want the final time to be displayed under the you win. It would be like a high score in the level, so if you play it again and beat your old time, the new time would appear. But if you don't beat your time, then the old time appears under the you win as the high score as well as the time you just received. If that made any sense! Anyway, that's probably way over my head at this point, but hopefully in the future I can get that to work. If you have any ideas for that, I'm all ears. Thanks again!!!
not way over your head … just focus on the getting the other things done now …
when you want to work on the displaying high-score … @tshirtbooth has excellent video(s) for this … so do those videos when you are ready!
also, we are getting writable tables … so storing the scene high-score will be simpler then
(well hopefully!
MH
1. When I play the scene for the first time, everything works, but when I replay the scene, the raceTime actor does not show up anymore. Thoughts?
2. So the raceTime actor will display the time as 00:36:11 but the bestTime actor will round down the time and displays the best time as 00:36:00. The milliseconds are always zero for the best time. Any ideas?
Cool, the writable tables sounds amazing! Would that be coming in the next version? Or how do you get it when it's available?
Woo, I'm slowly getting there! Thanks sooo much!!!
if you can … post a screenshot … please
2. what type of attribute is the bestTime actor? it should be real type to display the milliseconds
MH
1. Maybe I worded it weird...When I click on the play button to preview my game, I play it and all is working, but if I click the restart button (the circle with the arrow) still in the preview game, the race time will not display. But, if I go back to edit the scene (but don't edit anything) and click on the play button to preview my game again, then like before, everything works and the time is displayed but if I restart the scene to play again, the time doesn't show up...and so on and so on... Did you want a screenshot of the rules for the raceTime actor?
2. It is a real attribute. I duplicated the raceTime actor, so they should be the same except the bestTime actor displays the bestTime attribute instead of the raceTime attribute.
Thanks!
I will take a look at your project … might be easier to spot the problem
MH