使用init(rect:inTexture :)时我的精灵发生了什么?

时间:2015-05-14 02:00:10

标签: macos swift sprite-kit

我正在使用SpriteKit和swift写一个等距游戏,我和我的绵羊精灵有困难。它们有一个基本的AI随机移动它们,但是当它绘制时,我得到两条大的黑色水平线而不是一条羊。我使用init(rect:inTexture :)来获取我的精灵表的第一个子图像。我只做一只羊来测试我的代码。这是我的代码:

//
//  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精灵表看起来像这样: whiteSheep sprite sheet in my Images.xcassets folder

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

黑色的线条随意移动,就像绵羊应该做的那样 - 但是图形会发生什么?怪事。任何人都知道发生了什么?

1 个答案:

答案 0 :(得分:4)

初始化程序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)