Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 2 Updates for Follow My Finger #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Accelerometer-Soup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build
9 changes: 8 additions & 1 deletion Accelerometer-Soup/Accelerometer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@
BFE82E941B14202B008B6981 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0630;
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Linda Dong";
TargetAttributes = {
BFE82E9B1B14202B008B6981 = {
Expand Down Expand Up @@ -286,6 +288,7 @@
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -354,6 +357,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Accelerometer/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "lindadong.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -364,6 +368,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Accelerometer/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "lindadong.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -382,6 +387,7 @@
);
INFOPLIST_FILE = AccelerometerTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "lindadong.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Accelerometer.app/Accelerometer";
};
Expand All @@ -397,6 +403,7 @@
);
INFOPLIST_FILE = AccelerometerTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "lindadong.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Accelerometer.app/Accelerometer";
};
Expand Down
Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 15 additions & 19 deletions Accelerometer-Soup/Accelerometer/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SpriteKit
import CoreMotion

extension Array {
func sample() -> T {
func sample() -> Element {
let randomIndex = Int(rand()) % count
return self[randomIndex]
}
Expand All @@ -21,46 +21,45 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
let motionManager: CMMotionManager = CMMotionManager()

// Shape Colors
let colorArray = [ SKColor(red: 0 / 255, green: 64 / 255, blue: 170 / 240, alpha: 1 ),
let colorArray = [
SKColor(red: 0 / 255, green: 64 / 255, blue: 170 / 240, alpha: 1 ),
SKColor(red: 255 / 255, green: 250 / 255, blue: 233 / 240, alpha: 1 ),
SKColor(red: 245 / 255, green: 122 / 255, blue: 99 / 240, alpha: 1 )]
SKColor(red: 245 / 255, green: 122 / 255, blue: 99 / 240, alpha: 1 )
]

override func didMoveToView(view: SKView) {

// Add physics to the borders of the screen
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
physicsBody = borderBody

// Add gravity to the scene
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsWorld.contactDelegate = self
self.physicsBody?.contactTestBitMask = 0
physicsWorld.gravity = CGVectorMake(0, 0)
physicsWorld.contactDelegate = self
physicsBody?.contactTestBitMask = 0

self.backgroundColor = SKColor(red: 255 / 255, green: 191 / 255, blue: 180 / 240, alpha: 1 )

// Create Shapes
let maxShapes = 15
for i in 0..<maxShapes {
for _ in 0..<maxShapes {
self.addChild(self.createRandomShape())
}

motionManager.startAccelerometerUpdates()

}


func createRandomShape() -> SKSpriteNode {

// Chooses a random image for the shape
var randomShape = String("")
let shapes = ["triangle", "squiggle", "circle"]
let shapes = [ "triangle", "squiggle", "circle" ]
let randomIndex = Int(arc4random_uniform(UInt32(shapes.count)))
let shape = SKSpriteNode(imageNamed: shapes[randomIndex])

// Places shapes randomly within the bounds of the screen
var pointX = CGFloat(UInt(arc4random() % UInt32(UInt(self.frame.width - 100))))
var pointY = CGFloat(UInt(arc4random() % UInt32(UInt(self.frame.height - 100))))
let pointX = CGFloat(UInt(arc4random() % UInt32(UInt(frame.width - 100))))
let pointY = CGFloat(UInt(arc4random() % UInt32(UInt(frame.height - 100))))

shape.position = CGPointMake(pointX, pointY)
shape.name = "shape"
Expand All @@ -69,7 +68,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
shape.colorBlendFactor = 1.0

// Adds physics properties to the shapes
shape.physicsBody = SKPhysicsBody(texture: shape.texture, size: shape.size)
shape.physicsBody = SKPhysicsBody(texture: shape.texture!, size: shape.size)
shape.physicsBody?.dynamic = true
shape.physicsBody?.restitution = 0.5
shape.physicsBody?.mass = 0.9
Expand All @@ -85,14 +84,13 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
// Changes the velocity of the scene's gravity based on the accelerometer
if let data = motionManager.accelerometerData {
self.physicsWorld.gravity = CGVectorMake(10 * CGFloat(data.acceleration.x), 10 * CGFloat(data.acceleration.y))

}
}

override func didSimulatePhysics() {

// For those pesky shapes that fall out of the scene
self.enumerateChildNodesWithName("shape", usingBlock: { (node: SKNode!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
self.enumerateChildNodesWithName("shape", usingBlock: { (node: SKNode, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if node.position.y < 10 || node.position.x < 10 {
node.removeFromParent()
}
Expand All @@ -102,6 +100,4 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
override func update(currentTime: CFTimeInterval) {
processUserMotionForUpdate(currentTime)
}


}
Loading