【问题标题】:Sprite Kit Moving Nodes using SKAction causing troubleSprite Kit 使用 SKAction 移动节点导致问题
【发布时间】:2014-06-26 11:29:12
【问题描述】:

我正在尝试创建一个积木游戏,其中有一排积木,我将末端积木从右向左滑动,而其他积木中的每一个都向右移动 1 个位置。如果我然后再次将块向后滑动,它们都会向左滑动 1 个位置。我遇到的问题是,如果我缓慢拖动它可以正常工作,但是如果我快速来回拖动块会变得一团糟,它们会相互移动,所以不是一排 6,而是一排 4因为 2 个街区位于其他街区的后面。任何建议表示赞赏。

这是我的 Scene.M 中的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

self.selectedNode = [self nodeAtPoint:[[touches anyObject] locationInNode:self]];

[self.selectedNode.physicsBody setDynamic:YES];
self.selectedNode.zPosition = 1;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentPoint = CGPointMake([[touches anyObject] locationInNode:self].x , selectedNode.position.y);
CGPoint previousPoint = CGPointMake([[touches anyObject] previousLocationInNode:self].x , selectedNode.position.y);
_deltaPoint = CGPointSubtract(currentPoint, previousPoint);
}

-(void)update:(CFTimeInterval)currentTime {

CGPoint newPoint = CGPointAdd(self.selectedNode.position, _deltaPoint);

self.selectedNode.position = newPoint;

_deltaPoint = CGPointZero;

}

-(void)didBeginContact:(SKPhysicsContact *)contact{

SKNode *node = contact.bodyA.node;
if ([node isKindOfClass:[Block class]] && node != selectedNode) {
    [(Block *)node collidedWith:contact.bodyB contact:contact];
}

node = contact.bodyB.node;
if ([node isKindOfClass:[Block class]] && node != selectedNode) {
    [(Block *)node collidedWith:contact.bodyA contact:contact];
}
}

这是我的 Block.M 中的代码

-(instancetype)initWithPosition:(CGPoint)pos andType:(NSString *)blockType{

SKTextureAtlas *atlas =
[SKTextureAtlas atlasNamed: @"Blocks"];
SKTexture *texture = [atlas textureNamed:blockType];
texture.filteringMode = SKTextureFilteringNearest;

if (self = [super initWithTexture:texture]){
    self.name = blockType;
    self.position = pos;
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width - 30, self.size.height - 8)];
    self.physicsBody.usesPreciseCollisionDetection = YES;
    self.physicsBody.categoryBitMask = 1;
    self.physicsBody.collisionBitMask =  0;
    self.physicsBody.contactTestBitMask = 1;

}

return self;
}

- (void)collidedWith:(SKPhysicsBody *)body contact:(SKPhysicsContact*)contact {

CGPoint localContactPoint = [self.scene convertPoint:contact.contactPoint toNode:self];

if (localContactPoint.x < 0) {
    SKAction *moveLeftAction = [SKAction moveByX:-48 y:0 duration:0.0];
    [self runAction:moveLeftAction];
} else {
    SKAction *moveRightAction = [SKAction moveByX:48 y:0 duration:0.0];
    [self runAction:moveRightAction];
}
}

【问题讨论】:

  • 使用 runAction:withKey: 确保在给定节点上只运行一个移动操作。您的问题很可能是因为多个移动操作堆叠在一起。

标签: ios7 sprite-kit skaction sknode


【解决方案1】:

如果您在给定块上的先前移动操作完成之前开始碰撞,您可能会同时运行多个移动操作,这可能会导致奇怪的动画。

一个修复方法是在下一次开始之前删除现有动画。像这样的东西(未测试):

- (void)collidedWith:(SKPhysicsBody *)body contact:(SKPhysicsContact*)contact {

CGPoint localContactPoint = [self.scene convertPoint:contact.contactPoint toNode:self];

    // If we have any move actions still in progress, complete them so we can start a new one
    if ([self actionForKey:@"blockMove"]) {
        [self removeActionForKey:@"blockMove"];
        self.position = CGPointMake(_targetX, self.position.y);
    }

    if (localContactPoint.x < 0) {
        _targetX = self.position.x - 48;
    } else {
        _targetX = self.position.x + 48;
    }
    SKAction *moveLeftAction = [SKAction moveToX:_targetX y:0 duration:0.0];
    [self runAction:moveLeftAction withKey:@"blockMove"];
}

【讨论】:

  • 试过了,但同样的问题。一些块正在移动超过一个块空间。它们都开始排成一列,然后当我的手指在行中快速来回滑动时,只有 2 或 3 个可见,其他空间空白。
  • 我还注意到,有时 localContactPoint 应该是负数时是正数,所以当它应该向左移动时,块会向右移动!
  • hmm...我想知道这是否是使用 moveByX 并计算增量而不是 moveToX 与目的地的副作用...使用增量,如果事情一旦出现问题就会永久关闭.我已经更新了使用 moveToX 的解释,这行得通吗?
  • 没有 moveToX 有同样的问题。一些块仍在顶部或其他块下方移动。与物理速度有关吗?
  • 不幸的是我不知道从哪里开始,没有足够的调试物理问题的经验。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多