Shaking an object?

Pretty simple; I have a display object I need to visually / violently shake on-screen for a small length of time. The easiest thing that comes to mind is a recursively timed function, that is…

[code]local time = 1
local object = display.newRect(0, 0, 100, 100)

local shakeThis = function()
if time < 500 then
local shakeX, shakeY = math.random(5), math.random(5) – generate random delta
object:translate( shakeX, shakeY) – move to delta
time = time + 1 – increment counter
timer.performWithDelay(50, shakeThis) – perform again in 50ms
end
end[/code]

Not the most polished function, I know, but the end result just performs once and then moves on. The timer gets killed and that’s it. I tried using [lua]while[/lua] instead of if, which will increment time gladly…but the screen never bothers to update after the initial shakeX/shakeY calculation.

Ideas? I’m probably missing something fundamental about corona… [import]uid: 41884 topic_id: 21707 reply_id: 321707[/import]

well, you have to start the loop :wink:

local time = 1  
local function shakeThis()  
 if time \< 500 then  
 print ("beep")  
 time = time + 1 -- increment counter  
 timer.performWithDelay(50, shakeThis) -- perform again in 50ms  
 end  
end  
  
shakeThis() ------- let's go!  
  

-finefin [import]uid: 70635 topic_id: 21707 reply_id: 86156[/import]

Of course you do. :slight_smile: But if you read carefully, I do manage to trigger the function. It’s just that despite the function starting, I can’t get it to do what it seemingly should be capable of.

Using ‘if’, the function seems to quit out before the timer can perform.

Using ‘while’ the function will print as many statements as I like but the display object only moves once. (It’s either only moving the first time or maybe only doing a single update once all of the translates are performed?) [import]uid: 41884 topic_id: 21707 reply_id: 86158[/import]

ah I see, it’s a scope problem:

local shakeThis = function()  
 print ("beep!")  
 shakeThis() -- not in scope of this function  
end  
shakeThis() -- beep!  

seeing this I kinda wonder why this works:

local function shakeThis (theString)  
 print (theString)  
 shakeThis("beep2")  
end  
shakeThis("beep1")   
  
-- output: beep1 beep2 beep2 beep2...   

-finefin
EDIT:
or try this:

[code]
local fin = {}

fin.shakeThis = function (theString)
print (theString)
fin.shakeThis(“beep2”) – in scope!
end

fin.shakeThis(“beep1”)
[/code] [import]uid: 70635 topic_id: 21707 reply_id: 86165[/import]

I have no idea why the table version works. But it does seem to do so. Cool! [import]uid: 41884 topic_id: 21707 reply_id: 86187[/import]

Have you tried :
[lua]local shakeThis
shakeThis = function()
     print (“beep!”)
     shakeThis()
end
shakeThis() – beep![/lua]

or

See recursive local functions
http://www.lua.org/pil/6.2.html

Quote :

A subtle point arises in the definition of recursive local functions. The naive approach does not work here:
[lua] local fact = function (n)
if n == 0 then return 1
else return n*fact(n-1) – buggy
end
end[/lua]
When Lua compiles the call fact(n-1), in the function body, the local fact is not yet defined. Therefore, that expression calls a global fact, not the local one. To solve that problem, we must first define the local variable and then define the function:
[lua] local fact
fact = function (n)
if n == 0 then return 1
else return n*fact(n-1)
end
end[/lua]
Now the fact inside the function refers to the local variable. Its value when the function is defined does not matter; by the time the function executes, fact already has the right value. That is the way Lua expands its syntactic sugar for local functions, so you can use it for recursive functions without worrying:
[lua] local function fact (n)
if n == 0 then return 1
else return n*fact(n-1)
end
end[/lua]
Of course, this trick does not work if you have indirect recursive functions. In such cases, you must use the equivalent of an explicit forward declaration:
[lua] local f, g – `forward’ declarations

function g ()
… f() …
end

function f ()
… g() …
end[/lua]


Hope it helps. [import]uid: 95346 topic_id: 21707 reply_id: 86209[/import]

@richard9, from what I see with the translate, you object will only move in one direction, not really shake, unless that is the effect you want and are referring to as shake.

your x, y are between 1 and 5 and each time you translate, they get added in the Bottom Right direction only. [import]uid: 3826 topic_id: 21707 reply_id: 86226[/import]

Yeah, JayantV - I noticed as much when I tried the table function. Shouldn’t be too hard to solve though, I just need to apply a constant to the random output (eg: if I want -5 to + 5 I can just use math.random(11) and then use result = result - 6 to get the correct value range.) :slight_smile:

MrMells…well, that looks comprehensive. I’ll sit down and try to understand it in the morning. [import]uid: 41884 topic_id: 21707 reply_id: 86243[/import]

…“Syntactic Sugar”? Jeezus. lol I love it. Thanks for the pointer MrMells. Still only have a grasp around the edges but that was good reading. I’ll see if I can post the resulting function to code sharing later today. :slight_smile: [import]uid: 41884 topic_id: 21707 reply_id: 86316[/import]