Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to add collision event listener to a text table (values)
Started by patriciaflink Feb 08 2019 04:58 AM

9 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

patriciaflink

[GLOBAL: userInfoPane.html]
patriciaflink
  • Observer

  • 13 posts
  • Corona SDK

Hello everyone,

 

is there any way to add an event listener (collision) to a table?

 

Here my code so far: 

 

--table 2


local wordTable2 = {"Air","Breath" }
local word = {}

for i,v in ipairs (wordTable2) do
word[i] = display.newText (wordGroup, v, 250, 200, robotoFont, 25)
word[i]:setFillColor( 1, 0, 0 )
physics.addBody(word[i], "dynamic",{density=1, friction =.3})
physics.setGravity( 1, 10 )
wordTable2.name = "word2"
word[i].isVisible = false
print( k,v)
end

word[currentWord].isVisible = true


local function wordCollision ( self,event)
  if event.phase == "began" then
            for i,v in ipairs (#wordTable2) do
            if (self == wordTable2[i] and other == player) then
                display.remove (wordTable2[i])
                wordTable2[i] = nil
                score = score +1
                updateText()
            end
         end
     end
end

wordTable2.collision = wordCollision
wordTable2:addEventListener("collision", wordTable2)

the attempt to call event listener is a nil value

 

any help is appreciated,

 

tks in advanced

 

Patricia

 



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 524 posts
  • Corona SDK

It's not possible. A table is just an array of data. You can only add collision listeners to physics objects.

What is it that you are trying to accomplish?



[TOPIC: post.html]
#3

patriciaflink

[GLOBAL: userInfoPane.html]
patriciaflink
  • Observer

  • 13 posts
  • Corona SDK

tks for the quick answer!

 

well, the values in that array are physical (even if just text). I managed collision with text outside an array. I just can't manage collision within the value of an array. 

 

If "text" with physical attributes drops down the screen the player can catch it (collision). but if it is table {text, text, text} with each value having the same physical attributes, it gives me an error.

 

So, where do I add the listener to?



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 524 posts
  • Corona SDK

Ah, I see! :D

You are going about this a bit more difficult than it needs to be.
 

local wordCollision -- adding a forward declaration for the function here
local wordTable2 = {"Air","Breath" }
local word = {}

-- no need to use ipairs
for i = 1, #wordTable2 do
	word[i] = display.newText (wordGroup, wordTable2[i], 250, 200, robotoFont, 25)
	word[i]:setFillColor( 1, 0, 0 )
	physics.addBody(word[i], "dynamic",{density=1, friction =.3})
	physics.setGravity( 1, 10 )
	wordTable2.name = "word2" -- what's the purpose of this line? It doesn't really do anything
	word[i].isVisible = false
	-- add the collision listeners to each individual word
	word[i].collision = wordCollision
	word[i]:addEventListener("collision", word[i])
end

Now, since you will be creating the objects before the functions, you need to forward declare the function. This means that when you later write the contents of the function, you need to skip the local part in front of it.

Still, the fact is that you cannot add a collision listener to a text table. Only physical objects can collide. In other words, you can use the contents of a table in creating words and give the text objects bodies which will collide, but you cannot make an array collide. These text objects can, of course, be in a table, but then you have a table of display objects, not a table of strings.


  • patriciaflink likes this

[TOPIC: post.html]
#5

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 176 posts
  • Corona SDK

local function wordCollision ( self,event)
  if event.phase == "began" then
            for i,v in ipairs (wordTable2) do <--not with #
            if (self == wordTable2[i] and event.other == player) then <--don't forgot event.other ; player = ?
                display.remove (wordTable2[i])
                wordTable2[i] = nil
                score = score +1
                updateText()
            end
         end
     end
end

wordTable2.collision = wordCollision
wordTable2:addEventListener("collision", wordTable2)

little correction in the ipairs fonction argument & event.other.


  • patriciaflink likes this

[TOPIC: post.html]
#6

patriciaflink

[GLOBAL: userInfoPane.html]
patriciaflink
  • Observer

  • 13 posts
  • Corona SDK

thank you both for your replies!

 

sadly, it is still not working....

 

this is the code now:

local wordCollision
local wordTable2 = {"Air","Breath" }
local word = {}


for i = 1, #wordTable2 do
word[i] = display.newText (wordGroup, wordTable2[i], 250, 200, robotoFont, 25)
word[i]:setFillColor( 1, 0, 0 )
physics.addBody(word[i], "dynamic",{density=1, friction =.3})
physics.setGravity( 1, 10 )
word[i].isVisible = false
-- add the collision listeners to each individual word
word[i].collision = wordCollision
word[i]:addEventListener("collision", word[i])
end


word[currentWord].isVisible = true


local function wordCollision ( self, event)
if event.phase == "began" then
            for i = 1, #wordTable2 do 
                display.remove (word[i])
                word[i] = nil
                score = score +1
                updateText()
            end
end
     end

@ivandotet: I tried that before, but it wouldn't add the event listener :(

 



[TOPIC: post.html]
#7

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 524 posts
  • Corona SDK

I told you to skip the local from in front of the function when you later define it. :P

Just write:
 

function wordCollision ( self, event)

The forward declaration is like you tell your code that "this thing will mean something later on, trust me" and so your game won't crash when you add the listeners that refer to it. This means that when you later define the "function wordCollision", then the code understands that "Alright, this is what she meant!", but if you instead write "local function wordCollision", then you rewrite wordCollision instead of defining it and so the collision listener won't have anything to do.

Also, if you have a "for i = 1, #wordTable2 do" loop inside your collision function, then any collision will wipe out all of the entries in table word.


  • patriciaflink likes this

[TOPIC: post.html]
#8

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,296 posts
  • Enterprise

as per Xedur@Spyric, you just have a scope issue.  either follow his directions to "forward declare / backward define", OR simply move the entire declare/define of the wordCollision() function as currently written to BEFORE the loop where it is referenced and assigned to your text objects.


  • patriciaflink likes this

[TOPIC: post.html]
#9

patriciaflink

[GLOBAL: userInfoPane.html]
patriciaflink
  • Observer

  • 13 posts
  • Corona SDK

knowing how to read is an advantage - which I obviously lack of :(  sorry about that one.

 

 

thank you so much again for your help and patience, I will keep on trying.



[TOPIC: post.html]
#10

patriciaflink

[GLOBAL: userInfoPane.html]
patriciaflink
  • Observer

  • 13 posts
  • Corona SDK

@Davebollinger thank you for your answer.

 

I am new to lua/ corona sdk or programming perse, so I am still learning and trying to get through a logic which is by naturestrange to me.

 

I can imagine that for you guys answering silly newbie questions again and again is boring and nerve racking. Hence why I only post questions, if I can't find anything about on the forums or the net. 

 

I also appreciate any answer  given (and the time implied) tremendously and am very sorry if at times it takes more than one answer from you guys for me to grasp the issue at hand.

 

cheers

Patricia




[topic_controls]
[/topic_controls]