xna-4.0


Can't reset my gametime to zero in XNA


I'm creating a game with XNA 4.0 and I have a problem with resetting the game time in my game,
the problem is I'm using this code for adding my objects in game:
Timespan prevSpawn = timespan.zero;
Timespan objectSpawnTime = timespan.fromsec(5);
if (gameTime.TotalGameTime - prevSpawn> objectSpawnTime)
{
prevSpawn = gameTime.TotalGameTime;
AddObject();
}
I want to reset the game time to zero, when start the game again or game end and goes to mainmenu and hit the play again, I want to reset gametime.
As i understand from the MSDN entry below, the GameTime.TotalGameTime property will continue 'ticking' until you end the game, or, entire application.
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gametime.totalgametime.aspx
Perhaps it would be wiser to use your own object, for which you can reset with any of your actions and hence have more control over the time span. Then you could still use this property for counting the difference from one and the other.
i.e. when you reset/restart etc, do not refer to TotalGameTime and try to make it be zero, but refer to your own object's timespan, or to (TotalGameTime - TimeWhenReset) where TimeWhenReset is the timespan copied from TotalGameTime when you reset.
This is untested, but I think that it should work if I understood correctly.
For a more accurate timer, you would be better using a time span, and a DateTime vairable. Set the DateTime to now when you want to start the timer, and in the TimeSpan do something like this:
timer = StartTime - DateTime.Now;
This will give you a timer you can deploy at any time.

Related Links

Platformer tile engine--advice on what to do with long tiles
XNA Vector2 path contained inside rectangle
XNA changing 200 or so tiles pixels
Loading files during run time in XNA 4.0
Optimized keyboard controls for XNA game
Best structure for xna Arcade Game
xna 4 taking desktop screenshot
Can't reset my gametime to zero in XNA
Displaying seconds in XNA with Keypress?
XNA game how to make a snake tail
Creating an array of game objects
Lap timer in XNA 4.0?
XNA: Identifying identical sprites created with for loop
Increase Character Spacing in XNA
XNA choosing a subgroup of Game.Components
Loading Silverlight Texture2D from Resources

Categories

HOME
multithreading
wso2
keras
homebrew
include
relay
installshield
ios-charts
paradox
autotools
tomcat6
here-api
spring-kafka
rascal
handsontable
alignment
windows-phone-7
size
clearcase-ucm
after-effects
apache-metamodel
textfield
amazonsellercentral
clickonce
hammerspoon
facet
elasticsearch-ruby
compatibility
jspm
facebook-apps
windowbuilder
procdump
winrt-xaml-toolkit
html5-fullscreen
wixsharp
web-mining
withings
sfdc
arena-simulation
midl
s
qcombobox
jsch
dotcover
appcompat
hibernate-tools
dds
blogengine.net
veracode
dtexec
windows-mobile-6.5
flutterwave
bind9
word-vba-mac
setuptools
google-web-starter-kit
impresspages
clang-static-analyzer
markojs
lua-5.1
nessus
teamcity-8.0
metaclass
pundit
computer-algebra-systems
sframe
android-listview
service-accounts
generic-programming
key-management
nsmutabledictionary
rake-task
gulp-less
zend-route
article
hyprlinkr
resty-gwt
oracle-warehouse-builder
jplaton
bitsharp
pysimplesoap
cascalog
hosts-file
jquery-ui-layout
subscript
zpt
visitor-statistic
coda-slider
sortable-tables
dentrix
mozilla-prism
bespin
database-management
memory-size
photoshop-cs4
boost-smart-ptr
privilege
aquaticprime

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App