我正在尝试使用以下代码为我的游戏设置边框:
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
我有一些使用拖放的skspritenode。
问题是当我将它们移动到场景的左下角时节点从屏幕上消失:( 0,0),但它们在其他角落中正常运行。我不知道为什么。有人可以帮帮我吗?
[编辑]我需要补充一点,我有一些盒子从上面掉下来有物理机构,当我拖放它们时我希望它们不受我用于拖动的代码的影响:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
[self selectNodeForTouch:positionInScene];
_selectedNode.physicsBody.affectedByGravity = NO;
_selectedNode.physicsBody.dynamic = NO;
}
- (void)selectNodeForTouch:(CGPoint)touchLocation {
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
if(![_selectedNode isEqual:touchedNode]) {
[_selectedNode removeAllActions];
[_selectedNode runAction:[SKAction rotateToAngle:0.0f duration:0.1]];
_selectedNode = touchedNode;
if([[touchedNode name] intValue] > 0) {
[_selectedNode runAction:[SKAction sequence:@[ [SKAction waitForDuration:.1],
[SKAction scaleTo:1.5 duration:1.0]
// [SKAction scaleTo:1.0 duration:1.0],
]]];
}
}
else{
if([[_selectedNode name] intValue] > 0) {
[_selectedNode runAction:[SKAction sequence:@[ [SKAction waitForDuration:.1],
[SKAction scaleTo:1.5 duration:1.0]
// [SKAction scaleTo:1.0 duration:1.0],
]]];
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
[self panForTranslation:translation];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if([_selectedNode.name intValue] > 0){
_selectedNode.physicsBody.affectedByGravity = YES;
_selectedNode.physicsBody.dynamic = YES;
}
}
- (void)panForTranslation:(CGPoint)translation {
CGPoint position = [_selectedNode position];
if([[_selectedNode name] intValue] > 0) {
if((position.y+translation.y > 0)){
[_selectedNode setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)];
}
} else {
CGPoint newPos = CGPointMake(position.x + translation.x, position.y + translation.y);
}
}