Ninjadoodle Posted February 7, 2018 Share Posted February 7, 2018 Hi guys Here is my screen shake function. I just have to modify it to work with a moving/tweened sprite or container. The 'strength' is in pixels, and the minimum 'time' should be 100ms. PS. I know that the code is not the most concise shake: function(container, strength, time) { var dx = container.position.x; var dy = container.position.y; var num = strength / ( time / 100 ); var timer = game.Timer.add(100, shaker); var shaker = function() { game.Timer.add(0, shaker1); game.Timer.add(25, shaker2); game.Timer.add(50, shaker3); game.Timer.add(100, shaker4); } var shaker1 = function() { container.position.set(-strength, -strength); } var shaker2 = function() { container.position.set(strength, strength); } var shaker3 = function() { container.position.set(strength, -strength); } var shaker4 = function() { container.position.set(-strength, strength); if (strength > 0) { strength -= num; shaker(); } else { strength = 0; container.position.set(dx, dy); } } shaker(); }, quiphop 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted February 7, 2018 Share Posted February 7, 2018 Nice work! There are few things that doesn't make sense though, for example you are creating timer with time of 0. Also if your container's position is other than 0,0 it won't work correctly. Personally i try to avoid nested functions (function inside function) as much as possible, but if it works then that's the main thing Also the time between the shaker function varies, in first three it's 25ms but then it's 50ms. Not sure if that's intentional? Here is a bit cleaner version with just one function (just for example): shake: function(container, strength, time) { var dx = container.position.x; var dy = container.position.y; var num = strength / (time / 100); var count = 1; var shaker = function() { if (count === 1) container.position.set(dx - strength, dy - strength); else if (count === 2) container.position.set(dx + strength, dy + strength); else if (count === 3) container.position.set(dx + strength, dy - strength); else if (count === 4) container.position.set(dx - strength, dy + strength); if (count === 4 && strength > 0) { count = 1; strength -= num; } else if (count < 4) { count++; } else { container.position.set(dx, dy); timer.clear(); } }; var timer = game.Timer.add(25, shaker, true, true); } Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted February 7, 2018 Author Share Posted February 7, 2018 Hi @enpu Wow, thanks for cleaning it up! The time variance was definitely my mistake. Also, yeah I understand the timer at 0 not making sense I'm going to play with it and see if I can get it working with moving sprite. Thanks for the cleaner version lol. 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.