拉伸SKSpriteNodes

时间:2014-02-19 04:42:09

标签: ios objective-c sprite-kit

我的SKSpriteNode SKTexture如下所示。我将节点的centerRect设置为我告诉它向上拉伸的值,这样箭头就不会变形。图片为190x190 px,我正在设置我的centerRect

CGRectMake(90/190, 90/190, 5/190, 5/190);

根据我对docs的理解,这说明每个角都是90x90 px,而3x3网格的中心矩形是5x5 px。这意味着只有中心5x5像素被拉伸。

但我的结果是:

enter image description here

我有一个名为Obstacle的类,它是SKSpriteNode的子类,当它被初始化时,它会将上面的CGRect以及图像名称传递给此方法:

-(void)setTextureNamed:(NSString*)imageName centerRect:(CGRect)rect
{
// set texture for sprite
UIImage* image = [UIImage imageNamed: imageName];

SKTexture* texture = [SKTexture textureWithImage: image];
[self setTexture: texture];

// make stretchable
self.centerRect = rect;

[self setSize: texture.size];
}

在我的SKScene中,当添加Obstacle时,会调用此代码:

int bottomHeight, topHeight, topY, bottomY, max;
bottomY = 0;//_floor.size.height/2;
max = self.view.frame.size.height - _floor.size.height/2 - 2*OBSTACLE_PADDING - 2*OBSTACLE_MIN_HEIGHT;
bottomHeight = arc4random() % max + OBSTACLE_MIN_HEIGHT;
topY = bottomHeight + OBSTACLE_PADDING + _floor.size.height;
topHeight = self.view.frame.size.height - topY;

bottomY += bottomHeight/2;
topY += topHeight/2;

Obstacle *bottom = [[Obstacle alloc] initWithHeight: bottomHeight flipped: NO];
bottom.zPosition = 0;
[bottom setPosition: CGPointMake(400, bottomY)];

NSArray* objs = @[bottom, top];

for (Obstacle* o in objs)
{
    o.zPosition = 2;
    [self addChild: o];
    [o runAction: [SKAction
                        moveToX: -100 duration: SPAWN_SPEED*2]
           completion:^{
               [o removeFromParent];
           }];

}

1 个答案:

答案 0 :(得分:0)

设置SKSpriteNode的比例而不是大小。 [self setScale:...]