我有一个在3乘3平台(正交和2D精灵游戏)中间产生的角色。该平台由9块瓷砖组成。如果触摸在他上方,则角色需要向上移动,如果触摸他的左侧则需要向左移动等等。他只是向上,向下,向左和向上移动对;不是对角线的。我所有这一切都很有效,但它很笨重 - 因为角色并不总是"倾听" &安培;最终走向最接近触摸的另一个方向。有人可以看看&告诉我如何修复代码?以下代码的真正问题似乎是当玩家在四个90度角中的某个角度内触摸屏幕时。系统并不完全知道该做什么......或类似的东西。
//This movement method is called by 'touchesBegan' method inside an SKScene (Good ol' GameScene).
-(void)moveCharRedTo:(CGPoint)touchedLocation
{
//If touch is to the left of the character then move character to the left by 1 tile.
if (touchedLocation.x < _playerChar.position.x)
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedRight = CGPointMake(_charRedPointPos.x - 1, _charRedPointPos.y);
if ([_platformLayer isValidTileCoord:_charRedRight] && ![self tileAtCoord:_charRedRight hasAnyProps:(noGoCategory)])
{
_charRedRight = [_platformLayer pointForCoord:_charRedRight];
[_playerChar runAction:[SKAction moveTo:_charRedRight duration:0.0]];
}
}
//Move right.
if (touchedLocation.x > _playerChar.position.x)
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedRight = CGPointMake(_charRedPointPos.x + 1, _charRedPointPos.y);
if ([_platformLayer isValidTileCoord:_charRedRight] && ![self tileAtCoord:_charRedRight hasAnyProps:(blueTileCategory)])
{
_charRedRight = [_platformLayer pointForCoord:_charRedRight];
[_playerChar runAction:[SKAction moveTo:_charRedRight duration:0.0]];
}
}
//Move down.
if (touchedLocation.y < _playerChar.position.y)
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedDown = CGPointMake(_charRedPointPos.x, _charRedPointPos.y + 1);
if ([_platformLayer isValidTileCoord:_charRedDown] && ![self tileAtCoord:_charRedDown hasAnyProps:(noGoCategory)])
{
_charRedDown = [_platformLayer pointForCoord:_charRedDown];
[_playerChar runAction:[SKAction moveTo:_charRedDown duration:0.0]];
}
}
//Move up.
if (touchedLocation.y > _playerChar.position.y) //else
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedUp = CGPointMake(_charRedPointPos.x, _charRedPointPos.y - 1);
if ([_platformLayer isValidTileCoord:_charRedUp] && ![self tileAtCoord:_charRedUp hasAnyProps:(noGoCategory)])
{
_charRedUp = [_platformLayer pointForCoord:_charRedUp];
[_playerChar runAction:[SKAction moveTo:_charRedUp duration:0.0]];
}
//NSLog(@"Was on (%f,%f)", _charRedPointPos.x, _charRedPointPos.y);
}
[_playerChar movementAnimation]; //Little animation method for every time the character moves.
}
答案 0 :(得分:1)
你的问题是你需要选择一个方向来移动。找出如下所示的移动方式。
NS_ENUM(unsigned short, MovementDirection)
{
Left = 0,
Right = 1,
Top = 2,
Bottom = 3
}
-(MovementDirection)movementDirectionBetweenCurrentPoint:(CGPoint)currentPoint newPoint:(CGPoint)newPoint
{
double dx = newPoint.x - currentPoint.x;
double dy = newPoint.y - currentPoint.y;
if(dx > dy)
return (dx > 0) Right : Left;
else
return (dy > 0) Top : Bottom;
}
-(void)moveCharRedTo:(CGPoint)touchedLocation
{
MovementDirection direction = [self movementDirectionBetweenCurrentPoint:_playerChar.position newPoint:touchedLocation];
//If touch is to the left of the character then move character to the left by 1 tile.
if (direction == Left)
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedRight = CGPointMake(_charRedPointPos.x - 1, _charRedPointPos.y);
if ([_platformLayer isValidTileCoord:_charRedRight] && ![self tileAtCoord:_charRedRight hasAnyProps:(noGoCategory)])
{
_charRedRight = [_platformLayer pointForCoord:_charRedRight];
[_playerChar runAction:[SKAction moveTo:_charRedRight duration:0.0]];
}
}
//Move right.
else if (direction == Right)
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedRight = CGPointMake(_charRedPointPos.x + 1, _charRedPointPos.y);
if ([_platformLayer isValidTileCoord:_charRedRight] && ![self tileAtCoord:_charRedRight hasAnyProps:(blueTileCategory)])
{
_charRedRight = [_platformLayer pointForCoord:_charRedRight];
[_playerChar runAction:[SKAction moveTo:_charRedRight duration:0.0]];
}
}
//Move down.
else if (direction == Bottom)
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedDown = CGPointMake(_charRedPointPos.x, _charRedPointPos.y + 1);
if ([_platformLayer isValidTileCoord:_charRedDown] && ![self tileAtCoord:_charRedDown hasAnyProps:(noGoCategory)])
{
_charRedDown = [_platformLayer pointForCoord:_charRedDown];
[_playerChar runAction:[SKAction moveTo:_charRedDown duration:0.0]];
}
}
//Move up.
else if (direction == Top) //else
{
_charRedPointPos = [_platformLayer coordForPoint:_playerChar.position];
_charRedUp = CGPointMake(_charRedPointPos.x, _charRedPointPos.y - 1);
if ([_platformLayer isValidTileCoord:_charRedUp] && ![self tileAtCoord:_charRedUp hasAnyProps:(noGoCategory)])
{
_charRedUp = [_platformLayer pointForCoord:_charRedUp];
[_playerChar runAction:[SKAction moveTo:_charRedUp duration:0.0]];
}
//NSLog(@"Was on (%f,%f)", _charRedPointPos.x, _charRedPointPos.y);
}
[_playerChar movementAnimation]; //Little animation method for every time the character moves.
}