What is happening to my sprite when using init(rect:inTexture:)?

China☆狼群 提交于 2019-12-10 11:43:47

问题


I'm writing an isometric game using SpriteKit and swift, and I'm having trouble with sprites for my sheep. They have a basic AI that randomly moves them around, but when it draws I get two big black horizontal lines instead of a sheep. I'm using init(rect:inTexture:) to take the first sub-image of my sprite sheet. I'm only doing one sheep to test my code. Here's my code:

//
//  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")
    }

}

Then, to create the one sheep for a test:

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

Then, to place the sheep randomly based on an array of animals for each "location" (like a level):

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)

        }

Then, in my scene code:

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

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

My whiteSheep sprite sheet looks like this:

Finally, this is what it looks like when the game is running:

The black lines move randomly, like the sheep should be doing - but what is going on with the graphics? Weirdness. Anybody have an idea what's going on?


回答1:


The initialiser rect:inTexture: expects unit coordinates (0-1). So that line of code should be:

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)


来源:https://stackoverflow.com/questions/30228104/what-is-happening-to-my-sprite-when-using-initrectintexture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!