Directional buttons do not work in the "negative" direction - android

I am trying to make an image view move Up/Down/Left/Right using image buttons. I am able to get the image to move right and down, however up and left do not work.
I have tried changing the +/- sign, the length of the increments to move the image, and also the if/else statements to determine whether or not to move the image.
// Move ImageView Up
case R.id.upBtn: {
if (img.getY() > 0) {
img.setY(img.getY() - 25);
}
}
// Move ImageView Down
case R.id.downBtn: {
if (img.getY() < 950) {
img.setY(img.getY() + 25);
}
}
// Move ImageView Left
case R.id.leftBtn: {
if (img.getX() > 0) {
img.setX(img.getX() - 25);
}
}
// Move ImageView Right
case R.id.rightBtn: {
if (img.getX() < 2000) {
img.setX(img.getX() + 25);
}
}
The image view is supposed to move in all directions but it won't go in the up or left directions. I removed the if/else statements to see if they were the issue and they had no effect in regards to moving the image. They just keep it from going off the screen. It appears that I can not subtract from the getY() function but that does not make sense.

You are missing break statements, so once a case is matched, the code of all following case statements will also execute.
Try :
// Move ImageView Up
case R.id.upBtn: {
if (img.getY() > 0) {
img.setY(img.getY() - 25);
}
}
break;
// Move ImageView Down
case R.id.downBtn: {
if (img.getY() < 950) {
img.setY(img.getY() + 25);
}
}
break;
// Move ImageView Left
case R.id.leftBtn: {
if (img.getX() > 0) {
img.setX(img.getX() - 25);
}
}
break;
// Move ImageView Right
case R.id.rightBtn: {
if (img.getX() < 2000) {
img.setX(img.getX() + 25);
}
}
break;
default :

Related

Multi-touch bug on my first Android game

Right now I've been developing a game which will make the character move up or down if either swipe up or down on the left side of the screen. Then, he can also, shoot projectiles if I tap the right side of the screen. I've succeeded on making it work but the problem is that the swipe function do not works while I tap the the right screen. Both only works if I do one of them. I cannot move the character if I am firing projectiles. Any suggestions? Appreciate it in advanced.
Here's the code:
For the swipe movement function:
void Swipe() {
if (Input.touchCount > 0) {
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
lp = t.position;
fp = t.position;
text.text = fp.x.ToString();
}
else if (t.phase == TouchPhase.Ended)
{
lp = t.position;
//Swipe Up
if (fp.y < lp.y && fp.x < 400)
{
currentLane += 1;
}
//Swipe Down
else if (fp.y > lp.y && fp.x < 400)
{
currentLane -= 1;
}
}
}
}
And here's the code for the tap or firing projectiles function:
void FireBullets() {
interval += 2 * Time.deltaTime;
anim.SetBool("Attacking", firing);
if (Input.touchCount > 0 && interval > .75f) {
Vector3 bulletTouchPos;
Touch bulletT = Input.GetTouch(0);
bulletTouchPos = bulletT.position;
if (bulletT.phase == TouchPhase.Began) {
if (bulletTouchPos.x > 400)
{
interval = 0;
firing = true;
//Fire Bullets
Instantiate(bullets, new Vector3(transform.position.x + 1.2f, transform.position.y + .3f, transform.position.z), Quaternion.identity);
}
}
}else
{
firing = false;
}
}
Your lp and fp values don't care which finger is being checked.
If you want to detect multiple touch gestures at once, you need to discriminate your detection based on which finger that is.
You can do this by looking at the Touch.fingerId value, which is unique for each finger. Fingers are just an ID assigned to a given touching point as it moves across the screen in order to identify it from other touching points currently also on the screen.
You'll need to adjust your code to handle how to store the information you need in order to do what you need, but this should get you started.

Prevent Google Maps zooming when Double click or Pinch on Linear layout which is placed below the map fragment

how to stop map zooming when I pinch on linear layout which is placed bottom of our map fragment..please can any one help me..
This is my code to catch single and Double click events
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("on dispatchTouchEvent ", " dispatchTouchEvent ");
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
fingers = fingers + 1;
break;
case MotionEvent.ACTION_POINTER_UP:
fingers = fingers - 1;
break;
case MotionEvent.ACTION_UP:
fingers = 0;
break;
case MotionEvent.ACTION_DOWN:
if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
Log.w("on count=0", " count=0 ");
count = 0;
}
count++;
if (count == 1) {
Log.w("on count ", " count=1 ");
firstClick = System.currentTimeMillis();
} else if (count == 2) {
Log.w("on count ", " count=2 ");
lastClick = System.currentTimeMillis();
if (lastClick - firstClick < 300) {
CameraUpdate getzoom = CameraUpdateFactory.zoomIn();
mMap.animateCamera(getzoom, 400, null);
Log.w("on double click ", " event ");
}
}
fingers=1;
break;
}
if (fingers >1) {
Log.d("2fingersaction ", "2fingersaction ");
mMap.getUiSettings().setScrollGesturesEnabled(false);
// mMap.getUiSettings().setRotateGesturesEnabled(true); // disableScrolling()
}
else if (fingers < 1 )
{
Log.d("onefinger ", "onefinger ");
mMap.getUiSettings().setAllGesturesEnabled(false);
mMap.getUiSettings().setScrollGesturesEnabled(true);
// enableScrolling();
}
}
I placed this code out side of OnMapReady() Method ..
here is the screenshot of my app.. .
I had a similar requirement in my app and turns out the answer was pretty simple.
In your linear layout set the following property to true:
android:clickable="true"
Now what this does is makes your layout intercept the touch event and doesn't let it go to the map. Therefore it works for all gestures and touch events..
Hope this helps..
Cheers :D
``finally i got the Answer..
First of all we need to activate touch events only on mapfragment by getting Xpoint and Ypoint..
and check the condition of touch event in dispatchTouchEvent(MotionEvent ev), ,method...make sure that if we touch on mapfragment then only we need to activate Touch action or pinch action...
enter code here

