Applying one function to objects contained within an array

Apologies, I raised this question at the end of a ‘solved’ post, so if you’ve read this before, again, sorry.

I’m making an endless runner. My ‘floor’ is made up of a table of objects like so:

local tile = {} local groundGroup = display.newGroup() for j = 1,21 do tile[j] = display.newRect( display.contentCenterX, 0, 50, 50) tile[j].x = 20 + (j\*50) tile[j].y = 469 tile[j]:setFillColor(1, 1, 1) tile[j].strokeWidth = 1 tile[j].alpha = 1 tile[j]:setStrokeColor( 0, 0, 0 ) physics.addBody(tile[j], "static",{ density=2, friction=0.3, bounce=0.0 } ) tile[j].collision = bounceEventGround tile[j]:addEventListener( "collision", tile[j] ) groundGroup:insert(tile[j]) end

Thanks 

Alex@PaNc

When my ‘character’ hits one of the tiles, I’m applying an event, like this:

local function bounceEventGround(self, event) if event.phase == "began" then self:setFillColor( 0.8, 0.4, 0.3 ) self.apha = 0.5

As you can see, I’m just changing the colour and altering the transparency… What I can’t seem to do, is associate a variable with ‘self’ for further logic… For example, if the same ‘self’ is in another collision, I want to remove it based on it’s alpha value being 0.5… Just an example:

elseif event.phase == "began" and self.alpha == 0.5 then self:removeSelf()

This doesn’t work :frowning:

It would be nice if I could figure out how to associate variables with ‘tabled’ objects… Like:

self.hasBounced = 0 But this doesn’t work for me either.

I’ve been on this for a few hours, I’m struggling.

I was going to post on the previous thread.

I don’t use “self” so I’m not exactly sure how to use it or what it is capable of.

I add an ID to the spawned object so I can always reference it later.  You can add whatever variables to the object and reference them easily if you add the index ID to it.

Just add this to the object when you spawn into a table.

tile[j].ID = j  --now you’ve referenced the object with it’s table index and can always reference it by using it’s unique index

you could also add something like this and change it when collided with.

tile[j].Activated = false, change it to true or increment it to 1 depending on how many times it’s been hit or whatever.

reference the object with the event.target call and grab the index ID

local function bounceEventGround(event)

if event.phase == “began” then

local TempID = event.target.ID --this will reference the objects index
tile[TempID]:setFillColor( 0.8, 0.4, 0.3 )

tile[TempID].alpha = 0.5

end

Another advantage to using an index ID is that at some point you may associated something with the object, text for instance.

Let’s say your tile or button or whatever had text on it and when it is selected or hit you wanted to change the text on the button you would use the TempID and change it.

Spawn the text after you spawn the tile or button in the same function.

tile[j].TEXT = display.newText(“New”, blah,blah)

now when the object listener  triggers, change the tile/button and text.

if event.phase == “began” then

local TempID = event.target.ID --this will reference the objects index
tile[TempID]:setFillColor( 0.8, 0.4, 0.3 )

tile[TempID].alpha = 0.5

tile[TempID].TEXT.text = "Old "

end

This is how I do it, your mileage may vary.

Hope this helps,

Nail

I was going to post on the previous thread.

I don’t use “self” so I’m not exactly sure how to use it or what it is capable of.

I add an ID to the spawned object so I can always reference it later.  You can add whatever variables to the object and reference them easily if you add the index ID to it.

Just add this to the object when you spawn into a table.

tile[j].ID = j  --now you’ve referenced the object with it’s table index and can always reference it by using it’s unique index

you could also add something like this and change it when collided with.

tile[j].Activated = false, change it to true or increment it to 1 depending on how many times it’s been hit or whatever.

reference the object with the event.target call and grab the index ID

local function bounceEventGround(event)

if event.phase == “began” then

local TempID = event.target.ID --this will reference the objects index
tile[TempID]:setFillColor( 0.8, 0.4, 0.3 )

tile[TempID].alpha = 0.5

end

Another advantage to using an index ID is that at some point you may associated something with the object, text for instance.

Let’s say your tile or button or whatever had text on it and when it is selected or hit you wanted to change the text on the button you would use the TempID and change it.

Spawn the text after you spawn the tile or button in the same function.

tile[j].TEXT = display.newText(“New”, blah,blah)

now when the object listener  triggers, change the tile/button and text.

if event.phase == “began” then

local TempID = event.target.ID --this will reference the objects index
tile[TempID]:setFillColor( 0.8, 0.4, 0.3 )

tile[TempID].alpha = 0.5

tile[TempID].TEXT.text = "Old "

end

This is how I do it, your mileage may vary.

Hope this helps,

Nail