android


how do I check if a circle shape is touched to apply actions to the shape e.g. change color of the shape or to update the score?


So I am new to android games development and I have 4 CircleMapObjects which are yellow coloured I am using an InputProcessor to manage touch/click events. Now in the touchDown() method what I want is when a Circle is touched/clicked in the game (anywhere inside the circle ) then it should change to say the colour green. I am using the libgdx framework and am new to the framework, I have come across Actor however I've found that it draws rectangular shapes from looking at the API.
The InputHandler class:
public class InputHandler implements InputProcessor {
private Spot spot;
public InputHandler(Spot spot){
this.spot = spot;
}
#Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
float distanceX = (float) Math.sqrt(Math.pow((spot.getSpots()[0].getCircle().x - (screenX)), 2));
float distanceY = (float) Math.sqrt(Math.pow((spot.getSpots()[0].getCircle().y - (screenY)), 2));
//Below is for testing purposes not to do with touching a circle but touching a particular
//region on the screen
//float distance = distanceX + distanceY;
// System.out.println(distanceX);
// System.out.println(distanceY);
// System.out.println(spot.getSpots()[0].isVisible() + "" + "Color: " + spot.getSpots()[0].getColor());
System.out.println("CLICKED: " + " x: " + screenX + " y: " + screenY);
if (screenX > 45 && screenY < 431)
{
spot.getSpots()[0].setColor(Color.RED);
System.out.println("This works!");
}
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
}
The Spot class:
public class Spot {
private float x;
private float y;
private float radius;
//Use CircleMapObject instead?
private CircleMapObject spot_1, spot_2, spot_3, spot_4;
private CircleMapObject spotList[];
private float elapsedTime;
private int index;
public Spot(float x, float y, float radius){
this.x = x;
this.y = y;
this.radius = radius;
//each spot initialised
spot_1 = new CircleMapObject(x,y,radius);
spot_2 = new CircleMapObject(x, y-228,radius);
spot_3 = new CircleMapObject(x+440,y,radius);
spot_4 = new CircleMapObject(x+440,y-228,radius);
//spot list initialised
spotList = new CircleMapObject[4];
//adding the spots to the spotlist
spotList[0] = spot_1;
spotList[1] = spot_2;
spotList[2] = spot_3;
spotList[3] = spot_4;
elapsedTime = 0.0f;
index = 0;
}
public void update(float delta){
}
}
The Gamescreen where the inputHandler is set:
public class GameScreen implements Screen {
private GameWorld world;
private GameRenderer render;
private Spot spot;
public GameScreen(){
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
System.out.println("Screen width: " + screenWidth + ", " + "Screen height: " + screenHeight);
float gameWidth = 136;
float gameHeight = screenHeight / (screenWidth / gameWidth);
world = new GameWorld();
render = new GameRenderer(world);
Gdx.input.setInputProcessor(new InputHandler(world.getSpot()));
}
... (there's more code in the GameScreen class but this is the only reference of the InputHandler used in this class. This also applies to the rest of the code.)
To simplify this you only need to check if the distance of the circle vs touch location is less then the radius of the circle to detect a hit. Simple vector math does the trick, we calculate the distance horizontally and vertically and with Pythagoras (A² + B² = C²) we can calculate the final distance.
distance = √(circleX - (touchX))² + ((circleY) - touchY)²
if (Math.abs(distance) < circleRadius)
{
//Touch within circle...
}
Libgdx has some vector functions for calculating the distance.
if (touchVector.dst(circleOrigin) < circleRadius)
{
//Touch within circle...
}
When i am talking about touchVector you need to use Vector2 and put your coordinates in. When working with positions, directions, velocities, etc you should be using the Vector2 class or Vector3 for 3D environments.
Vector2 touchVector = new Vector2(x,y);
float distance = touchVector.dst(new Vector2(circleOriginX, circleOriginY));

Related Links

Android Camer2 Recommended Thread Model?
Android Studio: Import module window is too big, can't get any further?
How to clear focus of EditText under Fragment onResume() after manual input something in EditText
Show CollapsingToolbarLayout Title ONLY when collapsed doesn't work properly
Android studio doesn't recognize USB devices in ubuntu
How to get data in AutoCompleteTextView using mySQL and Json?
How to use colored strings in a listview?
Call Fragment method from activity using FragmentPagerAdapter
SSL connection reusing and caching with Android OkHttpClient
Android programming: ListActivity “suppresses” ActionBar
How to add a custom list view to AlertDialog in android?
Bottom Sheet below LinearLayout in Android
In sqlite database I insert the image and retrieve from it . But my image is not displaying in the list
Create custom Progressbar with color change at rotate in Android
Get media current position before disconnecting from cast
Which version of android gradle plugin i can use with gradle 2.14?

Categories

HOME
shell
url-redirection
search
microsoftgraph
fuelux
battery
package
access
dojo
crystal-reports-2013
mql5
modbus-tcp
dreamweaver
sigma.js
rubymotion
ksh
babel
named-entity-recognition
jquery-callback
google-cloud-print
angular-translate
mql
sqsh
paragraph
left-join
cart
clickjacking
password-protection
ods
gdata
hackintosh
ogc
portforwarding
install.packages
photos
openal
openrasta
jquery-ui-draggable
cnc
fclose
identification
tf
carrot2
context-switch
seq
java-websocket
pybrain
offline.js
mathcad
mta
pre-build-event
project-organization
fuzzywuzzy
findfirst
maybe
atan2
sapi
jdi
extjs-grid
sony-lifelog-api
omnifaces
zxspectrum
procedural-programming
iiop
imagefilter
with-statement
lambda-architecture
android-framework
authlogic
android-audiorecord
cmmi
traceability
video-codecs
cascadingdropdown
preload
uimanageddocument
adobe-media-server
usertype
fitbounds
automationelement
exponentiation
tmx
message-passing
mathematical-notation
fault-tolerance
int64

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App