macos - 使用 init(rect :inTexture:)?) 时我的 Sprite 发生了什么

标签 macos swift sprite-kit

我正在使用 SpriteKit 和 swift 编写一个等距游戏,但我在为我的绵羊设置 sprite 时遇到了问题。他们有一个基本的 AI,可以随机移动他们,但是当它绘制时,我得到的是两条黑色的大水平线,而不是一只羊。我正在使用 init(rect:inTexture:) 来拍摄我的 Sprite 表的第一张子图像。我只做一只羊来测试我的代码。这是我的代码:

//
//  Animal.swift

//  Anointed
//
//  Created by Jacob Jackson on 4/26/15.
//  Copyright (c) 2015 ThinkMac Innovations. All rights reserved.
//

import Foundation
import SpriteKit

/* DEFINES AN ANIMAL */
class Animal : SKSpriteNode {

    let animalName: String   //name
    let descript: String   //description
    let spriteSheetName: String  //image name for item icon
    var deltaT = 0.0
    var startTime = 0.0

    init( name: String, desc: String, sheetName: String ) {

        animalName = name
        descript = desc
        spriteSheetName = sheetName

        let texture = SKTexture(rect: CGRect(x: 0, y: 0, width: 32, height: 32), inTexture: SKTexture(imageNamed: spriteSheetName)) //make a texture for the animal's initial state
        super.init(texture: texture, color: SKColor.clearColor(), size: texture.size()) //sets up tex, bgcolor, and size

    }

    func updateAI( time: CFTimeInterval ) {

        if startTime == 0.0 {
            startTime = time
        } else {
            deltaT = time - startTime
        }
        if deltaT > 2 {
            moveRandom()
            deltaT = 0.0
            startTime = time
        }

    }

    func moveRandom() {

        var direction = random() % 4
        switch( direction ) {

        case 0: moveUP()
            break
        case 1: moveRT()
            break
        case 2: moveDN()
            break
        case 3: moveLT()
            break
        default:
            break
        }

    }

    func moveUP() {

        let moveUp = SKAction.moveByX(0, y: 64, duration: 1.0)
        self.runAction(moveUp)

    }

    func moveRT() {

        let moveRt = SKAction.moveByX(32, y: 0, duration: 1.0)
        self.runAction(moveRt)

    }

    func moveLT() {

        let moveLt = SKAction.moveByX(-32, y: 0, duration: 1.0)
        self.runAction(moveLt)

    }

    func moveDN() {

        let moveDn = SKAction.moveByX(0, y: -64, duration: 1.0)
        self.runAction(moveDn)

    }

    /* FROM APPLE. APPARENTLY NECESSARY IF I'M INHERITING FROM SKSpriteNode */
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

然后,创建一只羊进行测试:

var sheep1 = Animal(name: "Sheep", desc: "A White Sheep", sheetName: "whiteSheep")
var CaveStable = Location(n:"Cave Stable", d:"A small stable inside a cave, near an Inn", g:tempGrid, a:[sheep1]) //uses temporary grid defined above as the "Cave Stable" location where Jesus is born

然后,根据每个“位置”(如关卡)的一系列动物随机放置绵羊:

for x in 0...theGame.currentLocation.animals.count-1 {

            var animal = theGame.currentLocation.animals[x]
            var pos = twoDToIso(CGPoint(x: (random() % (theGame.currentLocation.grid.count-1))*64, y: (random() % (theGame.currentLocation.grid[0].count-1))*64))
            animal.position = CGPoint(x: pos.x, y: pos.y + animal.size.height / 2)
            world.addChild(animal)

        }

然后,在我的场景代码中:

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

        theGame.currentLocation.animals[0].updateAI(currentTime)
/* other stuff below here */

我的 whiteSheep Sprite 表如下所示: whiteSheep sprite sheet in my Images.xcassets folder

最后,这是游戏运行时的样子: Two black lines instead of a sheep

黑线随机移动,就像绵羊应该做的那样 - 但是图形是怎么回事?奇怪。有人知道发生了什么事吗?

最佳答案

初始化器 rect:inTexture: 需要单位坐标 (0-1)。所以那行代码应该是:

let spriteSheet = SKTexture(imageNamed: spriteSheetName)
let texture = SKTexture(rect: CGRect(x: 0, y: 0, width: 32/spriteSheet.size().width, height: 32/spriteSheet.size().height), inTexture: spriteSheet)

关于macos - 使用 init(rect :inTexture:)?) 时我的 Sprite 发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30228104/

相关文章:

HAXM 的 Android Studio 问题

macos - AV 将* View *内容捕获到电影中

swift - 如何存储 GameScene 以便我可以从游戏内菜单过渡回它?

ios - 有没有办法在 SKNode 对象的 spriteKit 中设置标签/使用标记? (就像在 cocos2d 中一样)

ios - NSInternalInconsistencyException 更新 Tableview

ios - 由于 UILongPress,当我触摸它时 UIButton 未被识别

ruby - 无法安装/编译 asciidoctor-mathematical gem

macos - Mac 应用程序图标在 itunes connect 中不显示

swift - 如何获取两个日期之间每个星期五的日期?

ios - 如何在 Swift 中为 UserDefaults 使用 KVO?