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

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
XNA Game - Open a Popup or a Custom Game Screen before launching a game
'Microsoft.Xna.Framework.Vector2' is a 'type' but is used like a variable?
Making a trading card game XNA
How do I fix 'No suitable method found to override' error on XNA 4.0?
When to use PresentationParameters.BackBufferWidth vs .Viewport.Width
What does the pixel shader function tex1D do with a Texture2d
How to add a lap timer in XNA?
Is there a Texture1D in XNA?
Particle System: XNA Framework HiDef profile does not support alpha blending on Vector4 textures

Categories

HOME
twitter
google-chrome-extension
blogger
proxy
azure-data-factory
mockito
react-router
jsp-tags
at-command
jira
cakephp-2.5
fancybox
convolution
node-notifier
applepay
ssl-client-authentication
adobe-analytics
resize
php-7.1
leiningen
try-catch
workload-scheduler
oracle-coherence
sms-gateway
beyondcompare
crystal-reports-2008
reverse-proxy
autoconf
clickonce
tapestry
autosys
one-to-many
phpfox
filezilla
social-media
log4js-node
jaxb2
unboundid
stormpath
key-value-observing
galsim
http-get
winrt-xaml-toolkit
openoffice.org
host
restlet
dartium
qwerty
web-mining
starteam
ruby-on-rails-3.1
mime
scorm
wso2carbon
parentheses
termination
prolog-setof
jquery-nestable
recursive-datastructures
csound
crypt
minimization
deadbolt-2
libpng
merge-conflict-resolution
lua-5.1
dataview
cloudbees
jms2
cartesian-product
web-controls
csquery
clicktag
jsonpickle
nsbutton
client-side-templating
device-manager
valueconverter
xsockets.net
sitemesh
spring-io
flash-builder4.5
cos
word-processor
labwindows
hosts-file
django-tagging
locationlistener
table-footer
netbeans-7.1
file-comparison
pyinotify
remember-me
html-input
suppress
fixed-width
web-architecture
ti-dsp
gacutil
ubuntu-9.04
grid-system
ugc
data-acquisition
.net-1.0

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App