2015-03-31 38 views
2

我有一個SKShapeNodeshapeNodeWithRect: cornerRadius:創建。這個圓角矩形是SKEffectNode的子元素,所以我可以設置shouldRasterize = YESSpriteKit - 更改SKShapeNode大小

我想根據用戶的觸摸更改此節點的寬度。即當他們將手指水平向右移動時,矩形變大(當它們向左移動時,變小)。

  1. 我可以通過用新尺寸替換原來的SKShapeNode來做到這一點(但這是壞的)。
  2. 我試着運行在SKEffectNodeSKShapeNode調整大小動作(但是,這並不因爲工作調整僅適用於SKSpriteNotes [SKAction Apple Docs -- Resize]):

[self runAction:[SKAction resizeToWidth:newSize.width height:newSize.height duration:0]]; [self.shapeNode runAction:[SKAction resizeToWidth:newSize.width height:newSize.height duration:0]];

  • 我可以在這個答案中改變xScale:Change height of an SKShapeNode。但如果我這樣做,SKShapeNode得到像素化。
  • 我該怎麼做?

    這UIKit中那麼容易(只需設置一個UIView的幀)...

    +0

    您是否嘗試過規模? – sangony 2015-03-31 18:15:19

    +0

    @sangony - 我試圖縮放(見我的問題#3)。問題是將節點從小到大縮放會使其像素化。 – rizzes 2015-03-31 18:16:17

    +0

    如果像素化是一個問題,那麼你很難做到這一點。我最近發佈了一個調整處理非常類似問題的SKLabelNode的答案。 http://stackoverflow.com/questions/29354494/how-should-i-resize-label-nodes-in-spritekit/29354830#29354830 – sangony 2015-03-31 18:32:42

    回答

    0

    創建SKShapeNode對象具有大尺寸,然後立即擴大他們失望。如果你這樣做,你不應該遇到模糊或像素化的節點。

    在雨燕4.0:

    class GameScene: SKScene { 
        var roundedRect: SKShapeNode! 
        didMove(to view: SKView) { 
         roundedRect = SKShapeNode(rect: Constants.RoundedRect.largeInitialRect, cornerRadius: Constants.Rect.largeInitialCornerRadius) 
         // configure roundedRect 
         addChild(roundedRect) 
         scaleRoundedRect 
        } 
    
        func scaleRoundedRect(to size: CGSize) { 
         roundedRect.xScale = roundedRect.xScale/frame.width * size.width 
         roundedRect.yScale = roundedRect.yScale/frame.height * size.height 
        } 
    } 
    

    在Objective-C:

    @implementation GameScene 
    
        - (void) didMoveToView:(SKView *)view { 
         CGRect largeRect = CGRectMake(0, 0, 0, 0); // replace with your own values 
         CGFloat largeCornerRadius = (CGFloat) 0; // replace with your value 
         _roundedRect = [SKShapeNode shapeNodeWithRect:largeRect cornerRadius:largeCornerRadius]; 
         CGSize initialSize = CGSizeMake(0, 0); // replace with your value 
         [self scaleRoundedRectToSize:initialSize]; 
        } 
    
        - (void) scaleRoundedRectToSize:(CGSize)size { 
         _roundedRect.xScale = _roundedRect.xScale/_roundedRect.frame.size.width * size.width; 
         _roundedRect.yScale = _roundedRect.yScale/_roundedRect.frame.size.height * size.height; 
        } 
    
    
    @end