Sprite spawning at wrong time

0 favourites
  • 7 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • I'm not sure what I'm doing wrong here, but I can't seem to get the sprite to spawn at the correct interval.

    I have some events that add 0.1 to a global variable every 0.1 seconds. This lets me reset the timespan when a level restarts. At 0.3 seconds I spawn a new sprite, which works fine. But as soon as I pass 0.7 seconds mark, sprites start to spawn with a 0.1 second delay.

    <img src="http://i.imgur.com/8VIAU.png" border="0">

    Sample Capx

    Even stranger, while the 0.3 above appears to work if using >=, this does not (the sprite never spawns):

    <img src="http://i.imgur.com/n2Rfq.png" border="0">

    Sample Capx

    Can anyone spot what I'm doing wrong? It seems this should be working <img src="smileys/smiley5.gif" border="0" align="middle">

  • Computers cannot represent 0.1 exactly in binary, so a very close approximation a little less than 0.1 is used. That difference increases every time 0.1 is added.

    One solution would be to replace the TimeSpan=0.8 condition with:

    System Compare abs(TimeSpan-0.8) < 0.0001

    Another would be to only add integers to TimeSpan and set the text to TimeSpan/10.

    A final idea would be to instead of adding to TimeSpan every 0.1 sec, add dt to TimeSpan every tick and set the text to int(TimeSpan*10)/10.

  • Setting text to a number rounds the number (to 5 decimal places) which is why it looks like it should be working. Set your text to str(TimeSpan) and you'll see what's happening.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Setting text to a number rounds the number (to 5 decimal places) which is why it looks like it should be working. Set your text to str(TimeSpan) and you'll see what's happening.

    OH, that makes sense. I wasn't aware that it rounds it. That's a little annoying =/

    R0J0hound: I'll give the dt trick a shot. I can't remember exactly why I didn't do that, I remember running into some weird timing problems but I can't for the life of me remember what they were. I'll give it another try and see how it goes.

  • My question is why are you changing the system time scale? and do you need to judge your time differnce as you are?

    I suggest using something like this.

    dl.dropbox.com/u/14087254/wierdtimebug.capx

    also keep in mind when you have a constantly incrementing value try to compare with <= or >= not just =.

  • jayderyu: Changing the timescale was just so I could demonstrate the oddity I was seeing, without the numbers flying by in milliseconds. As for why I'm recording it to a variable, it's so that when I restart the level (when the player dies) I can reset it back to 0.

    R0J0hound: I remember the issue I had originally. It was that when I was using dt I was finding it hard to say "every 0.3" because it was rare for the dt to match - it would jump from "0.29897" to "0.31102" and my statement would miss. I thought that by just saying "every 0.1 seconds add 0.1 to my local (and reset-able) timespan" I got around that. I didn't realize it was having the exact same affect, but it was just rounding for me!

    I think I've learnt a little bit more about how time works in games. I tells ya, I think understanding time and framerates is one of the trickier subjects to get your head around as an amateur game developer <img src="smileys/smiley9.gif" border="0" align="middle" />

  • About your dt issue, you can do an "every x second" this way:

    +Every ticks 
       -> add dt to timer 
    +timer >= x 
       -> substract x from timer 
       -> do things
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)