的会员请任何人都可以帮我重新安置一些代码,我知道这个事件可能需要移动,但我不知道在哪里。我已经找到了答案,但我无法看到任何特定于我的问题。Xcode Swift iOS - GameScene没有一个名为
该错误是
'GameScene' 没有一个名为构件 'spawnCrow'
下面是代码(第1行错误)....
var spawn = SKAction.runBlock({() in self.spawnCrow()}) //THIS IS WHERE THE ERROR IS
var delay = SKAction.waitForDuration(NSTimeInterval(2.0))
var spawnThenDelay = SKAction.sequence([spawn, delay])
var spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)
self.addChild(crow)
func spawnCrow() {
请让我知道是否需要更多的代码,我会很乐意分享。
func yourMethod() {
var spawn = SKAction.runBlock({() in self.spawnCrow()})
}
func spawnCrow() {
// do some spawning
}
或删除self
,并继续有定义:
与整个代码
import SpriteKit
class GameScene: SKScene {
var cat = SKSpriteNode()
var crow = SKSpriteNode()
var crowTexture1 = SKTexture()
var skyColor = SKColor()
var moveAndRemoveCrow = SKAction()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0, -4.0)
skyColor = SKColor(red:113.0/255.0, green:197.0/255.0, blue:207.0/255.0, alpha:1.0)
self.backgroundColor = skyColor
var catTexture1 = SKTexture(imageNamed: "Cat1")
catTexture1.filteringMode = SKTextureFilteringMode.Nearest
var catTexture2 = SKTexture(imageNamed: "Cat2")
catTexture2.filteringMode = SKTextureFilteringMode.Nearest
var anim = SKAction.animateWithTextures([catTexture1, catTexture2], timePerFrame: 0.2)
var run = SKAction.repeatActionForever(anim)
cat = SKSpriteNode(texture: catTexture1)
cat.position = CGPoint(x: self.frame.size.width/2.2, y: self.frame.size.height/7.0)
cat.runAction(run)
cat.physicsBody = SKPhysicsBody(circleOfRadius: cat.size.height/2.0)
cat.physicsBody!.dynamic = true
cat.physicsBody!.allowsRotation = false
self.addChild(cat)
// start of crow test
var crowTexture1 = SKTexture(imageNamed: "crow")
crowTexture1.filteringMode = SKTextureFilteringMode.Nearest
crow = SKSpriteNode(texture: crowTexture1)
crow.position = CGPoint(x: self.frame.size.width/1.8, y: self.frame.size.height/1.2)
// crow test 2 spawn
var distanceToMove = CGFloat(self.frame.size.width + 2 * crowTexture1.size().width);
var moveCrow = SKAction.moveByX(-distanceToMove, y:0, duration:NSTimeInterval(0.01 * distanceToMove));
var removeCrow = SKAction.removeFromParent();
moveAndRemoveCrow = SKAction.sequence([moveCrow, removeCrow]);
var spawn = SKAction.runBlock({() in self.spawnCrow()})
var delay = SKAction.waitForDuration(NSTimeInterval(2.0))
var spawnThenDelay = SKAction.sequence([spawn, delay])
var spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)
self.addChild(crow)
// end of crow test
// crow func start
func spawnCrow() {
var crow = SKSpriteNode(texture: crowTexture1)
crow.position = CGPointMake(self.frame.size.width + crowTexture1.size().width * 2, 0);
crow.zPosition = 0; // previous value -10;
var height = UInt32(self.frame.size.height/3)
var y = arc4random() % height;
crow.position = CGPointMake(0.0, CGFloat(y))
crow.physicsBody = SKPhysicsBody(circleOfRadius: 2.0)
crow.physicsBody!.dynamic = false
crow.addChild(crow)
crow.runAction(moveAndRemoveCrow)
crow.addChild(crow)
}
// crow func end
var groundTexture = SKTexture(imageNamed: "Ground")
groundTexture.filteringMode = SKTextureFilteringMode.Nearest
var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width, y: 0, duration: NSTimeInterval(0.01 * groundTexture.size().width))
var resetGroundSprite = SKAction.moveByX(groundTexture.size().width, y: 0, duration: 0.0)
var moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite]))
for var i:CGFloat = 0; i < 2 + self.frame.size.width/(groundTexture.size().width); ++i {
var sprite = SKSpriteNode(texture: groundTexture)
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height/2)
sprite.runAction(moveGroundSpritesForever)
self.addChild(sprite)
}
var dummy = SKNode()
dummy.position = CGPointMake(0, groundTexture.size().height/2)
dummy.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height))
dummy.physicsBody!.dynamic = false
self.addChild(dummy)
var skylineTexture = SKTexture(imageNamed: "Skyline")
skylineTexture.filteringMode = SKTextureFilteringMode.Nearest
var moveSkylineSprite = SKAction.moveByX(-skylineTexture.size().width, y: 0, duration: NSTimeInterval(0.1 * skylineTexture.size().width))
var resetSkylineSprite = SKAction.moveByX(skylineTexture.size().width, y: 0, duration: 0.0)
var moveSkylineSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveSkylineSprite,resetSkylineSprite]))
for var i:CGFloat = 0; i < 2 + self.frame.size.width/(skylineTexture.size().width); ++i {
var sprite = SKSpriteNode(texture: skylineTexture)
sprite.zPosition = -20;
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height/2 + groundTexture.size().height)
sprite.runAction(moveSkylineSpritesForever)
self.addChild(sprite)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
cat.physicsBody!.velocity = CGVectorMake(0, 0)
cat.physicsBody!.applyImpulse(CGVectorMake(0, 15))
}
func clamp(min: CGFloat, max: CGFloat, value: CGFloat) -> CGFloat {
if(value > max) {
return max;
} else if(value < min) {
return min;
} else {
return value;
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
cat.zRotation = self.clamp(-1, max: 0.5, value: cat.physicsBody!.velocity.dy * (cat.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001));
}
}
编译器警告非常简单 - 你的'GameScene'类显然没有一个名为'spawnCrow'的函数。这将有助于获得更多的代码,也许这个函数被定义在整个类上? –
可能只是一个复制/粘贴错误,但你错过了关于'func spanCrow()'的关闭'}',但是否则我在现有的'GameScene'中运行这个代码示例并且没有问题。你可以发布你的文件的其余部分吗? –
@PatrickLynch感谢您的帮助,我已经按要求提供了整个代码,再次感谢! –