【发布时间】:2013-10-05 14:38:08
【问题描述】:
我是 SpriteKit 的新手。我需要通过 UIImageView 或 SpriteNode 显示图像(它是游戏的背景图像)。但是,我需要用户能够放大背景并平移。我在不使用 SpriteKit 的情况下轻松完成了这项工作,但无法弄清楚如何让节点接受缩放和平移。如果我在情节提要中使用 UIScrollView,则添加到我的场景中的所有精灵都不会成为子对象,也不会缩放或平移。
有可能吗,你能指出我正确的方向吗?
编辑:
我对你的回答有点困惑,但我是这样做的:
在我的根视图控制器 .m 中:
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * showDesigner = (SKView *)self.view;
SKScene * scene = [iMarchMyScene sceneWithSize:showDesigner.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[showDesigner presentScene:scene];
}
在我的场景中.m:(你的步骤被注释掉了)
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
//create SKView (step 1)
SKView *skview_field = [[SKView alloc]initWithFrame:(CGRectMake(0, 0, size.width, size.height))];
//create UIScrollView with same dimensions as your SpriteKit view (step 2)
UIScrollView* uiscrollview_field = [[UIScrollView alloc]initWithFrame:skview_field.frame];
//initialize the field (field is the game's background node)
SKSpriteNode *field = [SKSpriteNode spriteNodeWithImageNamed:@"Field"];
field.position = CGPointMake(CGRectGetMidX(self.frame), (CGRectGetMidY(self.frame)));
field.size = CGSizeMake(self.size.width, self.size.height / 3);
//create a UIView (called scrollViewContent) with dimensions of background node (step 3)
UIView* scrollViewContent = [[UIView alloc]initWithFrame:field.frame];
//set UIScrollView contentSize with same dimensions as your scrollViewContent and add a scrollContentview as your UISCrollView subview (step 4)
[uiscrollview_field setContentSize:(CGSizeMake(scrollViewContent.frame.size.width, scrollViewContent.frame.size.height))];
scrollViewContent.frame = field.frame;
[uiscrollview_field addSubview:scrollViewContent];
//[self addChild:field]; -- my original code
//.....does a bunch of other inits
return self;
}
这就是我迷路的地方: 第 5 步:现在,将 SKView 和 UIScrollView 添加到您的根视图中。
如果我理解正确,我需要转到 myRootViewController.m 并在 viewDidLoad 方法中,将 SKView 和 UIScrollView(在 myScene.m 中初始化)作为子视图添加到在 viewDidLoad 方法中初始化的 SKView 中?我不知道该怎么做。
【问题讨论】:
-
我会使用动作,它们解决了我的缩放问题,并且似乎 sprite kit 拥有它的全部力量!
标签: ios7 zooming pan sprite-kit