is there a way to simplify this snippet based on a table ?

hi,

I search a way to simplify this snippet. I have characters with a table view and i have a lot a function depending of them. i think that with a boucle it’s possible but i don’t see how…

Thanks for your tips.

local function oncompleteKill1() Character[1].flag = 3 buttonclic = true print("oncompleteKill1") return Character[1].flag, buttonclic end local function killCharacter1()     if Character[1].flag == 1 or Character[1].flag == 4 then         Character[1].flag = 2         transition.cancel( Character[1].transition )         pfiuSound()         Character[1]:setSequence( "kill" )         Character[1].kill=transition.to( Character[1], {alpha=0, time=1000, onComplete=oncompleteKill1} )         print("killCharacter1")     end         return Character[1].flag end local function oncompleteKill2() Character[2].flag = 3 buttonclic = true print("oncompleteKill2") return Character[2].flag, buttonclic end local function killCharacter2()     if Character[2].flag == 1 or Character[2].flag == 4 then         Character[2].flag = 2         transition.cancel( Character[2].transition )         pfiuSound()         Character[2]:setSequence( "kill" )         Character[2].kill=transition.to( Character[2], {alpha=0, time=1000, onComplete=oncompleteKill2} )         print("killCharacter2")     end         return Character[2].flag end local function oncompleteKill3() Character[3].flag = 3 buttonclic = true print("oncompleteKill3") return Character[3].flag, buttonclic end local function killCharacter3()     if Character[3].flag == 1 or Character[3].flag == 4 then         Character[3].flag = 2         transition.cancel( Character[3].transition )         pfiuSound()         Character[3]:setSequence( "kill" )         Character[3].kill=transition.to( Character[3], {alpha=0, time=1000, onComplete=oncompleteKill3} )         print("killCharacter3")     end         return Character[3].flag end

You are doing the same thing for each character in the list, but duplicating the code for each. You should have one function which can be called for each character. So, create your functions separately and give them a parameter which indicates the character being killed. Something like this:

local function oncompleteKill( chr ) -- local chr = chr.target -- not sure if chr is a table with .target==character or not chr.flag = 3 buttonclic = true print("oncompleteKill1") return chr.flag, buttonclic end local function killCharacter( chr ) if chr.flag == 1 or chr.flag == 4 then chr.flag = 2 transition.cancel( chr.transition ) pfiuSound() chr:setSequence( "kill" ) chr.kill=transition.to( chr, {alpha=0, time=1000, onComplete=oncompleteKill } ) print("killCharacter") end return chr.flag end

I’m not sure if the parameter passed into the ‘oncompleteKill’ function is the character being transitioned or a table with .target being the character, but it would be a small change.

This is an important lesson to learn in coding: Code Reuse. Always write any function so that it can be reused and not specific to one instance of anything.

hi Horacebury, thanks

but i don’t understand really the sens of this…could you be more explicit … : )

– local chr = chr.target – not sure if chr is a table with .target==character or not

the character who passed into the ‘oncompleteKill’ function is the character being transitioned.

so i must put

local chr = Character.target

my table view is something like that :

local Character = {} Character[1] = display.newSprite( myGroupCharacter, mySheet, sequenceData1 ) Character[1].x = 240 Character[1].y = 160 Character[1].alpha = 0.5 Character[1].xScale = .5 Character[1].yScale = .5 Character[1]:setSequence( "anim" ) Character[1]:play() Character[1].myId = 1 Character[1].flag = 1 Character[1].time = 1000 Character[1].shadow = display.newImageRect(myGroupShadow,"shadow.png",100,80) Character[1].shadow.x = 200 Character[1].shadow.alpha = 0.2 Character[2] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[2].x = 240 Character[2].y = 260 Character[2].alpha = 0.5 Character[2].xScale = .2 Character[2].yScale = .2 Character[2]:setSequence( "anim" ) Character[2]:play() Character[2].myId = 2 Character[2].flag = 1 Character[2].time = 600 Character[3] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[3].x = 240 Character[3].y = 260 Character[3].alpha = 0.5 Character[3].xScale = .2 Character[3].yScale = .2 Character[3]:setSequence( "anim" ) Character[3]:play() Character[3].myId = 3 Character[3].flag = 1 Character[3].time = 500 Character[4] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[4].x = 240 Character[4].y = 260 Character[4].alpha = 0.5 Character[4].xScale = .2 Character[4].yScale = .2 Character[4]:setSequence( "anim" ) Character[4]:play() Character[4].myId = 4 Character[4].flag = 1 Character[4].time = 800

