Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Question about addEventListener
Started by yvandotet May 14 2017 10:36 AM

- - - - -
16 replies to this topic

Best Answer Quantumwave , 14 May 2017 - 04:02 PM

I imagine its begin like that :

local function addEventListener(self,eventName,listener)
  ... the code inside is what I want
end

 

If all you're interested in is how addEventListener works in general, here's one implementation:

https://github.com/daveyang/EventDispatcher/blob/master/EventDispatcher.lua

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

Hello,

One question : (the other is solved lol thx)

I so loved to have the inner code of the function "addEventListener".

Is it possible to have an history link about this function ?

 

I think it's firstly defined on stage.currentStage._class table and when we create a display object, an index table is automatically given to the metatable's object with a key value look's like "index.addEventListener=function(self,event)... "

 

Is it correct ? A clue pleased lol .

 

Thanks.



[TOPIC: post.html]
#2

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

1.  when using a table as a listener, the name of the method must exactly match that of the event, "tab" ~= "tap".  (when using a function as a listener, you may use any legal name, or even anonymous) and the "expectation" is that you'd be using the same object, fe:

circle.tap = function(self,event) print(1) end
circle:addEventListener("tap")

2.  i can't follow your use of "history link" here, so no idea what you're asking, but...  all display objects descend from EventDispatcher which provides the add/removeEventListener methods.  if the passed listener is a table it attempts to dispatch as object["eventName"](object, event) (there are at least three syntactically equivalent ways of writing that call) but none of that really matters, because all that actual dispatching happens internally, out of your control.



[TOPIC: post.html]
#3

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

"tab" ~= "tap"

Just corrected lol thanks.

 

I ask in fact of having the internal description code of the addEventListener function.



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Why are you using a proxy to do the work?
 
Just put the listener on the object and it will self-clean when you delete the object.
 
 
Here is a basic random example showing some good practices.
-- One function, used by multiple objects
local function onTap( self, event )
   print( self.name )
   display.remove( self ) -- safer than calling obj:removeSelf()
end


local circle = display.newCircle( 0, 100, 30 )
circle.name = "Bill" -- Store any values you want as fields on the object.
circle.tap = onTap -- Assign function reference to field with same name as event, then...
circle:addEventListener( "tap" ) -- No need to pass obj, it is implied.


-- Make another 'circle'...
local circle = display.newCircle( 100, 100, 30 )
circle.name = "Bob"
circle.tap = tap
circle:addEventListener( "tap" )


-- And another...
local circle = display.newCircle( 200, 100, 30 )
circle.name = "Sue"
circle.tap = tap
circle:addEventListener( "tap" )




Edited by roaminggamer, 14 May 2017 - 11:59 AM.


[TOPIC: post.html]
#5

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

ok for that answer.

Why do you tell proxy ? I don't understand.

 

Do you have the internal description code of the addEventListener function ?

 

Thanks



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Why did you change original post to a new question?

 

Never never never .... change your original post to ask a new question.

 

I simply won't help you in the future if you do this again.  The reasons for having a forum are:

 

1. Get help when you are stuck.

 

2. Keep a record of the conversation so others with similar questions can learn from the conversation in the future.

 

 

If you want to continue the discussion, add the new details at the end of the post, as long as it is closely related to the original question.

 

However, starting a whole new question at the end of a post bad practice.  This is not an email thread.  

 

Generally the best practice is ONE question per thread.  i.e. Make new posts for new questions.  This is most useful for future readers.  If you're extra awesome you can go back and link the threads to each other for future readers.

 

(Update: This may seem like a total freakout, but I put real effort into answering forums questions and It is a super-pet-peeve of mine to see that effort wasted.)

 

 

Your original text was:

 

Hello,

 

2 things :

 

First the "table listener" don't work with a "tap" detection.

local t={}
t.tab=function() print(1) end

local cercle=display.newCircle(0,0,30)
cercle:addEventListener("tap",t)
-- Don't work !

