fnovello
(Frank Novello)
1
Player code
//
// Player.swift
// PokemonProto
//
// Created by Frank Novello on 4/23/18.
// Copyright © 2018 Frank Novello. All rights reserved.
//
import SpriteKit
class Player:SKSpriteNode
{
let playerTexture:SKTexture?
let playerColor:UIColor
let playerSize:CGSize
var movePlayerUP :SKAction?
var playerSpeed = 100
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
self.playerTexture = texture
self.playerColor = color
self.playerSize = size
super.init(texture: texture, color: color, size: size)
self.physicsBody = SKPhysicsBody(texture: playerTexture!, size: size)
self.physicsBody?.affectedByGravity = false
self.physicsBody?.allowsRotation = false
self.physicsBody?.isDynamic = true
self.physicsBody?.collisionBitMask = 1
self.physicsBody?.usesPreciseCollisionDetection = true
self.physicsBody?.node?.name = "player"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func getPlayerLocation()->CGPoint
{
return self.position
}
func setPlayerPosition(newPosition: CGPoint)
{
self.position = newPosition
}
func isOutOfBoundsYUPPER(scene:SKScene)->Bool
{
if (self.position.y >= (scene.size.height/2))
{
return true
}
return false
}
func isOutOfBoundsYLOWER(scene:SKScene)->Bool
{
if (self.position.y <= (scene.size.height/2))
{
return true
}
return false
}
func movePlayerUpGo()
{
self.physicsBody?.applyForce(CGVector(dx: 0, dy: playerSpeed))
}
func movePlayerDOWNGo()
{
self.physicsBody?.applyForce(CGVector(dx: 0, dy: -playerSpeed))
}
func movePlayerLEFTGo()
{
self.physicsBody?.applyForce(CGVector(dx: -playerSpeed, dy: 0))
}
func movePlayerRIGHTGo()
{
self.physicsBody?.applyForce(CGVector(dx: playerSpeed, dy: 0))
}
func movePlayerStop()
{
self.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
}
}
House Code:
//
// House.swift
// PokemonProto
//
// Created by Frank Novello on 4/24/18.
// Copyright © 2018 Frank Novello. All rights reserved.
//
import SpriteKit
class House:SKSpriteNode {
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
self.physicsBody?.node?.name = "house"
self.physicsBody = SKPhysicsBody(texture: texture!, size: size)
self.physicsBody?.affectedByGravity = false
self.physicsBody?.allowsRotation = false
self.physicsBody?.isDynamic = false
self.physicsBody?.collisionBitMask = 1
self.physicsBody?.usesPreciseCollisionDetection = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
GameScene:
//
// GameScene.swift
// PokemonProto
//
// Created by Frank Novello on 4/23/18.
// Copyright © 2018 Frank Novello. All rights reserved.
//
import UIKit
import SpriteKit
import GameplayKit
class GameScene: SKScene,SKPhysicsContactDelegate{
let cameraNode: SKCameraNode
var backgroundMusic : [SKAudioNode]
var house: House
required init?(coder aDecoder: NSCoder) {
self.cameraNode = SKCameraNode()
self.backgroundMusic = [SKAudioNode.init(fileNamed: "palletTownMusic.mp3"),
SKAudioNode.init(fileNamed: "The road to viridian city - from palette.mp3"),
SKAudioNode.init(fileNamed: "pewter city's theme.mp3")]
house = House(texture: SKTexture(imageNamed: "House.png"), color: UIColor.clear, size: CGSize(width: 150, height: 150))
super.init(coder: aDecoder)
}
override func didMove(to view: SKView) {
MenuMusic.run(SKAction.stop())
//self.addChild(backgroundMusic[0])
self.addChild(backgroundMusic[1])
self.addChild(backgroundMusic[2])
self.addChild(house)
backgroundMusic[1].run(SKAction.stop())
backgroundMusic[2].run(SKAction.stop())
self.addChild(cameraNode)
camera = cameraNode
addChild(player)
player.setPlayerPosition(newPosition: CGPoint(x: 0, y: 0))
self.physicsWorld.contactDelegate = self
}
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.node?.name == "player" || contact.bodyB.node?.name == "player"){
print("asdf")
}
}
override func update(_ currentTime: TimeInterval) {
print(player.physicsBody?.node?.name)
cameraNode.position.x = player.position.x
cameraNode.position.y = player.position.y
if(player.getPlayerLocation().y > 800 && player.getPlayerLocation().y < 2000)
{
backgroundMusic[0].run(SKAction.stop())
backgroundMusic[1].run(SKAction.play())
backgroundMusic[2].run(SKAction.stop())
}
else if(player.getPlayerLocation().y < 650)
{
backgroundMusic[0].run(SKAction.play())
backgroundMusic[1].run(SKAction.stop())
backgroundMusic[2].run(SKAction.stop())
}
else if(player.getPlayerLocation().y > 2040)
{
backgroundMusic[0].run(SKAction.stop())
backgroundMusic[1].run(SKAction.stop())
backgroundMusic[2].run(SKAction.play())
}
}
}
Look into what categoryBitMask, contactTestBitMask and collision masks in general are. Collisions and contacts aren't the same thing. Here's a tutorial that mentions collision detection.
This will work, for instance.
struct PhysicsCollisionMasks {
static let ship = 1 << 1
static let wall = 1 << 2
}
ship.physicsBody?.categoryBitMask = PhysicsCollisionMasks.ship
ship.physicsBody?.contactTestBitMask = PhysicsCollisionMasks.wall
ship.physicsBody?.collisionBitMask = 0
wall.physicsBody?.categoryBitMask = PhysicsCollisionMasks.wall
wall.physicsBody?.contactTestBitMask = PhysicsCollisionMasks.ship
wall.physicsBody?.collisionBitMask = 0
func read() {
Frank, this is not the first time you are being told that questions about apple frameworks aren't appropriate on this forum. Please consider this. The fact that we are helping you out doesn't mean we approve the question. You are welcome to ask on the developer forums or stackoverflow. You are likely to find an answer there as well.
}