作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图找出是什么原因导致此 Tile_xxxxx 名称在此代码中成为某种长随机数。
SKTexture: Error loading image resource: "Tile_-4412407809"
与之相关的代码在本教程中:https://www.raywenderlich.com/54-how-to-make-a-game-like-candy-crush-with-spritekit-and-swift-part-2
Tile 名称在 swift 文件 GameScene.swift 第 144 行中设置。
let name = String(format: "Tile_%ld", value)
如果我将文件中的代码放在这里可能是最好的。有些东西正在将“值(value)”设置为它无法使用的东西。但什么?从哪里获取 hashValue
var value = topLeft.hashValue
import SpriteKit
import GameplayKit
class GameScene: SKScene {
// Sound FX
let swapSound = SKAction.playSoundFileNamed("Chomp.wav", waitForCompletion: false)
let invalidSwapSound = SKAction.playSoundFileNamed("Error.wav", waitForCompletion: false)
let matchSound = SKAction.playSoundFileNamed("Ka-Ching.wav", waitForCompletion: false)
let fallingCookieSound = SKAction.playSoundFileNamed("Scrape.wav", waitForCompletion: false)
let addCookieSound = SKAction.playSoundFileNamed("Drip.wav", waitForCompletion: false)
var level: Level!
let tilesLayer = SKNode()
let cropLayer = SKCropNode()
let maskLayer = SKNode()
let tileWidth: CGFloat = 32.0
let tileHeight: CGFloat = 36.0
let gameLayer = SKNode()
let cookiesLayer = SKNode()
var swipeHandler: ((Swap) -> Void)?
private var swipeFromColumn: Int?
private var swipeFromRow: Int?
private var selectionSprite = SKSpriteNode()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder) is not used in this app")
}
override init(size: CGSize) {
super.init(size: size)
anchorPoint = CGPoint(x: 0.5, y: 0.5)
// This is the Witches background image
let background = SKSpriteNode(imageNamed: "Background")
background.size = size
addChild(background)
addChild(gameLayer)
gameLayer.isHidden = true
let layerPosition = CGPoint(
x: -tileWidth * CGFloat(numColumns) / 2,
y: -tileHeight * CGFloat(numRows) / 2)
tilesLayer.position = layerPosition
maskLayer.position = layerPosition
cropLayer.maskNode = maskLayer
gameLayer.addChild(tilesLayer)
gameLayer.addChild(cropLayer)
cookiesLayer.position = layerPosition
cropLayer.addChild(cookiesLayer)
let _ = SKLabelNode(fontNamed: "GillSans-BoldItalic")
}
func addSprites(for cookies: Set<Cookie>) {
for cookie in cookies {
let sprite = SKSpriteNode(imageNamed: cookie.cookieType.spriteName)
sprite.size = CGSize(width: tileWidth, height: tileHeight)
sprite.position = pointFor(column: cookie.column, row: cookie.row)
cookiesLayer.addChild(sprite)
cookie.sprite = sprite
// Give each cookie sprite a small, random delay. Then fade them in.
sprite.alpha = 0
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.run(
SKAction.sequence([
SKAction.wait(forDuration: 0.25, withRange: 0.5),
SKAction.group([
SKAction.fadeIn(withDuration: 0.25),
SKAction.scale(to: 1.0, duration: 0.25)
])
]))
}
}
func addTiles() {
for row in 0..<numRows {
for column in 0..<numColumns {
if level.tileAt(column: column, row: row) != nil {
let tileNode = SKSpriteNode(imageNamed: "MaskTile")
tileNode.size = CGSize(width: tileWidth, height: tileHeight)
tileNode.position = pointFor(column: column, row: row)
maskLayer.addChild(tileNode)
}
}
}
for row in 0...numRows {
for column in 0...numColumns {
let topLeft = (column > 0) && (row < numRows)
&& level.tileAt(column: column - 1, row: row) != nil
let bottomLeft = (column > 0) && (row > 0)
&& level.tileAt(column: column - 1, row: row - 1) != nil
let topRight = (column < numColumns) && (row < numRows)
&& level.tileAt(column: column, row: row) != nil
let bottomRight = (column < numColumns) && (row > 0)
&& level.tileAt(column: column, row: row - 1) != nil
var value = topLeft.hashValue
value = value | topRight.hashValue << 1
value = value | bottomLeft.hashValue << 2
value = value | bottomRight.hashValue << 3
// Values 0 (no tiles), 6 and 9 (two opposite tiles) are not drawn.
if value != 0 && value != 6 && value != 9 {
let name = String(format: "Tile_%ld", value)
//print(name)
let tileNode = SKSpriteNode(imageNamed: name)
tileNode.size = CGSize(width: tileWidth, height: tileHeight)
var point = pointFor(column: column, row: row)
point.x -= tileWidth / 2
point.y -= tileHeight / 2
tileNode.position = point
tilesLayer.addChild(tileNode)
}
}
}
}
最佳答案
已修复。
改变
var value = topLeft.hashable
value = value | topRight.hashable << 1
value = value | bottomLeft.hashable << 2
value = value | bottomRight.hashable << 3
to
var value = (topLeft ? 1 : 0)
value = value | (topRight ? 1 : 0) << 1
value = value | (bottomLeft ? 1 : 0) << 2
value = value | (bottomRight ? 1 : 0) << 3
关于swift - SK纹理 : Error loading image resource: "Tile_-4412407809",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59547100/
我试图找出是什么原因导致此 Tile_xxxxx 名称在此代码中成为某种长随机数。 SKTexture: Error loading image resource: "Tile_-4412407809
我是一名优秀的程序员,十分优秀!