Change Images or Bitmap when ever touched on screen

I want to change the bitmap that contains an image whenever user tap on the screen. i.e. the default image is shadow1, now what i want is that when user touched on screen then this image changed to shadow2, then again if user touched then shadow3, then on next touch the image again comes as shadow1 and it goes on and on and on. so basically there are three images and i want that when ever user touched on screen then image changes with each tap.
Following is the code that i tried but it is still not working as expected i.e. the image changes from shadow1 to shadow2 but then not changes to shadow3 or shadow1 even if i touched many times.
public void Touched(float x, float y)
{
boom = false;
try{
switch (bird.GetState()) {
case 0:
distance = 0;
bird.SetState(1);
flapped = true;
Bitmap workingBitmap = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow1);
bitmapBird = workingBitmap.copy(Bitmap.Config.ARGB_8888, false);
if (bitmapBird==workingBitmap)
{
}
riseCounter = 0;
pipeValues.clear();
//SoundManager.playSound(2, 1);
break;
case 1:
{
riseCounter = 0;
flapped = true;
t = 3;
Bitmap workingBitmappp = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow2);
bitmapBird = workingBitmappp.copy(Bitmap.Config.ARGB_8888, false);
//SoundManager.playSound(2, 1);
}
break;
case 2:
{
riseCounter = 0;
flapped = true;
t = 0;
}
break;
default:
Bitmap workingBitma = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow3);
bitmapBird = workingBitma.copy(Bitmap.Config.ARGB_8888, false);
break;
}
} catch(Exception e){}
}
I think there should be a for loop or while loop in 'case 1' and whenever user tap then image changes. Please help me with this.
You can simply use an int value to keep track of the image displayed as;
First Initialize a int at class level;
int num = 0;
then you can use it as;
if(num == 0){
loadFirstImage();
num++;
}
else if(num == 1)
{
loadSecondImage();
num++;
}
else if(num == 2){
loadThirdImage();
num = 0 ;
}
I think that you need to change the state of the bird in your second case statement.
First iteration will set the state to 1, from there, the only case statement you can hit is case 1: because you never change it.
So you need something like
case 1:
bird.SetState(2);
//....
Hope that helps

Android: How to mirror your touch? (visual pointer issue)

I'm having a problem with my pointer for my tablet. I'm designing an art app and I'm trying to figure out how to mirror my movement (symmetry). Currently, if you touch the screen with your finger or stylus, an image (programmatically generated circle) is drawn and updates itself to wherever you move your finger/stylus. This works perfectly. But now I want to add two new features: Horizontal and vertical symmetry. So if you choose horizontal symmetry, another pointer will appear at the opposite X position, but along the same Y axis. So if you touch at 300x, 250y, and another pointer will show at -300x, 250y on the screen along with the original pointer. Now when I tried to apply and test it, I still only get the original pointer. Here is what I have so far:
rLO.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// Hardware accelerated at runtime
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
cursor.x = (int)event.getX() - (cursor.radius / 2);
cursor.y = (int)event.getY() - (cursor.radius / 2);
cursor.onDraw(cursor.e);
rLO.addView(cursor);
if (hSym > 0) {
vSym = 0;
cursorH.x = -cursor.x;
cursorH.y = cursor.y;
cursorH.onDraw(cursorH.e);
rLO.addView(cursorH);
}
if (vSym > 0) {
hSym = 0;
cursorV.x = cursor.x;
cursorV.y = -cursor.y;
cursorV.onDraw(cursorV.e);
rLO.addView(cursorV);
}
break;
case MotionEvent.ACTION_MOVE:
// Hardware accelerated at runtime
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
//update pointer to new position of stylus/finger
cursor.x = (int)event.getRawX() - (cursor.radius / 2);
cursor.y = (int)event.getRawY() - (cursor.radius / 2);
rLO.removeView(cursor);
rLO.addView(cursor);
if (hSym > 0) {
cursorH.x = -cursor.x;
cursorH.y = cursor.y;
rLO.removeView(cursorH);
rLO.addView(cursorH);
}
if (vSym > 0) {
cursorV.x = cursor.x;
cursorV.y = -cursor.y;
rLO.removeView(cursorV);
rLO.addView(cursorV);
}
break;
case MotionEvent.ACTION_UP:
if (vSym > 0 || hSym > 0) {
rLO.removeView(cursorH);
rLO.removeView(cursorV);
}
rLO.removeView(cursor);
break;
} // end switch
NOTE:
I used the word "cursor" in the code for "pointer." The pointer (cursor) is programmatically drawn from a different class which creates a circle.
It's probably working, but the problem is that your screen's top left is (0,0). Any negative position is off the screen. Instead divide the screen width or height in half mirror across that.

