gpt4 book ai didi

ios - 为什么 funcmotionEnded 在 GameScene 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 01:46:15 25 4
gpt4 key购买 nike

我在一个游戏上有一个标题屏幕,我正在尝试学习制作,并且想要添加一个响应用户摇动的功能。该功能在独立放置时有效,但在此页面上无效。我所说的不工作是指它似乎什么也没做(甚至不会打印到日志中)。

相关函数是motionEnded func。

import SpriteKit
import AVFoundation
import iAd

class GameScene: SKScene {

//loud soundfiles
var titleMusic = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Loping Sting", ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()

let playButton = SKSpriteNode(imageNamed: "PlayNow")
let titleScreen = SKSpriteNode(imageNamed: "Title")
let titleOrange = SKSpriteNode(imageNamed: "hero")

override func didMoveToView(view: SKView) {

//stop music from game
musicPlayer.stop()

//play sound file
audioPlayer = AVAudioPlayer(contentsOfURL: titleMusic, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()

self.titleScreen.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 100)
self.addChild(self.titleScreen)
self.addChild(self.playButton)

//animate button
let pulseUp = SKAction.scaleTo(1.1, duration: 0.9)
let pulseDown = SKAction.scaleTo(0.7, duration: 0.9)
let pulse = SKAction.sequence([pulseDown, pulseUp])
let repeatPulse = SKAction.repeatActionForever(pulse)
self.playButton.runAction(repeatPulse)

//show hero
titleOrange.position = CGPointMake(CGRectGetMidX(self.frame), 363)
self.addChild(titleOrange)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.playButton {
var scene = PlayScene(size: self.size)
let skView = self.view as SKView?
scene.scaleMode = .ResizeFill
scene.size = skView!.bounds.size
skView?.presentScene(scene)
}
}
}

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if motion == .MotionShake {
println("Shake")
}
}

override func update(currentTime: CFTimeInterval) {
}
}

最佳答案

因为您之前已经让它工作了,我假设您已将 applicationSupportsShakeToEdit 设置为 true(无论如何这是默认设置)。

然后您需要确保您的场景 GameScene 成为第一响应者 - 第一响应者将(顾名思义)是第一个有机会响应运动事件的人,之后它向上发展 responder chain . SKScene 符合UIResponder 协议(protocol),所以你只需要通过覆盖一个方法让它成为第一响应者,然后在场景出现时调用几个响应者方法/消失:

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

override func didMoveToView(view: SKView) {
// ...
becomeFirstResponder()
}

override func willMoveFromView(view: SKView) {
// ...
resignFirstResponder()
}

请注意,让您的场景成为第一响应者可能会影响您应用中其他地方的其他事件处理代码,具体取决于您应用的结构。

关于ios - 为什么 funcmotionEnded 在 GameScene 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31902375/

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