[BEHAVIOR] Shake (not camera)

0 favourites
From the Asset Store
Simple and easily editable template for a dynamic camera that zooms in and out based on how far apart the players are.
  • MPPlantOfficial

    c3runtime version.

    https://github.com/erenertugrul/construct-plugins/tree/master/ported_plugins/lg_shake/c3/dist

    jatin1726

    shake axis mode version

    https://github.com/erenertugrul/construct-plugins/tree/master/ported_plugins/lg_shake/c2/dist

    Thanks EREN, it was really helpful. Also want to share some issues after using it. like if i want to shake object only in x axis then it is not smooth as rex shake mod plus it doesnt end the shake of object by keeping it in original position of the object. How to do that?

  • it is not smooth as rex shake mod plus it doesnt end the shake of object by keeping it in original position of the object. How to do that?

    i edited original shake behavior. not rex shake mod.

    if you want keeping it in original position of the object check "enforce position"

    Also here rex_shake_mod_axis_mode

    https://drive.google.com/file/d/1pgkA0nQCuaGkBSspFTMtga8Q3zbzyk9t/view?usp=sharing

  • > it is not smooth as rex shake mod plus it doesnt end the shake of object by keeping it in original position of the object. How to do that?

    i edited original shake behavior. not rex shake mod.

    if you want keeping it in original position of the object check "enforce position"

    Also here rex_shake_mod_axis_mode

    https://drive.google.com/file/d/1pgkA0nQCuaGkBSspFTMtga8Q3zbzyk9t/view?usp=sharing

    I tried enforce position but it is not working. Also, rex plugin is also not showing any option for restricting movment to x axis only

  • rex plugin is also not showing any option for restricting movment to x axis only

    behavior in the settings menu

  • > rex plugin is also not showing any option for restricting movment to x axis only

    behavior in the settings menu

    Thanks a lot, updated plugin is working very right. Earlier it was not showing x & y axis option.

    God bless u.

  • Hi, not sure if this is a bug, but it can be replicated.

    Apply the behavior to a button .

    Make it so that when you click it it shakes [Enforce Position].

    If you click and wait for the shake to end, the position will indeed be respected.

    Now...

    Click it again and again really fast.

    Soon the button will no longer be at its original position even though Enforce Position is on.

  • This is such a useful plugin, can someone PLEASE convert this to a basic JavaScript function like:

    shake(object, are the rest of the argument this add-on requires);

    So that we can use it for everyday JavaScript DOM elements outside Construct 2 ?

  • There is a way via css, no need for a js lib.

    div {
     width: 50px;
     padding: 50px;
     background: gold;
     text-align: center;
    }
    div:hover { 
     -webkit-animation: shake 0.1s ease-in-out 0.1s infinite alternate;
    }
    
    

    shake { [/p] from { [/p] -webkit-transform: rotate(10deg); [/p] } [/p] to { [/p] -webkit-transform-origin:center center; [/p] -webkit-transform: rotate(-10deg); [/p] } [/p] [/p] }

    <div>Hello</div>
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • First of all, thank you.

    I appreciate the effort you took to type out all these codes, really thank you.

    However, it is just not the same, I do not know how to call that function from within JavaScript, this only works for hovers and stuff, it's not like I can call that CSS function from JavaScript when an enemy is attacked shake the enemy right ?

    Second of all, even if that were possible, that CSS alternative is just no match for the real deal in this add-on.

    It not only have control over Magnitude and Duration [Duration is a big deal], you also have the almighty "Reducing Magnitude" mode.

    AND AND AND it have the ability to enforce its position at the end !

    This is truly the most powerful add-on for object shake, I just wish I can isolate the add-on's code that does this, there are so many wrapper codes in the add-on to make it into an add on :-(

  • // ECMAScript 5 strict mode

    "use strict";

    assert2(cr, "cr namespace not created");

    assert2(cr.behaviors, "cr.behaviors not created");

    /////////////////////////////////////

    // Behavior class

    cr.behaviors.lgshake = function(runtime)

    {

    this.runtime = runtime;

    this.shakeMag = 0;

    this.shakeStart = 0;

    this.shakeEnd = 0;

    this.shakeMode = 0;

    this.shakeEnforcePosition = 0;

    this.shakeOriginalX = 0;

    this.shakeOriginalY = 0;

    this.axis = 0; //eklenen

    };

    (function ()

    {

    var behaviorProto = cr.behaviors.lgshake.prototype;

    /////////////////////////////////////

    // Behavior type class

    behaviorProto.Type = function(behavior, objtype)

    {

    this.behavior = behavior;

    this.objtype = objtype;

    this.runtime = behavior.runtime;

    };

    var behtypeProto = behaviorProto.Type.prototype;

    behtypeProto.onCreate = function()

    {

    };

    /////////////////////////////////////

    // Behavior instance class

    behaviorProto.Instance = function(type, inst)

    {

    this.type = type;

    this.behavior = type.behavior;

    this.inst = inst; // associated object instance to modify

    this.runtime = type.runtime;

    };

    var behinstProto = behaviorProto.Instance.prototype;

    behinstProto.onCreate = function()

    {

    this.enabled = (this.properties[0] !== 0);

    };

    behinstProto.saveToJSON = function ()

    {

    return {

    "smg": this.shakeMag,

    "ss": this.shakeStart,

    "se": this.shakeEnd,

    "smd": this.shakeMode

    };

    };

    behinstProto.loadFromJSON = function (o)

    {

    this.shakeMag = o["smg"];

    this.shakeStart = o["ss"];

    this.shakeEnd = o["se"];

    this.shakeMode = o["smd"];

    };

    behinstProto.tick = function ()

    {

    // Do work in tick2 instead, to eliminate one-frame lag if object position changes in events

    };

    function getShakeBehavior(inst)

    {

    var i, len, binst;

    for (i = 0, len = inst.behavior_insts.length; i < len; ++i)

    {

    binst = inst.behavior_insts;

    if (binst.behavior instanceof cr.behaviors.lgshake)

    return binst;

    }

    return null;

    };

    behinstProto.tick2 = function ()

    {

    if (!this.enabled)

    return;

    // Is in a shake?

    var now = this.runtime.kahanTime.sum;

    var offx = 0, offy = 0;

    if (now >= this.shakeStart && now < this.shakeEnd)

    {

    var mag = this.shakeMag * Math.min(this.runtime.timescale, 1);

    // Mode 0 - reducing magnitude - lerp to zero

    if (this.shakeMode === 0)

    mag *= 1 - (now - this.shakeStart) / (this.shakeEnd - this.shakeStart);

    var a = Math.random() * Math.PI * 2;

    var d = Math.random() * mag;

    offx = Math.cos(a) * d;

    offy = Math.sin(a) * d;

    }

    //update only when necessary and one more time to enforce object position

    if (offx != 0 || offy != 0 || (this.shakeEnforcePosition === 1 && this.shakeStart > 0)) {

    if (this.axis == 1){

    this.inst.x = this.shakeEnforcePosition ? this.shakeOriginalX + offx : this.inst.x + offx;

    }

    else if (this.axis == 2){

    this.inst.y = this.shakeEnforcePosition ? this.shakeOriginalY + offy : this.inst.y + offy;

    }

    else{

    this.inst.x = this.shakeEnforcePosition ? this.shakeOriginalX + offx : this.inst.x + offx;

    this.inst.y = this.shakeEnforcePosition ? this.shakeOriginalY + offy : this.inst.y + offy;

    }

    this.inst.set_bbox_changed();

    //turn off

    if (this.shakeEnforcePosition === 1 && now > this.shakeEnd)

    this.shakeStart = 0;

    }

    };

    //////////////////////////////////////

    // Actions

    function Acts() {};

    Acts.prototype.Shake = function (mag, dur, mode, enforcePosition,axis)

    {

    this.shakeMag = mag;

    this.shakeStart = this.runtime.kahanTime.sum;

    this.shakeEnd = this.shakeStart + dur;

    this.shakeMode = mode;

    this.shakeEnforcePosition = enforcePosition;

    this.shakeOriginalX = this.inst.x;

    this.shakeOriginalY = this.inst.y;

    this.axis = axis; //eklenen

    };

    Acts.prototype.SetEnabled = function (e)

    {

    this.enabled = (e !== 0);

    };

    behaviorProto.acts = new Acts();

    }());

    This is the source code the object shake.

    So useful across the board.

    I am going to try to reduce it to a generic JavaScript function, preferably:

    Object_Shake(Any_Dom_Div, Magnitude, Duration, Reducing_magnitude, Enforce_position, shake_x_y); //Arguments have been exaggeratedly verbose to make it easier to explain intention.

  • Simplifying the codes, removing all unnecessary stuff, I know there are two functions with the same name but I am still simplifying the function to make it a self contain useful function for now so naming doesn't matter for now, still work in progress:

    function shake()

    {

    this.shakeMag = 0;

    this.shakeStart = 0;

    this.shakeEnd = 0;

    this.shakeMode = 0;

    this.shakeEnforcePosition = 0;

    this.shakeOriginalX = 0;

    this.shakeOriginalY = 0;

    this.axis = 0;

    }

    function tick2()

    {

    if (!this.enabled)

    {

    return;

    }

    // Is in a shake?

    var now = this.runtime.kahanTime.sum;

    var offx = 0, offy = 0;

    if (now >= this.shakeStart && now < this.shakeEnd)

    {

    var mag = this.shakeMag * Math.min(this.runtime.timescale, 1);

    // Mode 0 - reducing magnitude - lerp to zero

    if (this.shakeMode === 0)

    {

    mag *= 1 - (now - this.shakeStart) / (this.shakeEnd - this.shakeStart);

    }

    var a = Math.random() * Math.PI * 2;

    var d = Math.random() * mag;

    offx = Math.cos(a) * d;

    offy = Math.sin(a) * d;

    }

    //update only when necessary and one more time to enforce object position

    if (offx != 0 || offy != 0 || (this.shakeEnforcePosition === 1 && this.shakeStart > 0))

    {

    if (this.axis == 1)

    {

    this.inst.x = this.shakeEnforcePosition ? this.shakeOriginalX + offx : this.inst.x + offx;

    }

    else if (this.axis == 2)

    {

    this.inst.y = this.shakeEnforcePosition ? this.shakeOriginalY + offy : this.inst.y + offy;

    }

    else

    {

    this.inst.x = this.shakeEnforcePosition ? this.shakeOriginalX + offx : this.inst.x + offx;

    this.inst.y = this.shakeEnforcePosition ? this.shakeOriginalY + offy : this.inst.y + offy;

    }

    this.inst.set_bbox_changed();

    //turn off

    if (this.shakeEnforcePosition === 1 && now > this.shakeEnd)

    {

    this.shakeStart = 0;

    }

    }

    }

    function Shake(mag, dur, mode, enforcePosition, axis)

    {

    this.shakeMag = mag;

    this.shakeStart = this.runtime.kahanTime.sum;

    this.shakeEnd = this.shakeStart + dur;

    this.shakeMode = mode;

    this.shakeEnforcePosition = enforcePosition;

    this.shakeOriginalX = this.inst.x;

    this.shakeOriginalY = this.inst.y;

    this.axis = axis;

    }

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)