【发布时间】:2015-09-30 09:44:33
【问题描述】:
我有一个有趣的问题。我创建了一个带有关联物理体的 SKSpriteNode。
SKTexture *shipTexture = [SKTexture textureWithImageNamed:@"Spaceship"];
self.heroSpriteNode = [HeroSpriteNode spriteNodeWithTexture:shipTexture size:CGSizeMake(40, 35.2)];
self.heroSpriteNode.physicsBody = [SKPhysicsBody bodyWithTexture:shipTexture size:CGSizeMake(40, 35.2)];
self.heroSpriteNode.physicsBody.dynamic = YES;
self.heroSpriteNode.physicsBody.mass = 1000.0;
self.heroSpriteNode.physicsBody.affectedByGravity = NO;
self.heroSpriteNode.physicsBody.allowsRotation = YES;
self.heroSpriteNode.physicsBody.angularDamping = 0.5;
self.heroSpriteNode.anchorPoint = CGPointMake(0.5,0.5);
self.heroSpriteNode.physicsBody.categoryBitMask = heroCategory;
self.heroSpriteNode.physicsBody.contactTestBitMask = groundCategory;
self.heroSpriteNode.physicsBody.collisionBitMask = groundCategory | edgeCategory;
场景本身没有重力。
在更新例程中,我将applyTorque: 称为物理体。
-(void) updateWithDeltaTime:(NSTimeInterval)seconds
{
CGFloat rotateTorque = self.rotateRate;
switch (self.rotateForceApplied)
{
case leftForceApplied:
break;
case rightForceApplied:
rotateTorque = -1.0 * rotateTorque;
break;
case zeroForceApplied: // separate from default in case behavior should change
rotateTorque = 0.0;
break;
default:
rotateTorque = 0.0;
break;
}
NSLog(@"before physics position: x %f, y %f", self.heroSpriteNode.position.x, self.heroSpriteNode.position.y);
[self.heroSpriteNode.physicsBody applyTorque:rotateTorque];
NSLog(@"after physics position: x %f, y %f", self.heroSpriteNode.position.x, self.heroSpriteNode.position.y);
}
精灵旋转并且看起来在原地旋转。它实际上在做的是围绕中心点旋转(因此没有净横向移动)。这可以通过在施加扭矩之前和之后记录精灵位置来看到。没有其他动作被应用于物理体。每个方向最多移动9个点。
因为相机被约束固定到“英雄”精灵,并且世界随着相机移动(centerOnNode:示例代码),这会导致整个世界随着精灵旋转而以圆形模式移动。世界本身并不旋转,而是以与旋转相同的速度循环运动。
精灵 anchorPoint 为 0.5, 0.5 我认为它应该围绕一个中心点旋转,这不应该改变精灵的位置。
我做错了什么?
如果重要的话,这是在 iOS9、Xcode7 上运行的设备上而不是在模拟器中。 (iOS9 SpriteKit 文档在 Apple 的网站上公开提供,iOS9 本身也是公开测试版,因此这不应该破坏任何 NDA,而且我不认为这里有任何特定于 iOS9 的内容)
【问题讨论】:
标签: ios objective-c sprite-kit ios9