Just doesn't begin to talk about touch hasn't moved function and what you could do with it.
I'm going to show you something very simple and then I'm going to try to make things a little bit more
advanced by showing you more details of what you would actually want to do when you use methods such
as this one.
So let's go ahead and do x4.
I'm going to start a new application touches more at the I'd just like the last time I'm going to have
something in my screen which is going to be another you I view.
These files are my shit rushing around madly explaining what I'm doing simply because I see by the time
you got to this portion of the course you know exactly what's happening.
So this one is the red box and that's about this quote world you controller and I don't need to view
it.
Look So here's what I wanted.
I want to say touches move with.
And that Tieri are for they want to do is this.
Let me write it down.
Place the red box where my torch is.
Now this may seem quite simple at the beginning but once you actually get in with details of it you
will not start so many things that you have to account for.
One of them is how many touches are there.
So you have to make sure that you do this only if there is only one touch on the device and then the
more important thing is your touch lands under that box.
But you want to find the position based on the overall view.
So much for the red box but the location for the main view.
That's the next thing we have to do Man view.
Finally and this is more of an advanced model.
Becomes a model of the offset against the touch.
And I would explain this in a socket.
Let's go ahead and do the easier ones first.
First thing you want to know how many touches are there so that one should be easy if touches count
is one.
Only two things in this only situation.
So if there are two touches I don't want to move anything maybe I want to rotate or a scale or do something
else but I only want to do this particular thing moving the red box.
If there's only one touch if that's the case then let my touch just like the last time become touches
that first let's get the very first touch.
And you know that's the only touch right now.
And then we have to check if the touch is for the red box.
So if my touch door view is a red box so now becomes about this one knowing the location not inside
the red box or rather inside the main view.
So I'm going to say to let's call it look in.
In view and that's the C-g point of course because that's a location that's going to become my touch
dart location in view and the view I'm interested in is self got to view and go ahead and say this until
you actually does exist because I know I have to touch some.
But on the view I'm going to make it explicitly onda.
So let's see what we have done.
We said if there is one touch and that touches on the red line find the location of that touch on the
actual view.
Because as you can imagine when you land the tap on the device you're touching both on the red box as
well as the main view and if there are more objects than you touch with so many different objects.
But for now you are only interested in in this one.
The one that is for the self that view.
And then the next thing is actually very simple.
I'm going to say Redbox thought.
Make it look in your so let's go ahead and see how this works out.
Taking out the first for some of them I only need this one and there on my app on while it is running
and when I do a little bit of a clean up you know I'm a stickler for that.
OK.
But that is simulator Yes.
So let's see what they can do.
I can grab this guy move him better I want to perfect.
It's working exactly the baby Y.
And you know this is a very good way of doing interaction if you want to make a little puzzle game or
anything of the sort.
This would really help you.
Now even though this is really great.
Let me show you where the system breaks if I talk somebody on the sensor.
You don't really notice it but if I tap on one of the corners you will see that the box immediately
jumps in there.
And that is because I'm actually going to give you a second to think to yourself why is it that I tap
anywhere on the red box the center of it jumps to where my mouse pointer is.
Or if you were on an actual device your touch is.
So here's the problem.
We are setting the center after box to that location.
So we are not accounting for the offset I might have when I first touched the red box.
So at the beginning of touching the red box maybe I was in this corner.
So that corner should be going.
I am going.
Not the center of the red box.
So that becomes a little bit of a mathematical problem rather than a programming problem.
But in solving the mathematical problem we have to write code.
So I'm explain to you how I intend to solve it.
When I top some doubt tap against the center of the red one has a little offset.
So imagine dislocation in my mouse pointer is in the center of the red box and I tap this little much
that vet and the height is the offset against the center.
So I have to find that offset and apply that any time I move.
The problem is that I find that offset that offset is going to be moving with my mouse.
So as a result of that I have to find the offset at the beginning of the touch.
Not in this method in a method called Touch has begun in that method.
I have to find how much offset I have and applied that in that I just move.
And that itself will introduce new problems simply because you can not add things such as C.G. points
to each other.
But it will solve one step at a time.
Let's go ahead and do the very very first thing I'm going to go ahead in here and say I have a variable
called miscalled the tap offsets and that's going to be a C.G. point.
So let's say somehow magically we calculate this knowledge and we know what's the value for the top
of set and then when we come here we want to actually move or object we say well find a location in
view and always at the top offset to it.
So for now this is obviously going to crash the application because the top offset is not really working.
And also is going to tell us you cannot do binary between two values of the type C.G. point.
I mean with the exact error for you it says binary operator plus cannot be applied to operators of type
C.G. point C.G. point.
So we have two issues that we have to address one is we have to find out what is this value type of
set.
At the very beginning and then we have to find a way to add them together.
So I suggest we first solve the easier problem easier problem is this.
What is the top of.
At the very beginning.
So for that I'm going to have a method called Touch has begun.
This is right at the very beginning when a touch lands and I have to almost for the same thing I have
to check if that touches count is 1.
Because that's the only situation where things are going to happen in the touches move.
So because of that when I actually count for that in here knows that I'm going to say if that happened
and I was valid then let's do these two things because it's exactly the same thing.
Didn't get the first touch and then if that touch is on the red box here is where we have to actually
calculate the value of the offset.
And let's see how I'm going to do that.
The first thing I'm going to do that I'm going to get the location of my Touch not on the actually view
right there get it under red box.
So I'm going to say Let's call this one that was called look in view so let's call this one let's look
in red for instance.
And that's again going to be is C.G. point.
We know that and that's going to be exactly the same thing.
So essentially my touch but location interview red box and I know that this thing exists so it is on
draft's forcibly because I know it is there now to find the offset this value we actually have to find
the difference between dislocation and the center of the red.
So if we go in here and I tap in that corner I have to find the distance between this location and the
center.
The problem is the center of red which is the red box that's there that is not what I want because red
box that center shows the center of red against the view.
While I want to know what is the middle of the right.
So it's going to do something that is actually the middle of the road.
I'm going to go ahead and say let's call it red.
The middle let's go to the values Louds or let's decode that middle.
I'm just trying to make sure that the names are kind of explanatory.
I'm going to say read the middle is the point of I want to have two values.
Both of them float the CGA point values of red box dot frame size dot vent divided by two and the same
thing with the height and how it works is I'm going to say get the red box.
Escalate it to a half on the beat the height.
Those are the values for the middle of it.
So for instance this box is 200 pixels by 100 pixels.
Then the center of it is out the 100 pictures and 50 pixels.
And that's what we want to know.
Now that I have that I'm going to go ahead and say well let's let's call it our difference of x or x
difference x difference is going to be red middle dot X and then I'm going to subtract the location
in red X from it.
So that's the difference between the axis I'm going to copy it out say why differences are obvious differences
between the two wise.
So so far we have managed to do this.
We have managed to find this center here and that corner that we touch of it is here or here or anywhere
and we found the difference between the x and between the lines.
So we got these two.
And now that they have that then it becomes very easy.
Hypothesis becomes a C.G. point value of these two guys x difference and y difference.
So here is what happened in the patches began.
We found a very have it we found very in the middle of it we found that this sounds two doors that difference
between the X's and the difference between the wise and even made in UCG point that represents depth
of sets.
And that happened right at the beginning of the torch.
So every time we wanted to just move that you is already a sword.
Is restored only if the count is one and the view is right which is the only situation that touches
most of the value is 1 and the view is the level now with a much bigger problem.
We want to move the center of the red to be a combination of the location in the view and the top offset
but we can not add them together let me show you a little or not a very good approach but it's going
to help us understand what we should do and then I'm going to do it the right way.
I'm going to say let's have it let us call it Kumba compound combined that is going to be a CGI point
of.
So the tool values for the x and y of these C.G. point is going to be location and view that X plus
cap offset that X and then location and you dot y plus top offset that Y and I'm going to say the red
box center is this right.
So don't worry about that that I commented out there.
It's simply we are making a point by adding the X's.
Otherwise of very very overview are moving in the view in the touches of wherever we are moving as well
as the little offsets that we have to apply.
Let's see if this works.
And then I'm going to come back and do it in a bed of the Essentially that is the simulator.
So now in the sense that works in a corner it also works.
You see when I grab a corner it actually follows me exactly because it's accounting for that little
offset that I had at the very beginning.
So we know this is working.
Everything we have written is correct.
That type of set is calculated properly and the touch has begun and the touches moves up buying it properly.
So I don't need that anymore.
But here's the part we haven't solved we have manually added the Xs and Ys of two vectors and there
is really nothing wrong with that.
But if you want to really push your work to kind of the next level I would suggest that we do something
like this.
And this is something that we call a function overload.
So I mean this function overload and function overloads are things that you have done in the past but
not just in not in this particular format.
Imagine if you had a function of Maime of our own which was called functions.
Hello.
And this was a situation with.
And we had this function and for whatever reason I needed to have this function to take two different
types of arguments.
So one of them was which was an integer.
And then I needed another function which is hello and this one is an F and that is a float.
So these two are essentially called function overloads.
They are forced the same function are called the same thing but since they get different arguments or
different parameters they become overloads of the same function.
And here's what we want to do.
We want to add in your function overload for plus that is going to help us not just add binary integers
or binary float values but also be able to C.G. points or anything.
Let's say you want to write a plus function overload that helps you add two vectors such as this one
or two CGI rechts that they're going to do so many different things together.
So here's what we want to do.
I'm going to go down here.
In my functional overload I'm going to go ahead and say function and the function is plus because something
that exists is an overload of plus that's already there.
And then the arguments of it I'm going to say Let's call this one of the first vector which is a CGU
point and let's call the second one second vector which is a C G point and it also gives us like a C
G point.
And here's the body of the function in the body of the function I'm going to say return C.G. point of.
I should have let him autocomplete float of the same thing we did here first Vache back x plus.
So I can vector Da'ath X and then do the same thing first y plus second after that y.
So what they have done is simply saying there is an overload plus that also takes seems C.G. points
up until this point they used to take integers floats potentially as strings but now they also do take
vectors into them C.G. points.
So instead of doing this anymore.
Acworth simply go ahead into my touches move and say the value that I want to have for my center is
location view and using this very new function that you have introduced Plus the offset and that's about
that.
And now everything should be working using attaches more than touches began on our new function overload
and then it's working exactly the way we want it.
But of course write a new function overlock if you want to subtract them I'm gonna write that because
that's the easiest one I can think of right now.
You just subtract the two of them that's about.
And if you want we could depending on what you're doing you might want to write tens of different function
overload because you're trying to do things this kind of thing really happens when you work with coding
for games particularly in two dimensional games.
I have and the writing so many different overloads for different matters because I didn't want to rewrite
that long piece of code every time I wanted to add to C.G. point so every time I wanted to calculate
the rectangle of a view or so on and so forth.
So that's about that.
We got the touches moved using a set as well as using a touch has moved itself based on a number of
the count and it works the way we wanted it to work.
Let's keep it up to here on we want to our next lesson.