我正在尝试创建一个积木游戏,其中有一排积木,我将末端积木从右向左滑动,其他每个积木都向右移动 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];
}
}