Continuing Part 1.
Last week I ended with enemy spaceships being added to the Array, to be iterated over later.
This week, I am going to cover the part where touch coordinates are checked for collision with the spaceships using Circle-Point collision test and display score on the screen.
In GameScene.swift :
In the function "touchesBegan" (function used to track beginning of touch. Complements touchesEnded, and touchesMoved), we get a Set of touches to deal with.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent){
Inside this functionswift :
In the function a for loop is used to iterate through all the touches.
for loops follow the syntax of
"for item: TYPE in ARRAY/SET{"
Here, AnyObject type is used to denote an instance of any Type. I am drawing parallels with "auto" keyword of C++ where auto takes on the type of the value. For e.g in C++ we could write the same range based for loop as
for (auto i : touches)
Inside the for loop, location of the touch is obtained using locationInNode() function for the SKScene (Game's main and only scene)
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Inside this for loop, Iterate through the enemies, and if the touch location falls inside the bounding circle for the enemy spaceship, then consider it a hit, remove the spaceship from the SKScene, remove it from the Array, and increment Score.
Here e.position indicates the center of the sprite, because the anchor position "anchorPoint" is left to its default value (0.5,0.5) which means the sprite is centered on its position.
If the distance square is less than 500( Trial and error value), then remove the child from the SKScene, and remove from the custom Array, and increment Hits count.
for (i,e) in enumerate(Enemies){
var x = CGFloat(location.x - e.postion.x)
var y = CGFloat(location.y - e.position.y)
var d2 = CGFloat(x*x + y*y) // Formula : (DistanceSquare = (X1 - X2) ^ 2 + (Y1 - Y2) ^ 2)
if(d2 < 500){
self.removeChildrenInArray([e])
Enemies.removeAtIndex(i)
Hits++
}
}
}
Get the child node with name "score" and verify if it is SKLabelNode, and change its text based on format "Score: %d" where %d denotes format specifier for int
if let node = self.childNodeWithName("score") as? SKLabelNode {
node.text = NSString(format: "Score: %d", Hits)
This should cover the collision detection.
Now moving on to initialization : this sets the basic properties of the label which is used to display score of the game.
in override func didMoveToView(view: SKView){
let ScoreLabel = SKLabelNode(fontNamed:"Chalkduster")
ScoreLabel.name = "score"
ScoreLabel.text = "Score : 0"
ScoreLabel.fontSize = 20
ScoreLabel.position = CGPoint(x:400, y:200)
self.addChild(ScoreLabel)
}
This is the code jam of the week starting with Jan 12 2015.
Last week I ended with enemy spaceships being added to the Array, to be iterated over later.
This week, I am going to cover the part where touch coordinates are checked for collision with the spaceships using Circle-Point collision test and display score on the screen.
In GameScene.swift :
In the function "touchesBegan" (function used to track beginning of touch. Complements touchesEnded, and touchesMoved), we get a Set of touches to deal with.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent){
Inside this functionswift :
In the function a for loop is used to iterate through all the touches.
for loops follow the syntax of
"for item: TYPE in ARRAY/SET{"
Here, AnyObject type is used to denote an instance of any Type. I am drawing parallels with "auto" keyword of C++ where auto takes on the type of the value. For e.g in C++ we could write the same range based for loop as
for (auto i : touches)
Inside the for loop, location of the touch is obtained using locationInNode() function for the SKScene (Game's main and only scene)
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Inside this for loop, Iterate through the enemies, and if the touch location falls inside the bounding circle for the enemy spaceship, then consider it a hit, remove the spaceship from the SKScene, remove it from the Array, and increment Score.
Here e.position indicates the center of the sprite, because the anchor position "anchorPoint" is left to its default value (0.5,0.5) which means the sprite is centered on its position.
If the distance square is less than 500( Trial and error value), then remove the child from the SKScene, and remove from the custom Array, and increment Hits count.
for (i,e) in enumerate(Enemies){
var x = CGFloat(location.x - e.postion.x)
var y = CGFloat(location.y - e.position.y)
var d2 = CGFloat(x*x + y*y) // Formula : (DistanceSquare = (X1 - X2) ^ 2 + (Y1 - Y2) ^ 2)
if(d2 < 500){
self.removeChildrenInArray([e])
Enemies.removeAtIndex(i)
Hits++
}
}
}
Get the child node with name "score" and verify if it is SKLabelNode, and change its text based on format "Score: %d" where %d denotes format specifier for int
if let node = self.childNodeWithName("score") as? SKLabelNode {
node.text = NSString(format: "Score: %d", Hits)
This should cover the collision detection.
Now moving on to initialization : this sets the basic properties of the label which is used to display score of the game.
in override func didMoveToView(view: SKView){
let ScoreLabel = SKLabelNode(fontNamed:"Chalkduster")
ScoreLabel.name = "score"
ScoreLabel.text = "Score : 0"
ScoreLabel.fontSize = 20
ScoreLabel.position = CGPoint(x:400, y:200)
self.addChild(ScoreLabel)
}
This is the code jam of the week starting with Jan 12 2015.



No comments:
Post a Comment