请任何人帮我重新安置一些代码,我知道该事件可能需要移动,但我不知道在哪里。我已经找到了答案,但我看不出任何具体问题。
错误是
'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() {
如果需要更多代码,请告诉我,我很乐意与您分享。
使用完整代码更新
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 ) );
}
}
答案 0 :(得分:2)
将spawnCrow
的声明移到当前函数上下文之外:
func yourMethod() {
var spawn = SKAction.runBlock({() in self.spawnCrow()})
}
func spawnCrow() {
// do some spawning
}
或删除self
并继续在函数中定义:
func yourMethod() {
func spawnCrow() {
}
var spawn = SKAction.runBlock({() in spawnCrow()})
}
请注意,在两种情况下,您都可以实际删除所有() in
。