Is there a shorter way to write this?

Is there a shorter way to write this?

[lua]

if event.target== plaatje1 or event.target== plaatje2 or event.target== plaatje3 or event.target== plaatje4 or event.target== plaatje5 then

[/lua]

So I don’t have that many “or”. Just seeing if I can make the code a bit more concise.

Thanks

If you define a object type variable…  you can add your own properties to your image types.

[lua]

local objectType = event.target.isType

if objectType == “sometype” then

[/lua]

based on your example of 5 jets, if you assign an id to each object, you can make the line this short :

if e.target.id < 6 then

(see code example below:)

&nbsp; &nbsp; local jets = {} &nbsp; local function onTouch(e) &nbsp;&nbsp;&nbsp;&nbsp;if e.target.id \< 6 then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.y = 200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp; &nbsp; for i = 1, 10 do &nbsp;&nbsp;&nbsp;&nbsp;jets[i] = display.newImage("debris.png", (i \* 40), 100) &nbsp;&nbsp;&nbsp;&nbsp;-- assign each jet a unique id &nbsp;&nbsp;&nbsp;&nbsp;jets[i].id = i &nbsp;&nbsp;&nbsp;&nbsp;jets[i]:addEventListener("touch", onTouch) end &nbsp;

If you define a object type variable…  you can add your own properties to your image types.

[lua]

local objectType = event.target.isType

if objectType == “sometype” then

[/lua]

based on your example of 5 jets, if you assign an id to each object, you can make the line this short :

if e.target.id < 6 then

(see code example below:)

&nbsp; &nbsp; local jets = {} &nbsp; local function onTouch(e) &nbsp;&nbsp;&nbsp;&nbsp;if e.target.id \< 6 then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.y = 200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp; &nbsp; for i = 1, 10 do &nbsp;&nbsp;&nbsp;&nbsp;jets[i] = display.newImage("debris.png", (i \* 40), 100) &nbsp;&nbsp;&nbsp;&nbsp;-- assign each jet a unique id &nbsp;&nbsp;&nbsp;&nbsp;jets[i].id = i &nbsp;&nbsp;&nbsp;&nbsp;jets[i]:addEventListener("touch", onTouch) end &nbsp;

i have created this function in my utils file:

-- used when searching a table as a list function Utils.propertyIn( list, property ) for i = 1, #list do if list[i] == property then return true end end return false end

and it is used in code like this:

if Utils.propertyIn( { plaatje1, plaatje2, plaatje3, plaatje4, plaatje5 }, event.target ) then ... end

though i more commonly use it when i have a dynamic list as opposed to hard-coded values.

myTable = { plaatje1, plaatje2, plaatje3, plaatje4, plaatje5 } if Utils.propertyIn( myTable, event.target ) then ... end

cheers,
dmc

Thank you all for the replies :slight_smile:

Hmm wanted to have cyberparkstudios also as solved.

The extra bit of info on the .id was very helpfull on a different bit of code I used.

Using .someName a lot now in different variations.

i have created this function in my utils file:

-- used when searching a table as a list function Utils.propertyIn( list, property ) for i = 1, #list do if list[i] == property then return true end end return false end

and it is used in code like this:

if Utils.propertyIn( { plaatje1, plaatje2, plaatje3, plaatje4, plaatje5 }, event.target ) then ... end

though i more commonly use it when i have a dynamic list as opposed to hard-coded values.

myTable = { plaatje1, plaatje2, plaatje3, plaatje4, plaatje5 } if Utils.propertyIn( myTable, event.target ) then ... end

cheers,
dmc

Thank you all for the replies :slight_smile:

Hmm wanted to have cyberparkstudios also as solved.

The extra bit of info on the .id was very helpfull on a different bit of code I used.

Using .someName a lot now in different variations.