this is actually my first time asking a question here, so please be patient with me :)
I need to make an android game for university without the use of any external libraries and I have the following problem:
In the game you control a taxi in space by sliding up and down the sides of the screen (landscape mode) where the top is max speed and the bottom is slow speed, on the right side you control the right thruster and on the left the left thruster. Basically the control is already working, with one problem: If you stop moving your finger (finger is still touching the screen) nothing happens anymore (after approx. 1 sec):
This is my first time programming Android, please bear that in mind (in case I made a really obvious mistake):
@Override
public boolean onTouchEvent(MotionEvent event) {
int maskedAction = event.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
{
// not needed
break;
}
case MotionEvent.ACTION_MOVE:
{
// all Settings are set here
// normalized in [0,1], will be given to GameEngine Acceleration methods
float leftThrusterNormalized = 0.0f;
float rightThrusterNormalized = 0.0f;
// counter for the current pointer (= TouchEvent)
int current = 0;
// true = more than one pointer; false = one or no pointers
boolean multiTouch = false;
// go through each pointer
for (int i = 0; i < event.getPointerCount(); i++) {
// if there are more than two pointers, this is needed, otherwise an
// java.lang.IllegalArgumentException will be thrown if only pointer is active
if (event.getPointerCount() >= 2) {
multiTouch = true;
current = event.getPointerId(i);
// the touch event occurs in the right area, relative to screen width
if (event.getX(current) < getWidth()*0.2 || event.getX(current) > getWidth()*0.8) {
engine.accelerateTaxiTranslation(normalizeCoords(event.getY(current)));
// Touch event on the right side of the screen, set value for the right Thruster
// via normalisation of y-Coordinate
if (event.getX(current) > getWidth() * 0.8) {
rightThrusterNormalized = normalizeCoords(event.getY(current));
}
// Touch event on the left side of the screen, set value for the left Thruster
// via normalisation of y-Coordinate
if (event.getX(current) < getWidth() * 0.2) {
leftThrusterNormalized = normalizeCoords(event.getY(current));
}
}
}
// only one pointer, therefore only one thruster at a time will be active,
// ==> only setting Rotation Speed, not Translation Speed
else if (event.getPointerCount() == 1) {
// turn left
if (event.getX() > getWidth() * 0.8) {
engine.accelerateTaxiRotation(-1*(normalizeCoords(event.getY())));
}
// turn right
else if (event.getX() < getWidth() * 0.2) {
engine.accelerateTaxiRotation(normalizeCoords(event.getY()));
}
}
}
// in case of multiple pointers the rotation value is set via the difference of
// bigger - smaller value (difference of the different touch events)
if (multiTouch) {
// right Thruster is stronger than left thruster ==> left turn and therefore multiply with -1
if (leftThrusterNormalized < rightThrusterNormalized) {
engine.accelerateTaxiRotation(-1*(rightThrusterNormalized-leftThrusterNormalized));
}
// left Thruster is stronger than right thruster ==> right turn and therefore positive value
else if (leftThrusterNormalized >= rightThrusterNormalized) {
engine.accelerateTaxiRotation(leftThrusterNormalized - rightThrusterNormalized);
}
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
{
// not needed
break;
}
}
// return true, otherwise MotionEvent.ACTION_MOVE will not be detected
return true;
}
Do I have to write the whole Code in ACTION.DOWN as well? Or take the time for that event? Help would be appreciated.
What I expect it to do when the finger doesn't move: (Current Behaviour) Depending on the y-Position of the fingers a value between [0,1] is interpolated and given to the translate method, when I don't move the fingers I would like the taxi to keep the speed so to speak. Right now, even if I comment out ACTION_DOWN, ACTION_POINTER_DOWN, etc. like user3427079 suggests, the ship stops, however I want it to continue at a constant speed if the finger stops moving but still is pressed on the screen.