Android - Moving a single image

Can anyone help please.
Im writing a small Android game where the player is able to select a "barrier" and drag it accross the screen with their finger. I have the barriers drawn on the screen and I am able to drag it accross the screen.
My problem however is when I add more than 1 barrier, eg 3 barriers, and drag a barrier accross the screen, they all drag and they all drag to the same position. That is to say they all lie on top of each other, making it look as though there is only 1 barrier.
Here is my code, can anyone please tell me where I am going wrong/explain where I am going wrong.
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener {
// Initialising the Barrier
private Barrier barrier[] = new Barrier[3];
// The Main Game Panel
public MainGamePanel(Context context) {
super(context);
// Adding a call-back (this) to the surfaceHolder to intercept events
getHolder().addCallback(this);
// Creating the Game items
// The starting coordinates of the Barrier
int x = 30;
int y = 270;
barrier[0] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.blue_barrier), x, y);
barrier[1] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.green_barrier), x + 15, y);
barrier[2] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.pink_barrier), x + 30, y);
// Create the Game Loop Thread
thread = new MainThread(getHolder(), this);
// Make the GamePanel focusable so it can handle events
setFocusable(true);
}
// Handles the touch events
public boolean onTouchEvent(MotionEvent event)
{
int eventAction = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch (eventAction)
{
// Touch down so check if finger is on Barrier
case MotionEvent.ACTION_DOWN:
if (x > barrier[0].getX() && x < barrier[0].getX() + 8
&& y > barrier[0].getX() && y < barrier[0].getY() + 8)
{
barrier[0].isTouched();
}
else if (x > barrier[1].getX() && x < barrier[1].getX() + 8
&& y > barrier[1].getX() && y < barrier[1].getY() + 8)
{
barrier[1].isTouched();
}
else if (x > barrier[2].getX() && x < barrier[2].getX() + 8
&& y > barrier[2].getX() && y < barrier[2].getY() + 8)
{
barrier[2].isTouched();
}
break;
// Touch-drag with the Barrier
case MotionEvent.ACTION_MOVE:
// Move the Barrier the same as the finger
for (int i = 0; i < barrier.length; i++)
{
if (barrier[i] == barrier[0])
{
barrier[0].setX(x);
barrier[0].setY(y);
} // end if
else if (barrier[i] == barrier[1])
{
barrier[1].setX(x);
barrier[1].setY(y);
}
else if (barrier[i] == barrier[2])
{
barrier[2].setX(x);
barrier[2].setY(y);
} // end else if
} // end for
break;
case MotionEvent.ACTION_UP:
// Finger no longer on Barrier - Do Nothing
break;
}
return true;
}
// Render - Draws the Game Item Bitmaps to the screen
public void render(Canvas canvas)
{
// Set the background to white
canvas.drawColor(Color.WHITE);
barrier[0].draw(canvas);
barrier[1].draw(canvas);
barrier[2].draw(canvas);
}
// Update
// This is the Game's update method
// It iterates through all the Objects and calls their update() methods (if they have one)
public void update()
{
} // end update
In your barrier move code, you aren't checking a particular barrier is touched, so you're moving all of them to the same coordinates. The following loop does exactly the same thing as your current code:
// Move the Barrier the same as the finger
for (int i = 0; i < barrier.length; i++)
{
barrier[i].setX(x);
barrier[i].setY(y);
} //end for
To fix this, you need to check if the current barrier in the loop is the one that was touched, and can change that whole loop to something like:
// Move the Barrier the same as the finger
for (int i = 0; i < barrier.length; i++)
{
if (barrier[i].isTouched())
{
barrier[i].setX(x);
barrier[i].setY(y);
} // end if
} // end for
You would then need to make sure that you un-set the touched property in the ACTION_UP section. If you post your Barrier class definition I can probably help more.
I'm pretty sure your problem could be solved by making the barriers three separate objects instead of in an array.
As already pointed out, your code in handling the move is wrong because it moves all the Barriers to the same X,Y when it should only move the one that is touched.
Also, you are never resetting the isTouched on the objects in the Action up. When the user lifts their finger you should set them all to isTouched == false. If you don't do that then once you touch one it will always move to the X,Y.

Categories

Resources