- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试向 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
关于ios - Spritekit SKNode 大小百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28768238/
我正在尝试构建一个 SpriteKit 游戏,但它因错误而失败:“无法将模块“SpriteKit”加载为“Spritekit”” 我使用的是 Xcode 版本 6.3.2 (6D2105)。 最佳答案
如何在 spritekit swift 中更改碰撞时的 spritekit。 else if collidedBird.birdType == 6 { if U
我刚刚创建了一个新的 SpriteKit 项目,就遇到了砰!问题。我运行该项目时没有触及任何内容,并且应该如此,默认的 hello world 项目出现了。这方面一切都很好。现在,我创建了一个新的 C
当我使用 SpriteKit 创建视频对象时 let background = SKVideoNode(videoFileNamed: "Video.mov") 我只使用了一次命令: backgrou
出于某种原因,我无法将节点放置在屏幕的顶部或底部。我曾经能够使用 self.frame.height/2 很好地做到这一点,但现在我在景观中工作,这行代码不起作用。注意:我的 anchor 是 0.5
在场景中“缩小”的正确方法是什么。 我有一个物体,我施加一个冲动将它发射到屏幕上。例如,它会发射大约 100 像素,这按预期工作 - 增加它飞行的力量,增加它飞行的密度更少等等。 我遇到的问题是缩放,
是否可以更改粒子发射的频率。例如,如果我发射一个粒子,我可以让它每 5 秒或 10 秒发射一次吗? 我搜索了文档,但找不到任何内容。有解决方法吗?我想做这样的事情: emitter.partic
有谁知道一些开箱即用的库,它们可能会为 SKNode 提供诸如相机抖动之类的效果。 ? 如果没有,是否有一种简单的方法可以使用 Action 来实现相机抖动? 谢谢 最佳答案 我找到了一种使用 SKA
我目前一直在使用委托(delegate)和 SKView 自定义类来监视我的 OS X SpriteKit 游戏中的按键。我需要为我的键盘监控类雇用多个代表,我知道这是不可能的,但可以通过使用观察者来
我从事 SpriteKit 游戏已经有一段时间了,但我有一个无法摆脱的恼人问题。我不想过多介绍这款游戏,但它非常简单。我每秒生成一些对象(SKSpriteNodes),这些对象使用 SpriteKit
我正在制作基于SpriteKit的多级游戏。 一切正常,除了一件事:当用户玩很长时间、改变很多关卡等时......然后 SpriteKit 开始丢失纹理。 我的意思是,没有像图像加载失败时那样的大红叉
我正在尝试将 SKSprite Particle Emitter 与 Swift 一起使用。 但我想在我的发射器中使用许多不同的纹理。 是否有可能:拥有许多图像,并且让发射器随机使用图像,而不是仅使用
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在用SpriteKit构建我的第一个项目,但我无法弄清楚这部分。 我的项目包括飞入屏幕的敌方直升机。每个敌人播放大约14秒长的声音文件。这不是循环。它包括敌人进入屏幕之前接近的声音,以及在屏幕上移
我试图在代码中到处搜索:解释文档在进入后台时会做什么,或者甚至有时会暂停,但无济于事-有人可以指导我在进入后台时建议做什么到启用 Sprite 套件的游戏中的背景吗? 我应该调用scene.pause
我是 SpriteKit 新手。我需要通过 UIImageView 或 SpriteNode 显示图像(它是游戏的背景图像)。但是,我需要用户能够放大背景并平移。我在不使用 SpriteKit 的情况
在我的游戏中,我正在创建一些受重力影响的球。当游戏开始时,他们会从屏幕外掉下来,并在底部找到自己的位置。没有其他力施加在他们身上。 问题是我正在用精确的坐标、精确的物理属性创建这些球,但结果不一样。它
我想让我的宇宙飞船有一条跟随它的粒子轨迹。我该怎么做? 这是我的宇宙飞船的代码: override func didMove(to view: SKView) { spaceS.positio
我正在尝试制作一个动画角色,可以在不移动他的躯干的情况下伸手去拿屏幕上的东西。 如果他够不到,他的手应该朝这个方向移动最大量,然后被他的静态躯干的物理限制所抵消。 所以在我的代码中,绿色矩形( han
我的场景上有player对象,并试图对其施加一些力。您可以看到我正在对update:文件上的MyScene.h方法施加作用力。但是,我的player根本没有动。发生了什么? 下面是Player SKS
我是一名优秀的程序员,十分优秀!