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 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.
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:
I thank you much. And yes, I have misspelled it sorry ha. But, I do thank you.
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 what is meant by return true
? And what exactly is touch propagation?
“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:
Thank you
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.
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.
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
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
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(...)
But, what if I want to create one? The object method I mean. I’m not sure if I follow/
Reads this
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
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
Essentially, you can create a table and you can add all types of entries into tables. For instance,
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.
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.
Thank you!
Hello, again. SO I have a question regarding chapter six (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
You should start entirely new topics for entirely new questions.
if file then
checks if the file is not nil or not false.
Alright, gotcha.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.