2015-03-02 46 views
1

縮放我有一個瓷磚類,這是SKShapeNodeSKShapeNode設置SKAction從中心

@interface Tile : SKShapeNode 

的子類,我實例化類,如下所示:

// Tile.h 
-(instancetype) addHiddenTileToSpace:(NSString *)letter withIndex:(int)i withPositionInWord:(int)p; 

我在執行文件我有

// Tile.m 
-(instancetype) addHiddenTileToSpace:(NSString *)letter withIndex:(int)i withPositionInWord:(int)p{ 
if(self == [super init]){ 
    self.name = @"Tile"; 
    // self.hidden = YES; 
    if (p == 0) { 
     self.firstLetter = YES; 
    } 
    else{ 
     self.firstLetter = NO; 
    } 
    self.positionInAnswer = i; 
    self.character = letter; 

    [self setPath:CGPathCreateWithRoundedRect(CGRectMake(0, 0, 60, 60), 4, 4, nil)]; 
    self.strokeColor = self.fillColor = [UIColor colorWithRed:66.0f/255.0f 
                 green:74.0f/255.0f 
                 blue:84.0f/255.0f 
                 alpha:1]; 
    SKLabelNode *characterNode = [[SKLabelNode alloc] initWithFontNamed:@"Avenir-Medium"]; 
    characterNode.text = [letter uppercaseString]; 
    characterNode.fontSize = 40; 
    characterNode.fontColor = [UIColor colorWithRed:216.0f/255.0f 
               green:216.0f/255.0f 
               blue:216.0f/255.0f 
               alpha:1]; 
    characterNode.position = CGPointMake(30, 15); 
    [self addChild:characterNode]; 
} 
return self; 

}

問題是我想動畫/縮放該節點,但它從節點的座標(0,0)縮放而不是從其中心縮放。我已經在其他地方看過我應該使用+ (instancetype)shapeNodeWithPath:(CGPathRef)path centered:(BOOL)centered方法並將中心設置爲YES

但是,在上面的實例中,我不確定如何使用shapeNodeWithPath方法初始化SKSHapeNode。或者是否有其他方式可以設置Tile.center = YES;

+0

你爲什麼使用SKShapeNode作爲簡單的瓷磚? – sangony 2015-03-02 18:45:28

回答

0

我認爲你繪製你的形狀將解決你的問題。您正在繪製從0,0這將是節點的中心,它看起來像你正在定位標籤也是爲了彌補這一點。像這樣的東西可能會有所幫助。

... 
[self setPath:CGPathCreateWithRoundedRect(CGRectMake(-30, -30, 60, 60), 4, 4, nil)]; 
//or it could be [self setPath:CGPathCreateWithRoundedRect(CGRectMake(-30, 30, 60, 60), 4, 4, nil)]; 
... 
characterNode.position = CGPointMake(0, 15); 
... 

現在,當你規模的一切實際上應該是在節點的中心,而不是關閉的30.我沒有測試這個理論偏差,但它應該修正定位和縮放的問題。

祝你好運,希望有所幫助。