Sprite Kit使用SKAction移动节点会造成麻烦

时间:2014-06-26 11:29:13

标签: ios7 sprite-kit skaction sknode

我正在尝试创建一个块游戏,我有一排块,我从右到左滑动结束块,每个其他块向右移动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];
}
}

1 个答案:

答案 0 :(得分:0)

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

一个解决方法是在下次启动之前删除现有动画。这样的事情(未经测试):

- (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"];
}