osx - What is happening to my sprite when using init(rect:inTexture:)? -
i'm writing isometric game using spritekit , swift, , i'm having trouble sprites sheep. have basic ai randomly moves them around, when draws 2 big black horizontal lines instead of sheep. i'm using init(rect:intexture:) take first sub-image of sprite sheet. i'm doing 1 sheep test code. here's code:
// // animal.swift // anointed // // created jacob jackson on 4/26/15. // copyright (c) 2015 thinkmac innovations. rights reserved. // import foundation import spritekit /* defines animal */ class animal : skspritenode { let animalname: string //name let descript: string //description let spritesheetname: string //image name 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 texture animal's initial state super.init(texture: texture, color: skcolor.clearcolor(), size: texture.size()) //sets tex, bgcolor, , 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) } /* apple. apparently necessary if i'm inheriting skspritenode */ required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
then, create 1 sheep test:
var sheep1 = animal(name: "sheep", desc: "a white sheep", sheetname: "whitesheep") var cavestable = location(n:"cave stable", d:"a small stable inside cave, near inn", g:tempgrid, a:[sheep1]) //uses temporary grid defined above "cave stable" location jesus born
then, place sheep randomly based on array of animals each "location" (like 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 scene code:
override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ thegame.currentlocation.animals[0].updateai(currenttime) /* other stuff below here */
my whitesheep sprite sheet looks this:
finally, looks when game running:
the black lines move randomly, sheep should doing - going on graphics? weirdness. have idea what's going on?
the initialiser rect:intexture:
expects unit coordinates (0-1). 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)
Comments
Post a Comment