【问题标题】:ios 7 SKScene size vs visible area?ios 7 SKScene 大小与可见区域?
【发布时间】:2014-07-15 18:57:05
【问题描述】:

Apple 的文档让我有点困惑。

根据文档页面:https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html场景可见区域的点数和场景大小之间的关系应该是相同的句子:

"场景的大小指定了可见部分的大小 以点为单位的场景”。

我创建了一个自定义场景,它根据设备屏幕的大小获取其大小(在我的例子中,我一直在 iPad 的纵向模式下对此进行测试,因此大小应该是 768 宽 x 1024 高)。

在场景的createContent 方法中调用的以下行

NSLog(@"self.size : %f %f", self.size.width, self.size.height);

返回

2014-07-15 14:53:28.844 SpriteWalkthrough[15888:90b] self.size : 768.000000 1024.000000

正如预期的那样。

但是,当我尝试在位置(self.size.width/2,self.size.height/2) 绘制 SKSpriteNode 时,该节点绘制在屏幕的右上角,而不是在中间。

为什么会这样?

【问题讨论】:

  • 如何将节点添加到场景中?
  • 我创建了一个带有 SKSpriteNode 属性(它是 SKNode 的子节点)的自定义 SKNode(称为 SpriteCar)。在场景的contentCreated 方法中,我使用了[self addChild:self.sCar]; 行。 SpriteCar 也是场景的属性 - 我知道我不必这样做,但我只是在试验。
  • 尝试在 didMoveToView: 中设置 SpriteCar
  • @JuJoDi 是的,我应该提到....createContent 方法在 didMoveToView
  • 必须为上下文提供一些代码。这种猜测无处可去。发布您启动第一个场景的方式和位置以及创建和添加精灵的方式。

标签: ios iphone objective-c sprite-kit


【解决方案1】:

对于其他在将 SpriteNodes 绘制到场景时可能会犯类似错误的人,请查看我的场景源代码,并特别注意//self.sCar.car.position = self.sCar.position;line。

FoolAroundScene.h

#import <SpriteKit/SpriteKit.h>

@interface FoolAroundScene : SKScene

@end

FoolAroundScene.m

#import "FoolAroundScene.h"
#import "ScrollingBackground.h"
#import "SpriteCar.h"

#define BACKGROUND_NAME @"clouds.jpg"



@interface FoolAroundScene ()
@property BOOL contentCreated;
@property (strong, nonatomic) ScrollingBackground * sbg;
@property (strong, nonatomic) SpriteCar * sCar;
@end

@implementation FoolAroundScene



-(void) didMoveToView:(SKView *)view
{
    if (!self.contentCreated) {

        [self createContent];
        self.contentCreated = !self.contentCreated;
    }
}

-(void)createContent
{
    self.sbg = [[ScrollingBackground alloc] initWithBackgroundImage:BACKGROUND_NAME size:self.size speed:2.0];
    self.sCar = [[SpriteCar alloc] init];

    [self addChild:self.sbg];
    [self addChild:self.sCar];
    self.sCar.position = CGPointMake(self.size.width/2, self.size.height/2);
    //self.sCar.car.position = self.sCar.position;

    SKSpriteNode * spriteCar = [self.sCar makeCarSprite];

    spriteCar.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));


    NSLog(@"anchor point : %f %f", self.anchorPoint.x, self.anchorPoint.y);
    NSLog(@"self.size : %f %f", self.size.width, self.size.height);
    NSLog(@"self.scalemode : %d", self.scaleMode);

}

-(void) update:(NSTimeInterval)currentTime
{
    [self.sbg update:currentTime];

}

@end

即使我已将 sCar 的位置属性设置为场景的中间,通过将其子节点(sCar 的汽车属性)位置设置为相同的值,子节点的位置也不会与其父节点的位置相同。这是因为孩子的位置是相对与其父母的。

用另一种方式解释:父级在场景上下文中的位置为 (384,512),而子级在其父级 上下文中的位置为 (384,512)。然而,这意味着孩子在场景上下文中的位置实际上是 (768,1024),这就是汽车被绘制在屏幕右上角的原因。

另外,如果有人想要精灵汽车的实现,就在这里。一辆画得很糟糕的汽车,可用于了解 Sprite Kit 的工作原理。

SpriteCar.h

#import <SpriteKit/SpriteKit.h>

@interface SpriteCar : SKNode
-(id)init;
-(SKSpriteNode *)makeCarSprite;

@end

SpriteCar.m

#import "SpriteCar.h"


@interface SpriteCar ()
@property (nonatomic, strong) SKSpriteNode * car;
@end

@implementation SpriteCar

-(id) init
{
    self = [super init];
    if (self) {
        self.car = [self makeCarSprite];
        [self addChild:self.car];
    }

    return self;
}

-(SKSpriteNode *) makeCarSprite
{
    SKSpriteNode * carBody1 = [[SKSpriteNode alloc] initWithColor:[SKColor redColor] size:CGSizeMake(64.0, 24.0)];
    SKSpriteNode * carBody2 = [[SKSpriteNode alloc] initWithColor:[SKColor redColor] size:CGSizeMake(32.0, 32.0)];

    SKSpriteNode * wheel1 = [[SKSpriteNode alloc] initWithColor:[SKColor blackColor] size:CGSizeMake(8.0, 8.0)];

    SKSpriteNode * wheel2 = [wheel1 copy];


    SKSpriteNode * light = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:CGSizeMake(6.0, 6.0)];


    carBody2.position = carBody1.position;
    wheel1.position = CGPointMake(30.0, -30);
    wheel2.position = CGPointMake(-30.0, -30.0);
    light.position = CGPointMake(32.0, 11.0);

    [carBody1 addChild:carBody2];
    [carBody1 addChild:wheel1];
    [carBody1 addChild:wheel2];
    [carBody1 addChild:light];

    SKAction * hover = [SKAction sequence:@[[SKAction moveByX:0.0 y:5.0 duration:0.1],
                                            [SKAction waitForDuration:0.05],
                                            [SKAction moveByX:0.0 y:-5.0 duration:0.1],
                                            [SKAction waitForDuration:0.05]]];

    [carBody1 runAction:[SKAction repeatActionForever:hover]];




    return carBody1;

}

@end

【讨论】:

  • 生成新项目时示例代码中是这样计算中点的:CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多