Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Spritekit节点大小(以百分比表示)_Ios_Swift_Sprite Kit_Size_Sprite - Fatal编程技术网

Ios Spritekit节点大小(以百分比表示)

Ios Spritekit节点大小(以百分比表示),ios,swift,sprite-kit,size,sprite,Ios,Swift,Sprite Kit,Size,Sprite,我正在尝试在SpriteKit中为场景添加触摸区域。它是透明的,但需要根据设备的大小(iPhone 5、6、6+)匹配特定的大小。我发现,如果我想确保可触摸区域保持相同的位置,并且可触摸区域大小根据设备的不同而缩放,我需要将位置/大小设置为场景大小的一部分。当我使用: import SpriteKit import AVFoundation class Page1: SKScene { // MARK: Constant Variables (buttons/background) let

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

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”的成员


有人知道出了什么问题以及我如何解决吗?

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

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

您的问题是有时无法访问self.size?你试过self.frame.size吗?你好,谢谢回答。当我输入它时,我得到Page1->0->Page1!'没有名为“frame
Page1->0->Page1”的成员是错误消息吗?它是在写行时出现还是在执行时出现?如果可以的话,你能给我们提供更详细的代码和更详细的错误日志吗?您可以编辑您的问题以添加这些。您好,谢谢您的回答。当我输入它时,我得到Page1->0->Page1!'没有名为“framework”的成员,但您正在尝试在函数中设置它?你是“SKScene”的子类吗?嗨,我编辑了这篇文章,加入了更多的代码。让我知道这是否有帮助。也许是一个愚蠢的问题,但你是否关闭了方法和类?嗨,是的,很抱歉我这么做了。代码很长,我不想全部发布