Shaking effect

7 replies [Last post]
darkconsoles's picture
darkconsoles
User offline. Last seen 15 weeks 4 days ago. Offline
Joined: 17 Jan 2011

Hello,

Does anyone know how to simulate "shaking" effect on background? Like there's some sort of earthquake shaking land?

any help is appreciated

Replies

gtatarkin's picture
gtatarkin
User offline. Last seen 14 hours 16 min ago. Offline
Joined: 16 Dec 2010

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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()

darkconsoles's picture
darkconsoles
User offline. Last seen 15 weeks 4 days ago. Offline
Joined: 17 Jan 2011

thanks, i'll try it

i didnt even think about using display.getCurrentStage, thanks)

@RSCdev's picture
@RSCdev
User offline. Last seen 2 hours 38 min ago. Offline
Joined: 6 Sep 2011

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. :)

Cheers,
Rodrigo.

gtatarkin's picture
gtatarkin
User offline. Last seen 14 hours 16 min ago. Offline
Joined: 16 Dec 2010

As you see:

1
2
function(event) rightTrans = transition.to(stage ...
function(event) leftTrans = transition.to(stage ...

object (or group of objects) named stage is shaken so if you do something like this:

1
2
3
4
local myBackgroundImage = display.newImageRect("link-to-bckg-image.png", 960, 640)
myBackgroundImage.x = 480
myBackgroundImage.y = 320  
local stage = myBackgroundImage 

your background image only will be shaken.
Regards

@RSCdev's picture
@RSCdev
User offline. Last seen 2 hours 38 min ago. Offline
Joined: 6 Sep 2011

Hey @gtatarkin,

I got it now!! :)

Thank you. So much appreciated for sure.

Regards,
Rodrigo.

Insurgent Pixel
User offline. Last seen 2 weeks 6 days ago. Offline
Joined: 1 Aug 2011

@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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
--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

bradido
User offline. Last seen 1 week 5 days ago. Offline
Joined: 21 Jun 2011

Thanks for posting this. It worked perfectly.

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.