[英]Android Changing object position with swipe (AndEngine)
I have been developing a game for 2 weeks. 我已经开发了2个星期的游戏。 I have a ball on center top of device screen.
我在设备屏幕的中央上方有一个球。 I use swipe for moving it just along X coordinate.
我使用滑动将其沿X坐标移动。 I have three cases for this situation.
对于这种情况,我有三种情况。
Case 1: (TouchEvent.ACTION_DOWN) Ball will not move any position. 情况1:(TouchEvent.ACTION_DOWN)球不会移动任何位置。
Case 2: (TouchEvent.ACTION_MOVE) This case is significant for me. 情况2:(TouchEvent.ACTION_MOVE)这种情况对我很重要。 If I swipe my finger to right side, ball moves right, If left side.....
如果我向右滑动手指,则球向右移动;如果向左滑动,则.....
Case 3: (TouchEvent.ACTION_UP) The ball falls down with gravity, after it has dynamic body. 情况3:(TouchEvent.ACTION_UP)在具有动态物体之后,球因重力而掉落。 anyway.
无论如何。
Normally with above three cases, when I swipe my finger on screen, ball immediately comes same x position with my finger. 通常,在上述三种情况下,当我在屏幕上滑动手指时,手指立即将球移到相同的x位置。 Let me explain this act with an example.
让我用一个例子来解释这一行为。
My camera_width is 720. The ball stands on 360. I touch down 480 x position. 我的camera_width是720。球的高度为360。我按下480 x位置。 If I just swipe 2 unit to right, My finger current position will be 482. Problem is that ball jumps to 482. I don't want it.
如果我仅向右滑动2个单位,我的手指当前位置将是482。问题是球跳到482。我不想要它。
My move difference is 2 unit. 我的移动差异是2个单位。 I want it that jumps 362. Here' my code:
我希望它跳362。这是我的代码:
if ( mPhysicsWorld != null && currentBody != null)
{
switch ( pSceneTouchEvent.getAction() )
{
case TouchEvent.ACTION_DOWN:
isFirstTouch = true;
oldLocationX = pSceneTouchEvent.getX();
newLocationX = pSceneTouchEvent.getX();
break;
case TouchEvent.ACTION_MOVE:
if ( oldLocationX + 30 > pSceneTouchEvent.getX() && pSceneTouchEvent.getX() > oldLocationX - 30 )
{
if ( !isFirstTouch )
{
if ( newLocationX != pSceneTouchEvent.getX() )
{
oldLocationX = newLocationX;
paddleX = pSceneTouchEvent.getX() + (pSceneTouchEvent.getX() - newLocationX);
paddleX = Math.max( paddleX, currentSprite.getWidthScaled() / 2 );
paddleX = Math.min( paddleX, CAMERA_WIDTH - currentSprite.getWidthScaled() / 2 );
currentBody.setTransform( paddleX / 32, currentBody.getPosition().y, 0 );
newLocationX = pSceneTouchEvent.getX();
}
}
}
else
{
isFirstTouch = false;
if ( newLocationX != pSceneTouchEvent.getX() )
{
oldLocationX = newLocationX;
paddleX = pSceneTouchEvent.getX() + (pSceneTouchEvent.getX() - newLocationX);
paddleX = Math.max( paddleX, currentSprite.getWidthScaled() / 2 );
paddleX = Math.min( paddleX, CAMERA_WIDTH - currentSprite.getWidthScaled() / 2 );
currentBody.setTransform( paddleX / 32, currentBody.getPosition().y, 0 );
newLocationX = pSceneTouchEvent.getX();
}
}
Log.d( "move", "oldPositionX: " + oldLocationX + " ");
break;
case TouchEvent.ACTION_UP:
currentBody.setType( BodyDef.BodyType.DynamicBody );
addFaceInFuture( currentBody );
break;
}
try to replace 尝试更换
paddleX = pSceneTouchEvent.getX() + (pSceneTouchEvent.getX() - newLocationX);
with 与
paddleX = currentBody.getPosition().x + (pSceneTouchEvent.getX() - newLocationX);
another approach would be to save the x position from the event (evtStartX) and the body (bodyStartX) at ACTION_DOWN 另一种方法是保存来自事件(evtStartX)和正文(bodyStartX)的x位置,位置为ACTION_DOWN
At ACTION_MOVE you only need to calculate the difference between the current x position and evtStartX. 在ACTION_MOVE,您只需要计算当前x位置和evtStartX之间的差。 Then add the result to bodyStartX
然后将结果添加到bodyStartX
case TouchEvent.ACTION_DOWN:
evtStartX = pSceneTouchEvent.getX();
bodyStartX = currentBody.getPosition().x
break;
case TouchEvent.ACTION_MOVE:
xDif = pSceneTouchEvent.getX() - evtStartX;
xDif = xDif/32;
body.setPositon(bodyStartX + xDif, body.getPosition().y);
break;
I improved my code for my problem. 我针对自己的问题改进了代码。 I give you solution at below.
我在下面给你解决方案。
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
{
int eid = pSceneTouchEvent.getAction();
float leftBorderPosition = currentSprite.getWidthScaled() / 2;
float rightBorderPosition = CAMERA_WIDTH - currentSprite.getWidthScaled() / 2;
if ( mPhysicsWorld != null && currentBody != null)
{
switch ( eid )
{
case TouchEvent.ACTION_DOWN:
oldLocationX = pSceneTouchEvent.getMotionEvent().getX() / 32;
break;
case TouchEvent.ACTION_MOVE:
newLocationX = pSceneTouchEvent.getMotionEvent().getX() / 32;
if ( pSceneTouchEvent.getMotionEvent().getHistorySize() > 0 )
{
oldLocationX = pSceneTouchEvent.getMotionEvent().getHistoricalX(0) / 32;
}
paddleX = (currentBody.getPosition().x + (newLocationX - oldLocationX));
paddleX = Math.max( paddleX, (leftBorderPosition / 32) );
paddleX = Math.min( paddleX, (rightBorderPosition / 32) );
currentBody.setTransform( paddleX, currentBody.getPosition().y, 0 );
break;
case TouchEvent.ACTION_UP:
engineLock.lock();
currentBody.setType( BodyDef.BodyType.DynamicBody );
engineLock.unlock();
if ( currentBody.getUserData().equals( "bomb" ) )
{
/**
*
* Bomba su anda aktif ve butun update'ler unregister olsun.
*
*/
bombBody = currentBody;
bombSprite = currentSprite;
mEngine.unregisterUpdateHandler( allBodyIsStoppedIUpdatehandler );
mEngine.unregisterUpdateHandler( isBallsReachedLimit );
addBallInFuture( null );
}
else
{
addBallInFuture( currentBody );
}
break;
}
}
return false;
}
TouchEvent.ACTION_DOWN and TouchEvent.ACTION_MOVE cases include the solution. 解决方案包括TouchEvent.ACTION_DOWN和TouchEvent.ACTION_MOVE案例。 Here is important thing that when we swipe, we have to get differrence between touch position and pre position.
重要的是,当我们滑动时,我们必须获得触摸位置和预设位置之间的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.