gpt4 book ai didi

ios - 我的 Sprite Kit 按钮在 Swift 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 06:24:12 25 4
gpt4 key购买 nike

你好,看到这个我想知道为什么我在 swift sprite 套件中的代码无法运行我可以运行它但是当我单击按钮时它不起作用(它不会进入现场)但是当我在 if 语句中添加了一个 println,然后单击它,它确实运行了 println。提前致谢!

这是我第一个场景的代码:

    //
// GameScene.swift
// Pocket Rocket
//
// Created by Lucas Farleigh on 11/11/2014.
// Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//

import SpriteKit;

class GameScene: SKScene {

let background = SKSpriteNode(imageNamed:"background")
let playButton = SKSpriteNode(imageNamed: "playbutton")
override func didMoveToView(view: SKView) {
playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame))
background.yScale = 2
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame))
self.addChild(background)
self.addChild(playButton)

}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
//making the scene vars
var scene = PlayScene(size: self.size)


for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
let skview = self.view as SKView!
scene.size = skview.bounds.size
playButton.name = "PB"


if node.name == "PB"{
println("it worked")
skview.presentScene(scene)
}


}
}

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

还有我的第二个场景(playscene)

    //
// PlayScene.swift
// Pocket Rocket
//
// Created by Lucas Farleigh on 14/11/2014.
// Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//

import SpriteKit
import UIKit
import Foundation

class PlayScene:SKScene{
let background = SKSpriteNode(imageNamed: "background")

let bar1 = SKSpriteNode(imageNamed:"Bar1")


let bar2 = SKSpriteNode(imageNamed:"Bar2")

let bar3 = SKSpriteNode(imageNamed:"Bar3")


let bar4 = SKSpriteNode(imageNamed:"Bar4")

let bar5 = SKSpriteNode(imageNamed:"Bar5")

let bar6 = SKSpriteNode(imageNamed:"Bar6")


override func didMoveToView(view: SKView) {
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame))
addChild(background)
background.yScale = 2

最佳答案

请注意,您正在创建 backgroundPlayButton 节点两次:一次作为类属性,第二次作为 didMoveToView 中的局部变量>。它是您随后作为 subview 添加到场景中的本地版本。如果您希望能够在类中的其他方法中针对这些节点进行测试,请去掉 didMoveToView 中的本地声明。您仍然可以填写有关这些节点的其他信息,只需删除 let 语句即可。

然后在您的 touchesBegan 中,您希望通过将 self 传递给 touch.locationInNode(self) 来获取触摸在场景中的位置然后检索对应于该触摸的节点:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node == Play_Button {
println("Hello!!!!")
}
}
}

实现 PlayButton 的第二种方法是在创建节点时为其命名:

Play_Button.name = "playButton"

然后您的 touchesBegan 将如下所示:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node.name == "playButton" {
println("Hello!!!!")
}
}
}

关于ios - 我的 Sprite Kit 按钮在 Swift 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26780248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com