Ninjadoodle Posted April 26, 2018 Share Posted April 26, 2018 Hi @enpu I'm trying to speed up the game with game.timer.speed, and it seems to work on all my objects, apart from my player ship rotation. Any idea why the rotation of the ship is not speeding up? I'm using this to rotate the ship ... update: function() { this.stageSetup.updateStage(); if (this.spinning) { if (this.shipSpeed < this.shipLimit) { this.shipSpeed += 0.0005 * Math.PI; } if (this.dir === 1) { this.ship.sprite.rotation += this.shipSpeed * Math.PI; } else { this.ship.sprite.rotation -= this.shipSpeed * Math.PI; } } }, Thank you! Quote Link to comment Share on other sites More sharing options...
enpu Posted April 26, 2018 Share Posted April 26, 2018 You should multiply the values with game.delta Always when moving something in the update function, it's a good idea to multiply the value with game.delta. This way the movement will be synced with different frame rates and game speed. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 26, 2018 Author Share Posted April 26, 2018 @enpu - Ahh ok, I thought I was missing something. Thanks! Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 26, 2018 Author Share Posted April 26, 2018 @enpu - Hmmm, now the ship is turning super slow. I've tried using game.delta here .... this.shipSpeed += 0.0005 * Math.PI * game.delta; Quote Link to comment Share on other sites More sharing options...
enpu Posted April 26, 2018 Share Posted April 26, 2018 Well increase that 0.0005 value Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 26, 2018 Author Share Posted April 26, 2018 @enpu - Ok, I'm not actually understanding how game.delta works - I was under the impression it was tied to framerate so didn't actually affect values in code. Quote Link to comment Share on other sites More sharing options...
enpu Posted April 26, 2018 Share Posted April 26, 2018 game.delta is time in seconds that tells how long did it take to get from last frame into the current frame. If your game runs on 60 frames per seconds, that would mean that one frame would take around 0.17 seconds to finish. Usually games don't run exactly 60 fps all the time, this means that the game.delta value changes a little in every frame. Now let's say we want to move a sprite every frame and keep the speed constant. If we just increase the position value with hard number like 10, that would mean that if the last frame did take longer than 0.17 seconds to finish, it would look like the sprite is slowing down. But if we multiply the value with game.delta every frame, we would end up with a value that is changing based on how long did the last frame take. So if the last frame did take longer, the value will be bigger and the sprite will be moved more. This way it would look like the sprite is moving same speed all the time, no matter how long the frames are taking. Does that make sense? Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 26, 2018 Author Share Posted April 26, 2018 Hi @enpu - Yup, thank you for the explanation My values are all messed up now, so gotta see what I can do to fix it lol. Quote Link to comment Share on other sites More sharing options...
enpu Posted April 26, 2018 Share Posted April 26, 2018 Try to multiply your values with (game.delta / 0.017). That should keep them about as what they were before not using the game.delta value sprite.position.x += 0.0005 * (game.delta / 0.017); No as clean as just multiplying with game.delta Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 26, 2018 Author Share Posted April 26, 2018 @enpu - I have a LOT of values in my code, when do I need to multiply by game.delta? Do I multiply when I'm setting a variable eg. speed = 5 * game.delta or, only when I'm moving / rotating a sprite ? Thank you! Quote Link to comment Share on other sites More sharing options...
enpu Posted April 26, 2018 Share Posted April 26, 2018 Only when changing values every frame and you want it to be synced to frame rate. So for example when moving or rotating Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 26, 2018 Author Share Posted April 26, 2018 @enpu - I'm less confused now lol - Thank you! Quote Link to comment Share on other sites More sharing options...
enpu Posted May 23, 2018 Share Posted May 23, 2018 Small clarification on what is the difference, when moving sprite using delta value vs when using just fixed number: // This will move sprite 100 pixels in one second sprite.position.x += 100 * game.delta; // This will move sprite 100 pixels every frame sprite.position.x += 100; So multiplying value with game.delta will sync it into time. 8bitdna and Ninjadoodle 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.