Krish Mehra Krish Mehra - 5 months ago 26
Swift Question

What should I do? In Spritekit swift it says: Expected Declaration [updated]

I was programming in SpriteKit swift and all of a sudden I got this error saying, "Expected Declaration". Here is the code of where I'm getting the error. I updated the code so it is easier to review:

for touch in touches {
let location = touch.locationInNode(self)

var SmallBall = SKSpriteNode(imageNamed: "Ball")
SmallBall.position = Mainball.position
SmallBall.size = CGSize(width: 30, height: 30)
SmallBall.physicsBody = SKPhysicsBody(circleOfRadius: SmallBall.size.width/2)
SmallBall.physicsBody?.affectedByGravity = false
SmallBall.color = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
SmallBall.colorBlendFactor = 1.0
SmallBall.physicsBody?.categoryBitMask = PhysicsCategory.SmallBall
SmallBall.physicsBody?.collisionBitMask = PhysicsCategory.Enemy
SmallBall.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
SmallBall.name = "SmallBall"
SmallBall.physicsBody?.dynamic = true
SmallBall.physicsBody?.affectedByGravity = true


SmallBall.zPosition = 1
self.addChild(SmallBall)

var dx = CGFloat(location.x - Mainball.position.x)
var dy = CGFloat(location.y - Mainball.position.y)
let magnitude = sqrt(dx * dx + dy * dy)
dx /= magnitude
dy /= magnitude

let vector = CGVector(dx: 40.0 * dx, dy: 40.0 * dy)
SmallBall.physicsBody?.applyImpulse(vector)


}


func Enemies(){
let Enemy = SKSpriteNode(imageNamed: "Ball")
Enemy.size = CGSize(width: 20, height: 20)
Enemy.color = UIColor(red: 0.9, green: 0.1, blue: 0.1, alpha: 1.0)
Enemy.colorBlendFactor = 1.0
Enemy.physicsBody = SKPhysicsBody(circleOfRadius: Enemy.size.width/2)
Enemy.physicsBody?.categoryBitMask = PhysicsCategory.Enemy
Enemy.physicsBody?.contactTestBitMask = PhysicsCategory.SmallBall | PhysicsCategory.Mainball
Enemy.physicsBody?.collisionBitMask = PhysicsCategory.SmallBall | PhysicsCategory.Mainball
Enemy.physicsBody?.dynamic = true
Enemy.physicsBody?.affectedByGravity = false
Enemy.name = "Enemy"

let RandomPosNum = arc4random() % 4

switch RandomPosNum{
case 0:
print("case0")
Enemy.position.x = 0
var PosY = arc4random_uniform(UInt32(frame.size.height))
Enemy.position.y = CGFloat(PosY)

self.addChild(Enemy)
break


case 1:
print("case1")

Enemy.position.y = 0
var PosX = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.y = CGFloat(PosX)

self.addChild(Enemy)


break

case 2:
print("case2")
Enemy.position.y = frame.size.height

var PosX = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.y = CGFloat(PosX)

self.addChild(Enemy)

break

case 3:
print("case3")
Enemy.position.x = frame.size.width

var PosY = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.y = CGFloat(PosY)

self.addChild(Enemy)
break

default:
print("error")
break

}
Enemy.runAction(SKAction.moveTo(Mainball.position, duration: 3))
}



//GameCenterStuff

// Game Center Stuff

func saveHighscore(score:Int) {

//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated {

let scoreReporter = GKScore(leaderboardIdentifier: "Highscore.SpiralBlast") //leaderboard id here

scoreReporter.value = Int64(score) //score variable here (same as above)

let scoreArray: [GKScore] = [scoreReporter]

GKScore.reportScores(scoreArray, withCompletionHandler: {
(error : NSError?) in
if error != nil {
print("error")
}
})

}

}

//shows leaderboard screen
func showLeader() {
let vc = self.view?.window?.rootViewController
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)
}

//hides leaderboard screen
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

//initiate gamecenter
func authenticateLocalPlayer(){

let localPlayer = GKLocalPlayer.localPlayer()

localPlayer.authenticateHandler = {(viewController, error) -> Void in

if (viewController != nil) {
let vc = self.view?.window?.rootViewController
vc?.presentViewController(viewController!, animated: true, completion: nil)
}

else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}


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


}

Answer

This is because you cannot declare a for statement wherever you would like. You most likely will need to put it in the touchesBegan method.

Comments