1 decade ago by matthew
I wanted to implement a shaking motion to my entity upon being clicked. This is how I implemented it:
The timer was necessary or otherwise the shaking is not noticeable to the naked eye. The downside is that the shaking is inconsistent (due to using Timer, which is influenced by frame rate).
If any one has a better solution, please post.
shake: function() { if(this.shakeDirection == "") { this.body.ApplyImpulse(new b2.Vec2(-10,0), this.body.GetPosition()); this.shakeDirection = "left"; this.timer = new ig.Timer(); this.timer.set(0.1); } else if(this.shakeDirection == "left" && this.timer.delta() >= 0) { this.body.ApplyImpulse(new b2.Vec2(20,0), this.body.GetPosition()); this.shakeDirection = "right"; this.timer.set(0.1); } else if(this.shakeDirection == "right" && this.timer.delta() >= 0) { this.body.ApplyImpulse(new b2.Vec2(-20,0), this.body.GetPosition()); this.shakeDirection = "left"; this.timer.set(0.1); } },
The timer was necessary or otherwise the shaking is not noticeable to the naked eye. The downside is that the shaking is inconsistent (due to using Timer, which is influenced by frame rate).
If any one has a better solution, please post.