Xcode, SpriteKit: 旋转整个场景,并编辑数组中精灵的属性
Xcode, SpriteKit: Rotating a whole scene, and editing properties of a sprite inside an array
我正在使用 Xcode、SpriteKit 创建游戏。 2 个问题:
1) 我需要在调用函数时旋转整个场景:
- (void)mouseClick {
// RotateWholeScene somehow
}
我知道通过改变 zrotation 来旋转精灵,但是改变 scene.zrotation 不起作用。
此外,场景中的一些东西需要保持在同一个位置,比如乐谱等
如果有一种方法可以让我旋转整个场景,就好像相机视图已经改变,我会更喜欢它,因为有计算(比如坠落的物体),如果它们不是这样的话,会更容易做到'不需要在代码中更改。
2) 现在程序每帧创建一个精灵,并将精灵放入一个数组(我在开始时声明)。
NSMutableArray *SomeArray = [NSMutableArray arrayWithObjects: nil];
稍后在代码中:
- (void)CalledEachFrame {
[self addChild: SomeSprite];
[SomeArray addObject: SomeSprite];
}
在代码的其他地方,我需要:
- (void)AlsoCalledEachFrame {
for (int i; i < X; i++) {
SomeArray[i].position = A;
}
}
这(以及其他几次尝试)都不起作用,我得到一个错误:
***编辑:我的错。在我展示的图像中,我将位置设置为整数而不是 CGPoint。尽管如此,即使我放置 CGPoint 而不是那些 99s,也会出现同样的错误。
您可以使用以下代码手动旋转视图:
CGAffineTransform transform = CGAffineTransformMakeRotation(-0.1); // change value to desires angle
self.view.transform = transform;
你在这里同时提出了两个问题。我应该劝阻。尽管如此,这里是:
1) 我遇到了类似的问题,我对这个问题的解决方案是将 scene 节点和 hud 节点分别放在一个分离 SKScene 子类的子节点,像这样:
@interface SKSceneSubclass : SKScene
// ...
@property (strong, nonatomic, readonly) SKNode *sceneContents;
@property (strong, nonatomic, readonly) SKNode *hud;
@end
在场景初始化器中,我会输入:
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.sceneContents = [SKNode new];
[self addChild:self.sceneContents];
self.hud = [SKNode new];
[self addChild:self.hud];
self.hud.zPosition = 20.f; // ensures the hud on top of sceneContents
// your custom stuff
// ...
}
return self;
}
这需要您决定添加子节点时哪个节点(sceneContents 或 hud)将成为它们的父节点。
[self.sceneContents addChild:newSprite];
[self.hud addChild:someCoolHudButton];
然后,您可以在 sceneContents 节点上 运行 [SKAction rotateByAngle:angle duration:duration]
。 hud 及其子项不会受到影响。我目前正在使用这种方法移动和缩放 sceneContents,同时拥有一个固定的 HUD,它的行为应该如此。
2) 问题是你没有告诉编译器你正在访问的项目是一个 SKNode 对象,所以编译器不允许你访问 position
属性。如果您确定数组只包含 SKNodes 或其子类,请使用 forin
迭代器方法。
for (<#type *object#> in <#collection#>) {
<#statements#>
}
在你的情况下,它看起来像这样。
CGPoint newPosition = CGPointMake(x, y);
for (SKNode *node in SomeArray) {
node.position = newPosition;
}
我正在使用 Xcode、SpriteKit 创建游戏。 2 个问题:
1) 我需要在调用函数时旋转整个场景:
- (void)mouseClick {
// RotateWholeScene somehow
}
我知道通过改变 zrotation 来旋转精灵,但是改变 scene.zrotation 不起作用。
此外,场景中的一些东西需要保持在同一个位置,比如乐谱等
如果有一种方法可以让我旋转整个场景,就好像相机视图已经改变,我会更喜欢它,因为有计算(比如坠落的物体),如果它们不是这样的话,会更容易做到'不需要在代码中更改。
2) 现在程序每帧创建一个精灵,并将精灵放入一个数组(我在开始时声明)。
NSMutableArray *SomeArray = [NSMutableArray arrayWithObjects: nil];
稍后在代码中:
- (void)CalledEachFrame {
[self addChild: SomeSprite];
[SomeArray addObject: SomeSprite];
}
在代码的其他地方,我需要:
- (void)AlsoCalledEachFrame {
for (int i; i < X; i++) {
SomeArray[i].position = A;
}
}
这(以及其他几次尝试)都不起作用,我得到一个错误:
***编辑:我的错。在我展示的图像中,我将位置设置为整数而不是 CGPoint。尽管如此,即使我放置 CGPoint 而不是那些 99s,也会出现同样的错误。
您可以使用以下代码手动旋转视图:
CGAffineTransform transform = CGAffineTransformMakeRotation(-0.1); // change value to desires angle
self.view.transform = transform;
你在这里同时提出了两个问题。我应该劝阻。尽管如此,这里是:
1) 我遇到了类似的问题,我对这个问题的解决方案是将 scene 节点和 hud 节点分别放在一个分离 SKScene 子类的子节点,像这样:
@interface SKSceneSubclass : SKScene
// ...
@property (strong, nonatomic, readonly) SKNode *sceneContents;
@property (strong, nonatomic, readonly) SKNode *hud;
@end
在场景初始化器中,我会输入:
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.sceneContents = [SKNode new];
[self addChild:self.sceneContents];
self.hud = [SKNode new];
[self addChild:self.hud];
self.hud.zPosition = 20.f; // ensures the hud on top of sceneContents
// your custom stuff
// ...
}
return self;
}
这需要您决定添加子节点时哪个节点(sceneContents 或 hud)将成为它们的父节点。
[self.sceneContents addChild:newSprite];
[self.hud addChild:someCoolHudButton];
然后,您可以在 sceneContents 节点上 运行 [SKAction rotateByAngle:angle duration:duration]
。 hud 及其子项不会受到影响。我目前正在使用这种方法移动和缩放 sceneContents,同时拥有一个固定的 HUD,它的行为应该如此。
2) 问题是你没有告诉编译器你正在访问的项目是一个 SKNode 对象,所以编译器不允许你访问 position
属性。如果您确定数组只包含 SKNodes 或其子类,请使用 forin
迭代器方法。
for (<#type *object#> in <#collection#>) {
<#statements#>
}
在你的情况下,它看起来像这样。
CGPoint newPosition = CGPointMake(x, y);
for (SKNode *node in SomeArray) {
node.position = newPosition;
}