2014-04-20 35 views
0

如何在場景中設置SKLabel的位置?在場景中爲一個SKLabel設置動畫效果?

我試過以下,但它似乎並不奏效:

[SKView animateWithDuration:0.3 
            delay:0.0 
           options:UIViewAnimationOptionCurveEaseOut 
          animations:^{ 
           newBestLabel.position = CGPointMake(CGRectGetMinX(self.frame)+70, CGRectGetMaxY(self.frame)-30); 
          } 
          completion:^(BOOL finished){}]; 

[UIView animateWithDuration:0.3 
            delay:0.0 
           options:UIViewAnimationOptionCurveEaseOut 
          animations:^{ 
           newBestLabel.position = CGPointMake(CGRectGetMinX(self.frame)+70, CGRectGetMaxY(self.frame)-30); 
          } 
          completion:^(BOOL finished){}]; 

在viewDidLoad中它開始於:

newBestLabel.position = CGPointMake(CGRectGetMinX(self.frame)+70, CGRectGetMaxY(self.frame)+30); 

什麼問題這裏?

回答

3

因爲SKLabelNode是一個子類的SKNode你可以使用一個名爲runAction方法:和傳遞一個SKAction,做你所需要的。

// create an instance of SKAction 
SKAction *moveLabel = [SKAction moveByX:0.0 y:30.0 duration:1.2]; 

// tell labelNode to run the action 
[newBestLabel runAction:moveLabel]; 

另外值得一提的是,座標系SpriteKit用途,是到的UIKit的不同。因此,在上面的代碼中,正x值將向右移動,正y值將向上移動!

有許多方法可以做你需要什麼,更多的事情,並在SKAction Class Reference

+0

大解釋,正是我一直在尋找被發現! – KingPolygon

3

SKLabelNode不會從UIView繼承,這不是在SpriteKit中處理動畫的方式。相反,你應該通過創建一個SKAction並將其應用到節點處理這個問題:

SKLabelNode *label = [SKLabelNode labelNodeWithFontNamed:@"Avenir"]; 
label.position = CGPointMake(30, 200); 
label.text = @"Lorem Ipsum"; 
[self addChild:label]; 

SKAction *moveLabel = [SKAction moveByX:100 y:0 duration:2.0]; 
[label runAction:moveLabel]; 

不相同的座標在你的代碼,但我敢肯定,你可以把它從這裏開始。 如果更適合您的需求,還有一個moveTo:action。

相關問題