Second, I so loved to have the inner code of the function "addEventListener".

Is it possible to have an history link about this function ?

 

I think it's firstly defined on stage.currentStage._class table and when we create a display object, an index table is automatically given to the metatable's object with a key value look's like "index.addEventListener=function(self,event)... "

 

 

Is it correct ? A clue pleased lol

 

Thanks.

 

 

 

 

Proxy
In your original question's code snippet, you had this:

local t={}
t.tab=function() print(1) end

local cercle=display.newCircle(0,0,30)
cercle:addEventListener("tap",t)

In this code, you are using the the table 't' as a proxy to catch event.  Or at least you were trying to.  This is a valid practice, but is is advanced and most people do not need to do it in their apps or games.

 

 

Do you have the internal description code of the addEventListener function ?

No idea what this means.  Please elaborate on 'internal description'


Edited by roaminggamer, 14 May 2017 - 12:44 PM.


[TOPIC: post.html]
#7

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

lol sorry for the changes, i think it was good like that.

 

For the second question, I speak about the full code behind the addEventListener function, which means :

local function addEventListener(self,event)

... the code inside, i hope to have this ...

end

Thanks :D



[TOPIC: post.html]
#8

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

I simple don't understand what you mean.

 

Maybe you should explain this by giving me a concrete example of what you hope to achieve.  

 

 

All I can say is.  In the example I've given:

  • self is a reference to the object that is being tapped (same as the object the listener is 'attached' to).
  • event is the standard tap event table.
  • In Corona apps, you can neither access the original script (it gets compiled to bytecode) during execution, nor can you modify it.
  • If you need a reference to the function itself (no idea what you would use this for), based on my code, you can find it at self.tap

Edited by roaminggamer, 14 May 2017 - 12:59 PM.


[TOPIC: post.html]
#9

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Edited and improved answer above.



[TOPIC: post.html]
#10

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

My question is to know if it's possible to have the code that create the addEventListener method.

 

For example, 

local cercle=display.newCircle(0,0,30)
cercle:addEventListener("touch",function(event) end)

In this example, the object "cercle" call the method "addEventListener" and then I write the event name "touch" and the listener function.

But what is the code executed inside that method ??

 

I imagine its begin like that :

local function addEventListener(self,eventName,listener)
  ... the code inside is what I want
end


[TOPIC: post.html]
#11

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

  • In Corona apps, you can neither access the original script (it gets compiled to bytecode) during execution, nor can you modify it.

I think this gives me the answer.

So it's impossible to have the original code build inside ?



[TOPIC: post.html]
#12

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Correct, It is not possible to access the plain text lua code.

 

You still haven't said WHY you want to do this.  

 

If you could elaborate on the why, I or others might have suggestions that help you achieve your true end goal.

 

Tip: Corona does (contrary to popular belief) support loadString, but you are NOT (according to the Apple guidelines) allowed to use it in iOS apps.


Edited by roaminggamer, 14 May 2017 - 01:38 PM.


[TOPIC: post.html]
#13

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,205 posts
  • Corona SDK

what a strange thread this is... I do not see the point in it at all!  Very confusing....

 

Forum etiquette totally thrown out of the window, kicked down the road and booted into the river!



[TOPIC: post.html]
#14

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

thread or threat?



[TOPIC: post.html]
#15

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,205 posts
  • Corona SDK

thread.... its late on Sunday



[TOPIC: post.html]
#16

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 132 posts
  • Corona SDK

  Best Answer

I imagine its begin like that :

local function addEventListener(self,eventName,listener)
  ... the code inside is what I want
end

 

If all you're interested in is how addEventListener works in general, here's one implementation:

https://github.com/daveyang/EventDispatcher/blob/master/EventDispatcher.lua



[TOPIC: post.html]
#17

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 195 posts
  • Corona SDK

You still haven't said WHY you want to do this.  

Just to know how it work and how them made it.

 

ok thanks qwidave that's what I wanted !




[topic_controls]
[/topic_controls]