Shaking effect

Hello,

Does anyone know how to simulate “shaking” effect on background? Like there’s some sort of earthquake shaking land?

any help is appreciated [import]uid: 16142 topic_id: 18729 reply_id: 318729[/import]

[lua]local stage = display.getCurrentStage()

local moveRightFunction
local moveLeftFunction
local rightTrans
local leftTrans
local shakeTime = 50
local shakeRange = {min = 1, max = 25}

moveRightFunction = function(event) rightTrans = transition.to(stage, {x = math.random(shakeRange.min,shakeRange.max), y = math.random(shakeRange.min, shakeRange.max), time = shakeTime, onComplete=moveLeftFunction}); end

moveLeftFunction = function(event) leftTrans = transition.to(stage, {x = math.random(shakeRange.min,shakeRange.max) * -1, y = math.random(shakeRange.min,shakeRange.max) * -1, time = shakeTime, onComplete=moveRightFunction}); end

moveRightFunction()[/lua] [import]uid: 12704 topic_id: 18729 reply_id: 71991[/import]

thanks, i’ll try it

i didnt even think about using display.getCurrentStage, thanks) [import]uid: 16142 topic_id: 18729 reply_id: 71998[/import]

Hi guys,

@darkconsoles , sorry to be posting here into your topic but I think that my doubt links with yours as well.

So @gtatarkin , firstly thanks for this piece of code, but wouldn`t this code “shake” all the display objects instead only the background image? If so how would I set exactly which object I want to shake?

PS: Sorry if I misunderstood something as well as I am just a starter. :slight_smile:
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 18729 reply_id: 72114[/import]

As you see:
[lua]function(event) rightTrans = transition.to(stage …
function(event) leftTrans = transition.to(stage …[/lua]

object (or group of objects) named stage is shaken so if you do something like this:
[lua]local myBackgroundImage = display.newImageRect(“link-to-bckg-image.png”, 960, 640)
myBackgroundImage.x = 480
myBackgroundImage.y = 320
local stage = myBackgroundImage [/lua]
your background image only will be shaken.
Regards [import]uid: 12704 topic_id: 18729 reply_id: 72141[/import]

Hey @gtatarkin ,

I got it now!! :slight_smile:

Thank you. So much appreciated for sure.

Regards,
Rodrigo. [import]uid: 89165 topic_id: 18729 reply_id: 72154[/import]

@gtatarkin, awesome code, thanks for sharing.
I just implemented this in a game, where if my character grows bigger (powerup) and lands on a platform I shake the screen a bit.

here’s the code:

--shaking effect  
  
local stage = display.getCurrentStage()  
local originalX = stage.x  
local originalY = stage.y  
local moveRightFunction  
local moveLeftFunction  
local rightTrans  
local leftTrans  
local originalTrans  
local shakeTime = 50  
local shakeRange = {min = 1, max = 3}  
local endShake   
   
moveRightFunction = function(event) rightTrans = transition.to(stage, {x = math.random(shakeRange.min,shakeRange.max), y = math.random(shakeRange.min, shakeRange.max), time = shakeTime, onComplete=moveLeftFunction}); end   
   
moveLeftFunction = function(event) leftTrans = transition.to(stage, {x = math.random(shakeRange.min,shakeRange.max) \* -1, y = math.random(shakeRange.min,shakeRange.max) \* -1, time = shakeTime, onComplete=endShake}); end   
  
moveRightFunction();  
  
endShake = function(event) originalTrans = transition.to(stage, {x = originalX, y = originalY, time = 0}); end  
  
--end shaking effect  

[import]uid: 80305 topic_id: 18729 reply_id: 78879[/import]

Thanks for posting this. It worked perfectly. [import]uid: 70996 topic_id: 18729 reply_id: 85085[/import]