使用init(rect:inTexture :)时,我的子画面发生了什么? [英] What is happening to my sprite when using init(rect:inTexture:)?
问题描述
我正在使用SpriteKit和swift编写一个等距游戏,但遇到了我的绵羊的精灵问题.它们具有基本的AI,可以随机移动它们,但是绘制时,我会得到两条大的黑色水平线,而不是一只绵羊.我正在使用init(rect:inTexture :)拍摄我的Sprite工作表的第一个子图像.我只在做一只羊来测试我的代码.这是我的代码:
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 */
我的whiteSheep Sprite工作表如下:
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?
推荐答案
初始化程序rect:inTexture:
期望单位坐标(0-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)
这篇关于使用init(rect:inTexture :)时,我的子画面发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!