Spritekit SKNode 大小百分比

Spritekit SKNode size as a percentage

我正在尝试向 SpriteKit 中的场景添加触摸区。它是透明的,但需要根据设备的大小匹配一定的大小(iPhone 5、6、6+)。我发现如果我想确保可触摸区域保持相同位置并且可触摸区域大小根据设备进行缩放,我需要将 position/size 设置为场景大小的一部分。当我使用:

import SpriteKit
import AVFoundation

class Page1: SKScene {

// MARK: Constant Variables (buttons/background)

let background = SKSpriteNode(imageNamed: "s2.png")
let menuButton = SKSpriteNode(imageNamed: "menuButton.png")
let pageForward = SKSpriteNode(imageNamed: "pageForward.png")
let pageBack = SKSpriteNode(imageNamed: "pageBack.png")
let soundButton = SKSpriteNode(imageNamed: "soundButton.png")
let pressSoundButton = SKSpriteNode(imageNamed: "pressedSoundButton.png")
let pressMenuButton = SKSpriteNode(imageNamed: "pressedMenuButton.png")
let pressPageForward = SKSpriteNode(imageNamed: "pressedPageForward.png")
let pressPageBack = SKSpriteNode(imageNamed: "pressedPageBack.png")

//MARK: Temporary variables

var pageCount = 1
var narrationPlayer: AVAudioPlayer!
var backgroundPlayer: AVAudioPlayer!
var buttonPress: AVAudioPlayer!

//MARK: Nodes

var kittyNode = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 100))
var kittyLabel = "Kitty"
var pepperNode = SKSpriteNode(color: SKColor.yellowColor(), size: CGSizeMake(100, 100))
var pepperLabel = "Pepper"
var groundNode = SKSpriteNode(color: SKColor.brownColor(), size: CGSizeMake(100, 100))
var groundLabel = "The ground"
var bushNode = SKSpriteNode(color: SKColor.greenColor(), size: CGSizeMake(100, 100))
var bushLabel = "The bushes"
var racoonNode = SKSpriteNode(color: SKColor.brownColor(), size: CGSizeMake(100, 100))
var racoonLabel = "Racoon"
var forestNode = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(100, 100))
var forestLabel = "The forest"

// MARK: Initialized classes

var settings = Settings()
var page = PauseMenu()

// MARK: Main code

