- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题,为什么在下面的代码中我的 SpriteKit 节点没有从我的父节点中删除。
我的项目目前分为两部分,一个是构建 SpriteKit 项目时默认的 GameScene 类,另一个是我制作的 Circles 类,用于对游戏中的每个圆圈进行操作。
圆圈在这里存储在一个数组中:
var circlesInPlay = [Circles]()
基本上,我正在尝试设计一个简单的游戏,其中圆圈的大小会减小,当它们的宽度为 0 时,会从屏幕上移除。
我的代码在这里,在
override func update(currentTime: CFTimeInterval) {
timeSinceUpdate = timeSinceUpdate + 1
print("time since update: " + String(timeSinceUpdate))
if timeForUpdate == timeSinceUpdate {
newCircle()
timeSinceUpdate = 0
//timeForUpdate = Int(arc4random_uniform(100) + 1)
timeForUpdate = 100
}
//Check to see if circle transparancies are 0
if checkGameOver() {
gameOver()
}
updateCircles()
removeCircles()
timeSinceUpdate 变量是自最后一个圆圈放置到屏幕上以来的时间,每更新一帧该时间就增加一个。
updateCircles() 调用它,
func updateCircles() {
for c in circlesInPlay {
c.update()
}
}
我在另一个 swift 文件中创建的 Circles 类中调用了 update():
func update() {
transparancy = transparancy - transparancyDecrease
size = size - ds
if (size <= 0) {
node.removeFromParent()
}
node.size.height = size
node.size.width = size
}
更新函数对 removeCircles() 的另一个调用在这里
func removeCircles() {
var posn = circlesInPlay.count - 1
for c in circlesInPlay {
if (c.size < 0) {
c.produceNode().removeFromParent()
circlesInPlay.removeAtIndex(posn)
posn = posn - 1
}
}
}
我真正想知道的是,我不知道为什么圆圈有时会卡住或没有从屏幕上移除。
非常感谢任何帮助!
整个游戏代码如下:
import SpriteKit
class GameScene: SKScene {
var bgImage = SKSpriteNode(imageNamed: "background.png")
let screenSize = UIScreen.mainScreen().bounds
var timeSinceUpdate: Int = 0
var circlesInPlay = [Circles]()
var timeForUpdate = Int(arc4random_uniform(200) + 1)
override func didMoveToView(view: SKView) {
bgImage.position = CGPointMake(self.size.width/2, self.size.height/2)
bgImage.zPosition = -100
self.addChild(bgImage)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
if !isTouchInCircle(location) {
gameOver()
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
timeSinceUpdate = timeSinceUpdate + 1
print("time since update: " + String(timeSinceUpdate))
if timeForUpdate == timeSinceUpdate {
newCircle()
timeSinceUpdate = 0
//timeForUpdate = Int(arc4random_uniform(100) + 1)
timeForUpdate = 100
}
//Check to see if circle transparancies are 0
if checkGameOver() {
gameOver()
}
updateCircles()
removeCircles()
}
func newCircle(){
let sizeX = UInt32(CGRectGetMaxX(self.frame))
let randomx = CGFloat(arc4random_uniform(sizeX))
let sizeY = UInt32(CGRectGetMaxY(self.frame))
let randomy = CGFloat(arc4random_uniform(sizeY))
//let randomx = CGFloat(arc4random_uniform(UInt32(self.size.width)))
//let randomy = CGFloat(arc4random_uniform(UInt32(self.size.height)))
if (circlesInPlay.count < 5) {
circlesInPlay.append(Circles.init(x: randomx, y: randomy))
self.addChild((circlesInPlay[circlesInPlay.count - 1]).produceNode())
}
}
func checkGameOver() -> Bool {
for c in circlesInPlay {
if c.getTransparancy() == 0 {
return false
}
}
return true
}
func isTouchInCircle(touch: CGPoint) -> Bool {
for c in circlesInPlay {
if (c.getX() <= (touch.x * 1.10)) {
return true
}
}
return false
}
func updateCircles() {
for c in circlesInPlay {
c.update()
}
}
func removeCircles() {
var posn = circlesInPlay.count - 1
for c in circlesInPlay {
if (c.size < 0) {
c.produceNode().removeFromParent()
circlesInPlay.removeAtIndex(posn)
posn = posn - 1
}
}
}
func gameOver() {
}
}
import Foundation
import SpriteKit
import GameKit
class Circles {
var node = SKSpriteNode()
var size: CGFloat = 200
var ds: CGFloat = 2
var colorC: String;
var xpos: CGFloat
var ypos: CGFloat
var transparancy: Int
var transparancyDecrease: Int = 1
let arrayColors = ["circleRed.png", "circleBlue.png", "circlePink.png", "circleGreen.png", "circleYellow.png"]
//The innitial constructor
init(x: CGFloat, y: CGFloat) {
self.xpos = x
self.ypos = y
self.transparancy = 100
let randomIndex = Int(arc4random_uniform(UInt32(arrayColors.count)))
colorC = arrayColors[randomIndex]
node.texture = SKTexture(imageNamed: colorC)
node.position = CGPointMake(self.xpos, self.ypos)
node.size.height = self.size
node.size.width = self.size
}
func produceNode() -> SKSpriteNode {
return node
}
func setColor() {
let randomIndex = Int(arc4random_uniform(UInt32(arrayColors.count)))
colorC = arrayColors[randomIndex]
}
func update() {
transparancy = transparancy - transparancyDecrease
size = size - ds
if (size <= 0) {
node.removeFromParent()
}
node.size.height = size
node.size.width = size
}
func getX() -> CGFloat {
return xpos
}
func getY() -> CGFloat {
return ypos
}
/*
func getColor() -> SKColor {
return colorC
}*/
func getTransparancy() -> Int {
return transparancy
}
}
最佳答案
问题出在这段代码中:
var posn = circlesInPlay.count - 1
for c in circlesInPlay {
if (c.size < 0) {
c.produceNode().removeFromParent()
circlesInPlay.removeAtIndex(posn)
posn = posn - 1
}
}
你总是从你的集合中移除最后一个元素,而不是你在迭代中使用的元素。在迭代时更改集合时也要小心。如果您从集合中删除元素,索引将会更改。
关于ios - SpriteKit 节点未从父节点中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940560/
我正在尝试构建一个 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
我是一名优秀的程序员,十分优秀!