Ignore that line. I was being dumb.

hi,

ihave tried what you explain but i have doing some errors i think…is it just ?

local Character = {} Character[1] = display.newSprite( myGroupCharacter, mySheet, sequenceData1 ) Character[1].x = 240 Character[1].y = 160 Character[1].alpha = 0.5 Character[1].xScale = .5 Character[1].yScale = .5 Character[1]:setSequence( "anim" ) Character[1]:play() Character[1].myId = 1 Character[1].flag = 1 Character[2] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[2].x = 240 Character[2].y = 260 Character[2].alpha = 0.5 Character[2].xScale = .2 Character[2].yScale = .2 Character[2]:setSequence( "anim" ) Character[2]:play() Character[2].myId = 2 Character[2].flag = 1 local chr = Character.target local function displacement(chr)     if chr.flag == 1 then     chr.flag = 4 print("chr.flag", chr.flag) end displacement(chr) --the error is \>\>\> local chr = Character.target --how do you code this ? 

thans and sorry to not understand quickly :frowning:

any idea ? to have only one function

any idea ? to have only one function

I allow myself to seek further assistance from the forum.
is it a way to simplify the code I posted above ?
thank you for your help

i have finded but now i don’t see how to do to have 4 different functions with the Characters.

local param1, param2, param3, param4 = Character[1], Character[1].flag, Character[1].shadow, Character[1].time

must be also :

local param1, param2, param3, param4 = Character[2], Character[2].flag, Character[2].shadow, Character[2].time

so how to have a unique function who recognize the good param if these are changing ?

local param1, param2, param3, param4 = Character[1], Character[1].flag, Character[1].shadow, Character[1].time local function displacementitself1(w, wflag, wshadow, wtime)     print("displacementitself1")     if wflag == 1 then     wflag = 4      print("w",w, "wflag",wflag,"wshadow",wshadow, "wtime", wtime) local function returnflag1(wflag)         print("returnflag1")         wflag = 1         displacementitself1(w, wflag, wshadow, wtime)         return wflag end --returnflag local function randomposition1()     sidex1 = mathr(-10,400)     sidey1 = mathr(-10,400)        print ("randomposition1", sidex1, sidey1)     return sidex1, sidey1 end --randomposition local function displacement1(w, wflag, wshadow, wtime)     if wflag == 4 then     print("displacement1")     w.transition = transition.to(w, {tag="displacement1", time=wtime,x=sidex1,alpha=1, y=sidey1, yScale=.35, xScale =.35, onComplete=returnflag1})     w.transitionshadow = transition.to(wshadow, {tag="displacement1", time=wtime,x=sidex1-20,alpha=0.5, y=sidey1-5, yScale=1, xScale =1})     end --if end -- displacement randomposition1() displacement1(w, wflag, wshadow, wtime) return wflag end return wflag end --displacementitself displacementitself1(param1, param2, param3, param4)

You are doing the same thing for each character in the list, but duplicating the code for each. You should have one function which can be called for each character. So, create your functions separately and give them a parameter which indicates the character being killed. Something like this:

local function oncompleteKill( chr ) -- local chr = chr.target -- not sure if chr is a table with .target==character or not chr.flag = 3 buttonclic = true print("oncompleteKill1") return chr.flag, buttonclic end local function killCharacter( chr ) if chr.flag == 1 or chr.flag == 4 then chr.flag = 2 transition.cancel( chr.transition ) pfiuSound() chr:setSequence( "kill" ) chr.kill=transition.to( chr, {alpha=0, time=1000, onComplete=oncompleteKill } ) print("killCharacter") end return chr.flag end

I’m not sure if the parameter passed into the ‘oncompleteKill’ function is the character being transitioned or a table with .target being the character, but it would be a small change.

This is an important lesson to learn in coding: Code Reuse. Always write any function so that it can be reused and not specific to one instance of anything.

hi Horacebury, thanks

but i don’t understand really the sens of this…could you be more explicit … : )

– local chr = chr.target – not sure if chr is a table with .target==character or not

the character who passed into the ‘oncompleteKill’ function is the character being transitioned.

so i must put

local chr = Character.target

my table view is something like that :

