Here is my method code I use in onTouchEvent:
public void touch(MotionEvent event) {
// starta = "klicka för att börja";
if (event.getAction() == MotionEvent.ACTION_DOWN) {
starta = "Click to start";
fN.aktivitetNer.start();
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
if (y == -300) {
touch(event);
}
else if (y > -300) {
fN.aktivitetNer.interrupt();
fU.aktivitetUpp.start();
p++;
}
}
else if (y2 <= y + xMax) {
fN.aktivitetNer.interrupt();
fU.aktivitetUpp.start();
p = 0;
}
}
}
What should happening is a knife coming down very fast with help of a Thread(fN.aktivitetNer) and if the knife slice the finger(y2) you loose, if you're fast enough you score one point(p), when you loose or win the knife will go up again with another Thread(fU.aktivitetUpp)
I know it's not perfect but it's now working at all!
ps. if you click two times on the screen the programm will crash.
Well it certainly won't work in it's current form, because the code will never enter your second if statement.
Your first if statement asserts that event.getAction() == MotionEvent.ACTION_DOWN. If this is true, then atevent.getAction() cannot possibly be equal to MotionEvent.ACTION_CANCEL and you will never enter that block of code.
Since I don't know what your thread does I can't be certain, but you probably want something more like this:
public void touch(MotionEvent event)
{
// starta = "klicka för att börja";
int actionCode = event.getAction();
if (actionCode == MotionEvent.ACTION_DOWN)
{
//Start to drop knife
}
else if (actionCode == MotionEvent.ACTION_CANCEL || actionCode == MotionEvent.ACTION_UP)
{
if (Knife is falling, and touch point is below knife)
{
//Add one to score
}
else if (Knife is falling, and touch point is above knife)
{
//Reset score to 0
}
}
else if (Knife has already fallen and still touching screen)
{
//Reset score to 0
}
}
Related
I have researched a lot but I couldn't find even a blog that tell about MultiTouch events. All are giving tutorials on single swipe. I wish to apply fling and 2 finger swipe both in same activity.
I got the solution myself after trying and failing a lot
#Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "onTouchEvent: " + event.getAction());
//with the getPointerCount() i'm able to get the count of fingures
if(event.getPointerCount()>1){
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_POINTER_DOWN:
// This happens when you touch the screen with two fingers
mode = SWIPE;
// event.getY(1) is for the second finger
p1StartY = event.getY(0);
p2StartY = event.getY(1);
break;
case MotionEvent.ACTION_POINTER_UP:
// This happens when you release the second finger
mode = NONE;
float p1Diff = p1StartY - p1StopY;
float p2Diff = p2StartY - p2StopY;
//this is to make sure that fingers go in same direction and
// swipe have certain length to consider it a swipe
if(Math.abs(p1Diff) > DOUBLE_SWIPE_THRESHOLD
&& Math.abs(p2Diff) >DOUBLE_SWIPE_THRESHOLD &&
( (p1Diff>0 && p2Diff>0) || (p1Diff < 0 && p2Diff<0) ))
{
if(p1StartY > p1StopY)
{
// Swipe up
doubleSwipeUp();
}
else
{
//Swipe down
doubleSwipeDown();
}
}
this.mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if(mode == SWIPE)
{
p1StopY = event.getY(0);
p2StopY = event.getY(1);
}
break;
}
}
else if(event.getPointerCount() == 1){
//this is single swipe, I have implemented onFling() here
this.gestureObject.onTouchEvent(event);
}
return false;
}
i am trying to make the game Pong in Java. Now i have a problem with the collision on the paddle. It is not moving the other direction. It does work when it hits the sides of the panel.
For now i just needs to go the other way when it hits the X value of the paddle.
I made a function to check the collision.
What am i doing wrong here. Can someone please explain me this?
Thanks in advance
public void checkCollision()
{
if (ball.getY() == 0)
{
moveDown = true;
moveUp = false;
}
if ((ball.getY() + ball.getHeight()) == height)
{
moveDown = false;
moveUp = true;
}
if (ball.getX() == computer.getX())
{
moveRight = false;
moveLeft = true;
}
}
class TimerHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (counter == 0)
{
Random rand = new Random();
int random = rand.nextInt(2) + 1 ;
if (random == 1)
{
moveUp = true;
moveRight = true;
counter++;
}
else if (random == 2)
{
moveDown = true;
moveLeft = true;
counter++;
}
}
controlMovement();
checkCollision();
repaint();
}
}
I believe that your checkCollision function is incomplete... somehow, the function must have access to both paddle current positions to check if the ball hits one of the paddles...
Another suggestion is to use only one flag to indicante the movement in one axis, for example, boolean monvingUp = true indicantes that the ball is moving up at the Y-axis, when false, such flag indicates that is moving down at the Y-axis...
I changed the = to > and it fixed the problem
I am trying to make a donkey kong game.
Currently, I have a boolean called jump which tells that game if the player is jumping or not, when the space bar is pressed.
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE){
StartJump();
}
}
StartJump: Makes the sprite start jumping by changing velocity. It also changes the jump boolean to true.
public void StartJump() {
if (onground) {
velocityY = -4;
onground = false;
jump = true;
}
}
KeyReleased:
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE){
EndJump();
}
}
EndJump: Sets jump to false
public void EndJump() {
if(velocityY < -6.0)
velocityY = -6;
jump = false;
}
The problem occurs when you hold down the spacebar, jump is set to true.
Which then causes the forloop which sets the Y position of Mario to not work and he falls through the floor.
Loop: bricks just contains rectangles for Mario to stand on
for(int i = 0; i < bricks.size(); i++) {
if(marHitBox.intersects(bricks.get(i)) && !mario.getClimb() && !mario.getJump() && marHitBox.intersects(jump.get(i))) {
mario.setMarY((int)bricks.get(i).getY()-40);
mario.setVeloY(0f);
mario.setOnGround(true);
}
}
It is the !mario.getJump() that is causing the problem. Is there any way to see if the spacebar is held for more than x milliseconds to make jump = false?
Try measuring the passing time between the spacebar is pressed and released, it will fit your need.
I think just a check would make it go away
public void StartJump(){
if (!jump && onground){
velocityY = -4;
onground = false;
jump = true;
}
}
I have an issue in my app where I need to perform a different action based on if one fingers or two fingers are pressed down.
Currently I have the follow code:
#Override
public boolean onTouchEvent(MotionEvent event){
int touchX = (int)event.getX();
int touchY = (int)event.getY();
int action = event.getActionMasked();
//Multitouch
if(event.getPointerCount() > 1 && action == MotionEvent.ACTION_POINTER_DOWN){
System.out.println("2 Finger press");
return true;
}
else if(action == MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_POINTER_DOWN){
if(event.getPointerCount() == 1) {
System.out.println("single finger down");
invalidate();
return true;
}
}
return false;
}
The issue I am having is when I press with 2 fingers, the multitouch part registers, and after that so does the single press.
I did some googling around on ways to fix that and that would be why I am checking on the single touch conditional that action != MotionEvent.ACTION_POINTER_DOWN which I thought would fix the issue. That didn't fix the issue hence I decided to check that event.getPointerCount() == 1, but unfortunately that still causes both lines to print when I press with 2 fingers.
To summarize I need to only call the 2 finger part when 2 fingers are pressed down and not both.
refer to the documentation on "Handling Multi-Touch Gestures". it details exactly what you're looking for.
in essence, each active pointer is assigned an ID when it makes contact with the screen. your solution will should take into account which IDs come into play and are removed by observing various action states.
something a little bit along these lines (but beware compatibility calls for methods like getActionIndex()):
UPDATED CODE SAMPLE
public class MotionActivity extends AppCompatActivity {
private int primaryPointerId = -1;
private int secondaryPointerId = -1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new FrameLayout(this));
}
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getActionMasked();
switch(action) {
case MotionEvent.ACTION_DOWN:
primaryPointerId = event.getPointerId(0);
Log.d(getClass().getName(), "Primary pointer ID == [" + primaryPointerId + "]");
break;
case MotionEvent.ACTION_POINTER_DOWN:
secondaryPointerId = event.getPointerId(event.getActionIndex());
Log.d(getClass().getName(), "Secondary pointer ID == [" + secondaryPointerId + "]");
break;
case MotionEvent.ACTION_MOVE:
if(primaryPointerId > -1 && secondaryPointerId > -1) {
Log.d(getClass().getName(), "Two-point touch...");
} else {
Log.d(getClass().getName(), "One-point touch...");
}
break;
case MotionEvent.ACTION_POINTER_UP:
if(event.getPointerId(event.getActionIndex()) == primaryPointerId) {
primaryPointerId = secondaryPointerId;
}
secondaryPointerId = -1;
break;
case MotionEvent.ACTION_UP:
primaryPointerId = -1;
break;
}
return true;
}
}
I'm making a platfrom game for android and I could use some help with the touch events.
This is my code:
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
if (tempObject.getId() == ObjectId.Player) {
if (e.getAction() == MotionEvent.ACTION_MOVE) {
if (moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(true);
tempObject.setMovingRight(false);
}
if (moveLeftExit.contains(scaledX, scaledY)
&& !moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(true);
tempObject.setMovingLeft(false);
}
if (moveRightExit.contains(scaledX, scaledY)
&& !moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(false);
}
}
if (e.getAction() == MotionEvent.ACTION_UP
|| e.getAction() == MotionEvent.ACTION_OUTSIDE) {
if (moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(false);
}
}
if (e.getAction() == MotionEvent.ACTION_DOWN) {
if (jump.contains(scaledX, scaledY)) {
if(tempObject.getVelY() ==0)
tempObject.setVelY(-15);
}
}
}
}
return true;
}
Everything works great while I use one finger, if I touch the moveRight rectangle the character moves right and when I move my finger away he stopes as expected. The problem is that if I touch a button while touching some other button it wont react to it.
So I guess my question is, how can I modify my code so it will react to multi touch?
Thanks!! :)
Here is a simple code.
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
int pointerIndex = MotionEventCompat.getActionIndex(event);
int x = (int)MotionEventCompat.getX(event,pointerIndex);
int y = (int)MotionEventCompat.getY(event,pointerIndex);
switch(action)
{
case MotionEvent.ACTION_DOWN:
//
// First finger was touched. Set "pointerIndex" to some value (lets say 0 or -1)
// Save "pointerIndex" corresponding to this touched object.
//
break;
case MotionEvent.ACTION_POINTER_DOWN:
//
// More finger touched when first finger is already touched.
// Save "pointerIndex" corresponding to this touched object.
//
break;
case MotionEvent.ACTION_MOVE:
//
// Finger with "pointerIndex" was moved.
//
break;
case MotionEvent.ACTION_UP:
//
// The first touched finger went off.
//
break;
case MotionEvent.ACTION_POINTER_UP:
//
// Finger with "pointerIndex" went off (other than first finger)
//
break;
}
return true;
}
Good Luck. :)