# Help with event.phase

**URL:** <https://forums.solar2d.com/t/help-with-event-phase/354233>\
**Category:** Other\
**Created:** [November 24, 2021, 5:26pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233 "2021-11-24T17:26:09Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 24, 2021, 5:26pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/1 "2021-11-24T17:26:10Z")

</div>

HELLO EVERYONE! My name is Eye, and I (pun intended :p) am relatively new to the Solar2D framework (said framework because I wanted to look smart and cool do not roast me), I am currently on [https://docs.coronalabs.com/guide/programming/03/index.html#touch-events](https://docs.coronalabs.com/guide/programming/03/index.html#touch-events) At local ship = event.phase, we have already created a ship variable (the image). So are we overriding ship with this new variable? Sorry, my native is not english.

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [November 24, 2021, 5:30pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/2 "2021-11-24T17:30:43Z")

</div>

Hello and welcome!

You mentioned `local ship = event.phase`, but you probably meant `local ship = event.target`?

This is a matter of scope. Since that is inside a function, it will be local to said function. If and when you move outside of the function, that same `ship` variable will no longer exist.

Have a read:

> **[Programming in Lua : 4.2](https://www.lua.org/pil/4.2.html)**

> **[Solar2D Documentation — Tutorials | Basics](https://docs.coronalabs.com/tutorial/basics/scope/index.html)**

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 24, 2021, 8:58pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/3 "2021-11-24T20:58:45Z")

</div>

I thank you much. And yes, I have misspelled it sorry ha. But, I do thank you.

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 26, 2021, 12:49am UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/4 "2021-11-26T00:49:37Z")

</div>

Good night, sorry for bothering. But what does `return` mean in Solar2D terms? In [https://docs.coronalabs.com/guide/programming/03/index.html#touch-listener](https://docs.coronalabs.com/guide/programming/03/index.html#touch-listener) what is meant by `return true`? And what exactly is touch propagation?

---

<div class="post-metadata">

**Author:** ![colinmorgan](https://forums.solar2d.com/user_avatar/forums.solar2d.com/colinmorgan/32/241_2.png) [@colinmorgan](https://forums.solar2d.com/u/colinmorgan)\
**Post date:** [November 26, 2021, 8:16am UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/5 "2021-11-26T08:16:35Z")

</div>

“Propagation” can be roughly defined as “spread”, so touch propagation is when a single touch is spread to multiple objects. Without `return true`, if you have an object that can receive a touch event on top of a second object that can receive a touch event, a single touch will interact with both objects. However, if you have `return true` in the event function, that touch event will “stop” on the first object and not propagate (spread) to other objects that would otherwise be capable of receiving a touch event.

If you have a function that performs a calculation (or any number of other things), return is what determines what result is sent back to whatever called it.

Here’s a blog post from a few years back that attempts to address the issue, but it’s a big question:

> **[Functions and return statements in Corona](https://coronalabs.com/blog/2019/09/13/functions-and-return-statements-in-corona/)**
>
> Functions are one of the most useful tools in a programmer's tool box. But how do you make the best use of this tool? This tutorial can help you understand how functions work.

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 26, 2021, 12:44pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/6 "2021-11-26T12:44:43Z")

</div>

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 26, 2021, 7:31pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/7 "2021-11-26T19:31:15Z")

</div>

So, in this context (the touch propagation I mean), where does the `return true` value go? Is it dumped? And why is it declared outside of the if-elseif-else chain? How does `return true` prevent touch propagation, does it exit the function (this question is also related to the one i subsequently said because, why not exit the function in one of the conditional statements?)

Thanks in advance,  
Eye.

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [November 26, 2021, 9:54pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/8 "2021-11-26T21:54:44Z")

</div>

For touch/tap events, `return true` essentially just means that you stop the the on-going touch event from reaching other display objects that are further down in the display hierarchy.

The reason why you don’t (usually) have that within the conditional statements is because most of the time you want to stop the touch event from propagating regardless of the conditions.

When you touch somewhere on the screen, then Solar2D will go through the display objects that overlap that location until one of the associated functions returns true. If none of them return true, then the touch event goes through all of them.

There’s just a loop that checks for the true/false value when going through the objects.

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 27, 2021, 12:46am UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/9 "2021-11-27T00:46:57Z")

</div>

I see. So `return true` is for distinguishing touch events? And by hierarchy, do you mean other display objects that we want to apply that touch event to in the same, in this case, `dragShip` function? Could you please explain it a little bit more simpler? I’m a slow learner 😆

Thanks in advance,  
Eye

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 27, 2021, 1:09am UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/10 "2021-11-27T01:09:55Z")

</div>

Sorry, for bothering again, I know that this is not related to our discussion, but when can I know when to use the dot notation `player.x` and the… “object method”, `name:doName()`? I’m getting so confused!

Thanks in advance,  
Eye

---

<div class="post-metadata">

**Author:** ![e.sobole](https://forums.solar2d.com/user_avatar/forums.solar2d.com/e.sobole/32/2087_2.png) [@e.sobole](https://forums.solar2d.com/u/e.sobole)\
**Post date:** [November 27, 2021, 11:06am UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/11 "2021-11-27T11:06:58Z")

</div>

> [@Eye](#):
>
> when can I know when to use the dot notation `player.x` and the… “object method”, `name:doName()`? I’m getting so confused!

You said the reply, most of time,

If you want to acces a property, you use dot syntax  
If you want to acces an oject method, you use colon syntax

**So you must know what your are calling !**

But where it’s complicated, is for lua build-in Library where you are using dot for calling method !  
For example

```
display.newText(...)

```

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 27, 2021, 12:36pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/12 "2021-11-27T12:36:01Z")

</div>

But, what if I want to create one? The object method I mean. I’m not sure if I follow/

---

<div class="post-metadata">

**Author:** ![e.sobole](https://forums.solar2d.com/user_avatar/forums.solar2d.com/e.sobole/32/2087_2.png) [@e.sobole](https://forums.solar2d.com/u/e.sobole)\
**Post date:** [November 27, 2021, 12:48pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/13 "2021-11-27T12:48:10Z")

</div>

Reads this

> **[Programming in Lua : 16](https://www.lua.org/pil/16.html)**

That explain, you can create method with many way

```
function YourObject.YourMethod(self, ...)
...
end

```

or

```
function YourObject:YourMethod(...)
...
end

```

Both works and finally you can decide to use . ou : depending on your method declaration !  
I’m using : but finally using the first syntaxe can simplify OOP while you can use . for properties and methods 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [November 27, 2021, 5:00pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/14 "2021-11-27T17:00:56Z")

</div>

The confusion here might be about how tables and functions (including methods) actually work in Lua.

You could take a quick look at: [https://docs.coronalabs.com/guide/start/introLua/index.html#tables](https://docs.coronalabs.com/guide/start/introLua/index.html#tables)

Essentially, you can create a table and you can add all types of entries into tables. For instance,

```lua
local t = {}

t.number = 1
t.string = "abc"
t.subtable = {}

function t.foo()
    print( "hello!" )
end

-- These are all different types of table entries.

```

Like @e.sobole pointed out, methods are just a specific type of functions. The only difference between methods and normal functions is that with methods, you are always passing the table the function is attached to as the first parameter, i.e.

```lua
local t = {}

function t:foo()
    print( self, t )
end

t:foo()

```

When you call method `t:foo()`, it’ll print out the value of `self` and `t`, and you can see that they are both the same table. `self` is passed to the function even though there are no parameters listed.

With a regular function, you’d need to manually pass the table as an parameter to the function.

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [November 27, 2021, 5:09pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/15 "2021-11-27T17:09:44Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [December 1, 2021, 1:14pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/16 "2021-12-01T13:14:35Z")

</div>

Hello, again. SO I have a question regarding chapter six ([https://docs.coronalabs.com/guide/programming/06/index.html#loading-data](https://docs.coronalabs.com/guide/programming/06/index.html#loading-data)), is `if file then...` equivalent to `if file ~= nil then...`?

Thanks in advance,  
Eye

---

<div class="post-metadata">

**Author:** ![XeduR](https://forums.solar2d.com/user_avatar/forums.solar2d.com/xedur/32/256_2.png) [@XeduR](https://forums.solar2d.com/u/XeduR)\
**Post date:** [December 1, 2021, 3:00pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/17 "2021-12-01T15:00:22Z")

</div>

You should start entirely new topics for entirely new questions.

`if file then` checks if the file is not nil or not false.

---

<div class="post-metadata">

**Author:** ![Eye](https://forums.solar2d.com/user_avatar/forums.solar2d.com/eye/32/2159_2.png) [@Eye](https://forums.solar2d.com/u/Eye)\
**Post date:** [December 1, 2021, 5:05pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/18 "2021-12-01T17:05:38Z")

</div>

Alright, gotcha.

---

<div class="post-metadata">

**Author:** ![system](https://forums.solar2d.com/uploads/default/original/1X/1bf6a947166df24e92aaa3358558f4b82d7f3d5a.png) [@system](https://forums.solar2d.com/u/system)\
**Post date:** [May 30, 2022, 5:05pm UTC](https://forums.solar2d.com/t/help-with-event-phase/354233/19 "2022-05-30T17:05:42Z")

</div>

Closed on May 30, 2022, 5:05pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