local Character = {} Character[1] = display.newSprite( myGroupCharacter, mySheet, sequenceData1 ) Character[1].x = 240 Character[1].y = 160 Character[1].alpha = 0.5 Character[1].xScale = .5 Character[1].yScale = .5 Character[1]:setSequence( "anim" ) Character[1]:play() Character[1].myId = 1 Character[1].flag = 1 Character[1].time = 1000 Character[1].shadow = display.newImageRect(myGroupShadow,"shadow.png",100,80) Character[1].shadow.x = 200 Character[1].shadow.alpha = 0.2 Character[2] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[2].x = 240 Character[2].y = 260 Character[2].alpha = 0.5 Character[2].xScale = .2 Character[2].yScale = .2 Character[2]:setSequence( "anim" ) Character[2]:play() Character[2].myId = 2 Character[2].flag = 1 Character[2].time = 600 Character[3] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[3].x = 240 Character[3].y = 260 Character[3].alpha = 0.5 Character[3].xScale = .2 Character[3].yScale = .2 Character[3]:setSequence( "anim" ) Character[3]:play() Character[3].myId = 3 Character[3].flag = 1 Character[3].time = 500 Character[4] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[4].x = 240 Character[4].y = 260 Character[4].alpha = 0.5 Character[4].xScale = .2 Character[4].yScale = .2 Character[4]:setSequence( "anim" ) Character[4]:play() Character[4].myId = 4 Character[4].flag = 1 Character[4].time = 800

Ignore that line. I was being dumb.

hi,

ihave tried what you explain but i have doing some errors i think…is it just ?

local Character = {} Character[1] = display.newSprite( myGroupCharacter, mySheet, sequenceData1 ) Character[1].x = 240 Character[1].y = 160 Character[1].alpha = 0.5 Character[1].xScale = .5 Character[1].yScale = .5 Character[1]:setSequence( "anim" ) Character[1]:play() Character[1].myId = 1 Character[1].flag = 1 Character[2] = display.newSprite( myGroupCharacter, mySheet, sequenceData2 ) Character[2].x = 240 Character[2].y = 260 Character[2].alpha = 0.5 Character[2].xScale = .2 Character[2].yScale = .2 Character[2]:setSequence( "anim" ) Character[2]:play() Character[2].myId = 2 Character[2].flag = 1 local chr = Character.target local function displacement(chr)     if chr.flag == 1 then     chr.flag = 4 print("chr.flag", chr.flag) end displacement(chr) --the error is \>\>\> local chr = Character.target --how do you code this ? 

thans and sorry to not understand quickly :frowning:

any idea ? to have only one function

any idea ? to have only one function

I allow myself to seek further assistance from the forum.
is it a way to simplify the code I posted above ?
thank you for your help

i have finded but now i don’t see how to do to have 4 different functions with the Characters.

local param1, param2, param3, param4 = Character[1], Character[1].flag, Character[1].shadow, Character[1].time

must be also :

local param1, param2, param3, param4 = Character[2], Character[2].flag, Character[2].shadow, Character[2].time

so how to have a unique function who recognize the good param if these are changing ?

local param1, param2, param3, param4 = Character[1], Character[1].flag, Character[1].shadow, Character[1].time local function displacementitself1(w, wflag, wshadow, wtime)     print("displacementitself1")     if wflag == 1 then     wflag = 4      print("w",w, "wflag",wflag,"wshadow",wshadow, "wtime", wtime) local function returnflag1(wflag)         print("returnflag1")         wflag = 1         displacementitself1(w, wflag, wshadow, wtime)         return wflag end --returnflag local function randomposition1()     sidex1 = mathr(-10,400)     sidey1 = mathr(-10,400)        print ("randomposition1", sidex1, sidey1)     return sidex1, sidey1 end --randomposition local function displacement1(w, wflag, wshadow, wtime)     if wflag == 4 then     print("displacement1")     w.transition = transition.to(w, {tag="displacement1", time=wtime,x=sidex1,alpha=1, y=sidey1, yScale=.35, xScale =.35, onComplete=returnflag1})     w.transitionshadow = transition.to(wshadow, {tag="displacement1", time=wtime,x=sidex1-20,alpha=0.5, y=sidey1-5, yScale=1, xScale =1})     end --if end -- displacement randomposition1() displacement1(w, wflag, wshadow, wtime) return wflag end return wflag end --displacementitself displacementitself1(param1, param2, param3, param4)