override func didMoveToView(view: SKView) {
    println("In Page1.swift")

    //narration location
    let path = NSBundle.mainBundle().URLForResource("BATCAT01", withExtension: "mp3")
    narrationPlayer = AVAudioPlayer(contentsOfURL: path, error: nil)

    playBackground()
    playNarration()

    //addming background and default buttons

    background.position = CGPoint(x: size.width/2, y: size.height/2)
    background.size.height = self.size.height/1
    background.size.width = self.size.width/1
    addChild(background)

    menuButton.position = CGPoint(x: size.width-60, y: 60)
    menuButton.size.height = menuButton.size.height/5
    menuButton.size.width = menuButton.size.width/5
    menuButton.alpha = 0.7
    menuButton.zPosition = 10
    addChild(menuButton)

    soundButton.position = CGPoint(x: 60, y: 60)
    soundButton.size.height = soundButton.size.height/5
    soundButton.size.width = soundButton.size.width/5
    soundButton.alpha = 0.7
    soundButton.zPosition = 10
    addChild(soundButton)

    pageForward.position = CGPoint(x: size.width-60, y: size.height-60)
    pageForward.size.height = pageForward.size.height/5
    pageForward.size.width = pageForward.size.width/5
    pageForward.alpha = 0.7
    pageForward.zPosition = 10
    addChild(pageForward)

    pageBack.position = CGPoint(x: 60, y: size.height-60)
    pageBack.size.height = pageBack.size.height/5
    pageBack.size.width = pageBack.size.width/5
    pageBack.alpha = 0.7
    pageBack.zPosition = 10
    //addChild(pageBack)

    //adding pressed buttons and setting alpha

    pressSoundButton.position = CGPoint(x: 60, y: 60)
    pressSoundButton.size.height = pressSoundButton.size.height/5
    pressSoundButton.size.width = pressSoundButton.size.width/5
    pressSoundButton.alpha = 0
    pressSoundButton.zPosition = 11
    addChild(pressSoundButton)

    pressMenuButton.position = CGPoint(x: size.width-60, y: 60)
    pressMenuButton.size.height = pressMenuButton.size.height/5
    pressMenuButton.size.width = pressMenuButton.size.width/5
    pressMenuButton.alpha = 0
    pressMenuButton.zPosition = 11
    addChild(pressMenuButton)

    pressPageForward.position = CGPoint(x: size.width-60, y: size.height-60)
    pressPageForward.size.height = pressPageForward.size.height/5
    pressPageForward.size.width = pressPageForward.size.width/5
    pressPageForward.alpha = 0
    pressPageForward.zPosition = 11
    addChild(pressPageForward)

    pressPageBack.position = CGPoint(x: 60, y: size.height-60)
    pressPageBack.size.height = pressPageBack.size.height/5
    pressPageBack.size.width = pressPageBack.size.width/5
    pressPageBack.alpha = 0
    pressPageBack.zPosition = 11
    //addChild(pressPageBack)

    if settings.checkDimension() == 1 {

        println("Setting up the iPhone touchables")

        kittyNode.zPosition = 11
        kittyNode.alpha = 0.5
        kittyNode.position = CGPoint(x: background.size.width*0.36, y: background.size.height/2) // (155,207) (245,130)
        addChild(kittyNode)

        pepperNode.zPosition = 11
        pepperNode.alpha = 0.5
        pepperNode.position = CGPoint(x: background.size.width*0.61, y: background.size.height*0.32) // (296.0,163.5) (398.5,43.0)
        addChild(pepperNode)

        racoonNode.zPosition = 11
        racoonNode.alpha = 0.5
        racoonNode.position = CGPoint(x: background.size.width*0.84, y: background.size.height*0.535) // (437.0,206.0) (518.0,137.0)
        addChild(racoonNode)

        groundNode.zPosition = 11
        groundNode.alpha = 0.5
        groundNode.position = CGPoint(x: background.size.width/2, y: background.size.height*0.0937) // (1.5,59.5)
        addChild(groundNode)

        bushNode.zPosition = 11
        bushNode.alpha = 0.5
        bushNode.position = CGPoint(x: background.size.width/2, y: background.size.height*0.32) // (1.0,145.5) (568,59.5)
        addChild(bushNode)

        forestNode.zPosition = 11
        forestNode.alpha = 0.5
        forestNode.position = CGPoint(x: background.size.width/2, y: background.size.height*0.727) // (0, 320) (1.0,145.5)
        addChild(forestNode)


    } else if settings.checkDimension() == 2 {

        println("Setting up the iPad touchables")

        //add label nodes
        kittyNode.zPosition = 11
        kittyNode.alpha = 0
        kittyNode.position = CGPoint(x: 360, y: 404.5)
        addChild(kittyNode)

        pepperNode.zPosition = 11
        pepperNode.alpha = 0
        pepperNode.position = CGPoint(x: 627, y: 240)
        addChild(pepperNode)

        groundNode.zPosition = 11
        groundNode.alpha = 0
        groundNode.position = CGPoint(x: self.size.width/2, y: 78.5)
        addChild(groundNode)

        bushNode.zPosition = 11
        bushNode.alpha = 0
        bushNode.position = CGPoint(x: self.size.width/2, y: 261)
        addChild(bushNode)

        racoonNode.zPosition = 11
        racoonNode.alpha = 0
        racoonNode.position = CGPoint(x: 846, y: 413)
        addChild(racoonNode)

        forestNode.zPosition = 11
        forestNode.alpha = 0
        forestNode.position = CGPoint(x: self.size.width/2, y: 564)
        addChild(forestNode)
    }
}

// MARK: Touch handling

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        var location = touch.locationInNode(self)
        println("\(location)")

        //checks if someone touched the menu button
        if menuButton.containsPoint(location) {
            println("Play story with sound!")
            menuButton.alpha = 0
            pressMenuButton.alpha = 0.7
            settings.setInProgress()
        }

        if soundButton.containsPoint(location) {
            println("Play story with sound!")
            soundButton.alpha = 0
            pressSoundButton.alpha = 0.7
            settings.setInProgress()
        }

        //checks if someone touched the forward button
        if pageForward.containsPoint(location) {
            println("Next Page!")
            pageForward.alpha = 0
            pressPageForward.alpha = 0.7
            settings.setInProgress()
        }

        if pageBack.containsPoint(location) {
            println("Next Page!")
            pageBack.alpha = 0
            pressPageBack.alpha = 0.7
            settings.setInProgress()
        }
    }
}

//MARK: functions when touches end

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touch:AnyObject in touches {
        var location = touch.locationInNode(self)
        println("touch ended at \(location)")

        //user touch error checking
        soundButton.alpha = 0.7
        pressSoundButton.alpha = 0
        menuButton.alpha = 0.7
        pressMenuButton.alpha = 0
        pageForward.alpha = 0.7
        pressPageForward.alpha = 0
        pageBack.alpha = 0.7
        pressPageBack.alpha = 0

        if soundButton.containsPoint(location) { //button code starts
            println("touch ended in sound button")
            soundButton.alpha = 0.7
            pressSoundButton.alpha = 0
            playNarration()
        } else if menuButton.containsPoint(location) {
            println("touch ended in menuButton")
            menuButton.alpha = 0.7
            pressMenuButton.alpha = 0
            goToPauseMenu()
        } else if pageForward.containsPoint(location) {
            println("touch ended in sound button")
            pageForward.alpha = 0.7
            pressPageForward.alpha = 0
            nextPage()
        } else if pageBack.containsPoint(location) {
            println("touch ended in page back button")
            pageBack.alpha = 0.7
            pressPageBack.alpha = 0
            //lastPage()
        } else if kittyNode.containsPoint(location) { //label code starts
            labelActions(kittyLabel, target: location)
        } else if pepperNode.containsPoint(location) {
            labelActions(pepperLabel, target: location)
        } else if groundNode.containsPoint(location) {
            labelActions(groundLabel, target: location)
        } else if bushNode.containsPoint(location) {
            labelActions(bushLabel, target: location)
        } else if racoonNode.containsPoint(location) {
            labelActions(racoonLabel, target: location)
        } else if forestNode.containsPoint(location) {
            labelActions(forestLabel, target: location)
        }
    }
}

