Jump to content

Here is my Screen Shake function


Ninjadoodle
 Share

Recommended Posts

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();
},

 

Link to comment
Share on other sites

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);
}

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...