Tip:
Highlight text to annotate it
X
Hello, I'm Eli for Edu4java and this tutorial number 7 of Android Game Programming
in this tutorial we are going to see "Touch Events
and Sprite Collision Detection
What am I talking about?
in the last tutorial we had managed to do all this; sprites moving in different speeds from one place to another
now, we want that when we click on one of them
it should disappear, for example I click
click,
click and they should disappear
how are we going to do this?
the first thing we are going to do is to override a method
a method of the...
View
in the GameView
SurfaceView has a method which we can override
...
this one
onTouchEvent
...
shift+ctrl+f to order
shift+ctrl+o for the imports
...
This event is going to be called each time we put our fingers on the screen
and
we are going to receive an event
which has
two properties; getX and getY which is going to give us the position of the mouse...
where the finger pressed the screen.In the emulator it is going to be a click
From now on I'm going to speak of onTouchEvent and clickEvent as the same thing
let's iterate on each of the sprites
and see for each of the sprites if there is a collision or not. A collision would be if the
coordinates x,y where I did click and the area occupied by the sprite. I am going to do this through a method called isCollition
which I have to develop now and in which I am going to send as parameters the coordinates x and y of where it touches
What I am going to do is sprites.remove
of the collection of sprites I am going to do a remove of the sprite which collides
This one is going to be removed from the list. The remove of the list is going to give me an error if I move on in the list in a normal way
due to the reorder of the list. What I can do is a iteration in an inverse way
I iterate from the last one
until the first one
I make a sprites.get(i)
this way I can remove without an error
here
this one can be replaced by
true
in the other way it also works. This return true in this event stops the evaluation of possible
listener for this event
It is more efficient if you write true because it is not going to evaluate anything else
we want this one to be the only manager of this event
here we can
ask him to make the method
in sprite. Here we should
return true if
x2 and y2 is inside the area.How are we going to do this?
in a short way
but a bit complicated
this way
let's see if I can explain it
so that there is a collision all this factors have to be true
if any of this conditions is not true, then the collision is going to be false
if the position x2
the click
is bigger than
the x position of the sprite
this one here. If it is bigger it means that it is on this side
if it is smaller
and if it is smaller than the x position of the sprite plus the width of the sprite, it means
that it is on this side. With these two "and" we are sure that it's on this side of the line
click here or on top
then
with these two question on x we know that it is in this
column. The same column were the sprite is. We should do the same for y
if know that
for example this one
here the x of the click
is bigger than the x, this would be the point...
of the sprite. We know it is bigger and we know it is smaller
than the x of the sprite plus the height
we know that it is in the row. It is as if it is here
If both conditions are true we know that it is inside the character
We consider the sprite as a square. If we click on the square it is going to be true
here it is
if it is bigger than x and smaller than x+width
and if y2 is bigger than y
let's try it
here you can see it disappears
there is a small trick that makes this a bit ugly still. I clicked and they both disappear at the same time
let's run it again
if you watch carefully and you click when both are together I kill both
...
here I have killed three...for this game this is not good. I want to kill only the one on top
to do this
to just kill the one on top I should add a
break
in the
in the onTouch
this for is going to iterate and
if there is more than one in the same position it will work. This break is going to avoid this
another detail
which we have to take into account
is this synchronized. This onTouchEvent is going to be called
in another different thread to the application. If you remember we opened a thread; GameLoop
it's the one which makes the update and the draw
we have this thread
which will be the one of the application which is going to managed the events. This means that when we call here it is going to be called in
another thread to the one which is drawing. This can give us error
because of a synchronization problem
with this synchronize
we avoid that we have collisions between threads
for the drawing.That's why I put getHolder, because it is the container of the drawing
...
as you see here
in GameLoop
we have another synchronize
for the same Object
see? I have the view.getHolder.What does this means?It can only execute this
or this code
in a synchronize way.
We can't execute both at the same time. What is happening is that we can't mix the drawing
with the modification of the data which are going to be printed
for example the remove is going to modify a sprite which is going to be printed.
The easiest way is this;It avoids this code to be run
at the same time as this other code.
With this
Let's see. Here we have two together
auch
here we have another problem
This event is executed very quickly a lot of times so even if I get out of the for, it goes into it again
quickly and it is in the same position. It is as if it tries with x and y several times
The way to avoid this is to put a delay
so that it doesn't execute in less than...
...instead of 500 I'm going to put
300 milliseconds. That should be ok
...
let's close this
let's create the field
field, create field
That's it.With this the first time we are going to see time when it came in and the second time we are only going to
let him in if the difference between the present time and the lastClick is more than
300 milliseconds. Less than a third part of a second
We are not going to notice and we are going to get rid of that sensation
ok, let's try it
here it is
let's look for two together. Here, I just killed one, and here I also killed just one. It works!
Ok
this is all for this tutorial
See you in the next one!!