// MARK: Button actions

func playNarration() {
    //narration location
    let path = NSBundle.mainBundle().URLForResource("BATCAT01", withExtension: "mp3")
    narrationPlayer = AVAudioPlayer(contentsOfURL: path, error: nil)
    narrationPlayer.stop()
    narrationPlayer.prepareToPlay()
    narrationPlayer.play()
}

func playBackground() {
    //background location
    let path = NSBundle.mainBundle().URLForResource("BatKitty_Innocence.v1", withExtension: "wav")
    backgroundPlayer = AVAudioPlayer(contentsOfURL: path, error: nil)
    backgroundPlayer.stop()
    backgroundPlayer.prepareToPlay()
    backgroundPlayer.numberOfLoops = -1
    backgroundPlayer.play()
}

func goToPauseMenu() {
    var scene = PauseMenu(size: self.size)
    page.setPage(pageCount)
    let skView = self.view as SKView!
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .ResizeFill
    scene.size = skView.bounds.size
    let sceneTransition = SKTransition.revealWithDirection(SKTransitionDirection.Up, duration: 1.3)
    skView.presentScene(scene, transition: sceneTransition)

    let effectPath = NSBundle.mainBundle().URLForResource("Soft Pulsing Accent", withExtension: "mp3")
    buttonPress = AVAudioPlayer(contentsOfURL: effectPath, error: nil)
    buttonPress.prepareToPlay()
    buttonPress.play()
}

func nextPage() {
    narrationPlayer.stop()
    var scene = Page2(size: self.size)
    let skView = self.view as SKView!
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .ResizeFill
    scene.size = skView.bounds.size
    let sceneTransition = SKTransition.revealWithDirection(SKTransitionDirection.Left, duration: 1.3)
    skView.presentScene(scene, transition: sceneTransition)

    let effectPath = NSBundle.mainBundle().URLForResource("Soft Pulsing Accent", withExtension: "mp3")
    buttonPress = AVAudioPlayer(contentsOfURL: effectPath, error: nil)
    buttonPress.prepareToPlay()
    buttonPress.play()
}

func lastPage() {
    var scene = Page1(size: self.size)
    let skView = self.view as SKView!
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .ResizeFill
    scene.size = skView.bounds.size
    let sceneTransition = SKTransition.revealWithDirection(SKTransitionDirection.Right, duration: 1.3)
    skView.presentScene(scene, transition: sceneTransition)

    let effectPath = NSBundle.mainBundle().URLForResource("Soft Pulsing Accent", withExtension: "mp3")
    buttonPress = AVAudioPlayer(contentsOfURL: effectPath, error: nil)
    buttonPress.prepareToPlay()
    buttonPress.play()
}

//MARK: Label Triggers

func labelActions(text:String, target:CGPoint) {

    var dice = arc4random_uniform(4)
    var tempText = SKLabelNode(text: text)
    tempText.fontName = "ChalkboardSE-Bold"
    tempText.fontColor = SKColor(red: 0, green: 1, blue: 1, alpha: 1)
    var tempTarget = target

    switch dice {
    case 0:
        println("dice rolled a 0")
        tempText.zRotation = 0.3
    case 1:
        tempText.zRotation = -0.3
        println("dice rolled a 1")
    case 2:
        tempText.zRotation = 0.15
        println("dice rolled a 2")
    case 3:
        tempText.zRotation = -0.15
        println("dice rolled a 3")
    default:
        println("default angle")
    }
    tempText.position = target
    var actions = [SKAction.scaleTo(2, duration: 0.5),
                   SKAction.waitForDuration(3),
                   SKAction.scaleTo(0, duration: 0.5),
                   SKAction.removeFromParent()]
    tempText.runAction(SKAction.sequence(actions))

    addChild(tempText)
}

}

理想情况下,我想将宽度设置为 self.size.width*0.25。因此,如果设备宽度为 100,则节点宽度将为 25。当我这样做时,它不起作用。我收到错误 Page1 -> 0 -> Page1!'没有名为“size

的成员

任何人都知道出了什么问题以及我该如何解决这个问题?

您需要访问场景的 frame。您可以使用 self.frame.size 来做到这一点。例如:

var width:CGFloat = self.frame.size.width/4
var height:CGFloat = self.frame.size.height/4