Maybe I'm doing it the hard way....challenging scaling issue for Newbie

The objective is to make animated objects larger in time and disappear when the user clicks on them; my approach (shown below) was to use timers to scale the objects. The problem for me is that I cannot figure out how to assign each object its very own timer with timerID so they can be canceled and removed when tapped. A problem I see further down the road is how will I identify which object has been tapped so the appropriate timer is removed?

Is there an easier way to do this? I’ve looked at beebegames.lua but didn’t see how that would help this situation.

Any help is greatly appreciated!

[code]
–what happens when killed (play sound + add to score + remove timer + remove self)
local function kill(event)

media.playEventSound( brickSound )
score.setScore (score.getScore()+107)
timer.cancel(getlarger)
event.target:removeEventListener(“tap”,kill)
end
–Function to make Bad Guys
local bunch = {}
–# of bad guys
local evil = 3

local function spawnBadguy(event)

local myAnim = movieclip.newAnim{“cube01.png”, “cube02.png”, “cube03.png”, “cube04.png”, “cube05.png”, “cube06.png”}
p0_subGroup1:insert(myAnim)

local x = math.random(50,200)
local y = 200
local i = event.count

bunch[i] = myAnim
bunch[i].x = x
bunch[i].y = y

p0_subGroup2:insert(bunch[i])

local growth = .01

local function grow()

bunch[i].xScale = bunch[i].xScale + growth
bunch[i].yScale = bunch[i].yScale + growth

end

–Make Bad Guys Come at You (time between frames, function,number of scales)
getlarger = timer.performWithDelay(5,grow,1000)

– Start the animations
bunch[i]:play() – play all frames
bunch[i]:addEventListener(“tap”,kill)

end
–Spawn Bad Guy
timer.performWithDelay(1000,spawnBadguy,evil) --spawn 2 seconds apart

[/code] [import]uid: 27080 topic_id: 6083 reply_id: 306083[/import]

when grow gets called it wont know what “i” is, so you need to change that…

try something like this

[lua]local function grow(event)
local theTimer = event.source
local gameObject = theTimer.gameObject
gameObject.xScale = gameObject.xScale + growth
end

local function kill(event)
local gameObject=event.target
timer.cancel(gameObject.myGetLargerTimer)
gameObject:removeSelf()
gameObject=nil
end

local getlarger = timer.performWithDelay(5,grow,1000)
getlarger.gameObject = bunch[i]

bunch[i].myGetLargerTimer = getlarger
bunch[i]:addEventListener(“tap”,kill[/lua]

[import]uid: 6645 topic_id: 6083 reply_id: 20887[/import]

Awesome. That worked for me; thanks!

I would like to understand why that worked:

did I miss an API about adding properties to variables?

I was not aware you could connect a var with a var (theTimer.gameObject) - what does this do?

I get lost when I read “.myGetLargerTimer” - is this a timerID?

I believe you’ve named each object and then named each timer with the objects name attached so that you could use event.target to fill in the name in the kill function - did I get this right?

“give a man a fish and you feed him for a day, teach a man to fish and you’ll have fed him his whole lifetime” - some guy

many thanks again! [import]uid: 27080 topic_id: 6083 reply_id: 20980[/import]

read up on Lua tables. pretty much everything in Lua is a table. therefore you can assign whatever property you want to it

i set .gameObject on the timer so I can access a reference/pointer to my game object from the timer complete event.

.myGetLargerTimer is just a pointer to your timer. I store this against the game object bunch[i] so that in the tap event I can reference the timer

go here http://www.lua.org/cgi-bin/demo (or corona) and type this

[lua]local A = {“red”,“green”,“blue”} – a table
local b = {} – a table
b.pointerToA = A
print(b.pointerToA[2]) – “green”
A[2]=“orange”
print(b.pointerToA[2]) – “orange”

local anotherPointerToA = b.pointerToA
anotherPointerToA[3] = “yellow”
print(A[3]) – “yellow”[/lua]

i think you’ve understood what i’m doing essentially though. I could have named “gameObject” and “myGetLargerTimer” anything i wanted. i just did it like that to be descriptive to you. i’d probably actually call them “obj” and “t”

[import]uid: 6645 topic_id: 6083 reply_id: 20985[/import]

one thing to be aware of though. if you *redefine* “A” eg A={1,2,3} that actually creates a *new* table at a new address. the pointer’s to A will no longer point to the original “A” (as you have created a new “A”)

[lua]local A = {“red”,“green”,“blue”} – a table
print(tostring(A)) – take note of this address eg 0x633510
local b = {} – a table

b.pointerToA = A
print(tostring(b.pointerToA)) – note address, same as above 0x633510
print(b.pointerToA[2]) – “green”

A[2]=“orange”
print(b.pointerToA[2]) – “orange”

local anotherPointerToA = b.pointerToA
anotherPointerToA[3] = “yellow”

print(A[1],A[2],A[3]) – red orange yellow

A={“dog”,“monkey”,“snake”} – make a new A
print(b.pointerToA[2]) – “orange” ??!

– explanation: A now points to a *new* table so the old pointers are broken

print(tostring(A)) – see the address has changed? eg 0x632e20 not 0x633510

– fix: point to the new A
b.pointerToA = A
anotherPointerToA = A
print(b.pointerToA[2]) – “monkey”

–[[
note though that the old A table has no-pointers to it,
but still exists in memory (this might cause a memory leak?)

or you could have said

A[1]=“dog”,
A[2]=“monkey”,
A[3]=“snake”

for instance rather than creating a new A
–]][/lua] [import]uid: 6645 topic_id: 6083 reply_id: 20989[/import]