SKShapeNode fillTexture有时无法使用Swift

时间:2015-05-04 22:11:39

标签: swift ios8 sprite-kit

我试图通过带纹理的点填充一个简单的SKShapeNode方格。它应该非常简单,因为您现在可以将.fillTexture属性设置为您选择的SKTexture

但是,我的结果确实不一致,并且纹理实际显示的时间很短。然后我会尝试更改为另一个纹理,并且新纹理不会显示。看起来正方形的大小也会影响纹理是否显示......?

  

GameScene.swift

import SpriteKit

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        // Set background
        let background = SKSpriteNode(imageNamed: "background")
        background.position = CGPoint(x: 0, y: 0)
        background.anchorPoint = CGPoint(x: 0, y: 0)
        addChild(background)

        // Record scene size
        let w = self.size.width
        let h = self.size.height

        // Screen center
        let center = CGPoint(x: w/2, y: h/2)

        // Make points
        let squarePoints = UnsafeMutablePointer<CGPoint>.alloc(5)
        squarePoints[0] = CGPoint(x: -40, y: -40)
        squarePoints[1] = CGPoint(x:  40, y: -40)
        squarePoints[2] = CGPoint(x:  40, y:  40)
        squarePoints[3] = CGPoint(x: -40, y:  40)
        squarePoints[4] = CGPoint(x: -40, y: -40)


        // Make the SKShapeNode
        var square = SKShapeNode(points: squarePoints, count: 5)
        square.strokeColor = UIColor.whiteColor()
        square.lineWidth = 2.0
        square.position = center
        square.fillTexture = SKTexture(imageNamed: "square")
        addChild(square)

        // Define an action
        var action = SKAction.rotateByAngle(-1.0, duration: 1.0)
        square.runAction(SKAction.repeatActionForever(action))

        //
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}
  

GameViewController.swift

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = GameScene(size: view.bounds.size)
        let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        scene.scaleMode = .ResizeFill
        skView.presentScene(scene)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

我的代码中没有其他内容。结果是旋转的空白色方块,背景图像顶部没有填充。我发布了截图,但我还不能。

2 个答案:

答案 0 :(得分:1)

您是否尝试过离开最后一点并查看它是否会自动连接到原点?

答案 1 :(得分:0)

尝试使用

SKTexture(imageNamed: "square.png")

或您正在使用的任何图像类型