moving paddles only pushing the puck, not hitting it - ios

I am trying to move the paddle, if it hits the puck, the puck should move away. But the puck only move, more like the paddle is only pushing it.
Declaring paddle:
p1.fillColor = SKColor.blackColor()
p1.name = p1Name
p1.physicsBody = SKPhysicsBody(circleOfRadius: 30)
p1.physicsBody.restitution = 1.0
p1.physicsBody.angularDamping = 0.0
p1.physicsBody.allowsRotation = false
p1.physicsBody.dynamic = true
p1.physicsBody.categoryBitMask = p1Category
p1.physicsBody.collisionBitMask = wallCategory | pCategory
p1.physicsBody.contactTestBitMask = pCategory
p1.position = CGPointMake(self.frame.width/2, self.frame.height/4)
self.addChild(p1)
Delcaring puck:
puck.fillColor = SKColor.redColor()
puck.name = pName
puck.physicsBody = SKPhysicsBody(circleOfRadius: 25)
puck.physicsBody.restitution = 0.8
puck.physicsBody.angularDamping = 0.5
puck.physicsBody.linearDamping = 0.5
puck.physicsBody.allowsRotation = false
puck.physicsBody.usesPreciseCollisionDetection = true
puck.physicsBody.friction = 0.0
puck.physicsBody.dynamic = true
puck.physicsBody.categoryBitMask = pCategory
puck.physicsBody.collisionBitMask = wallCategory | p1Category
puck.physicsBody.contactTestBitMask = p1Category
puck.position = CGPointMake(self.frame.width/2, self.frame.height/2)
self.addChild(puck)
How i move the paddle:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let body: SKPhysicsBody = self.nodeAtPoint(location).physicsBody
if (body.node.name == self.p1Name) {
self.fingerOnP1 = true
self.p1.physicsBody.collisionBitMask = wallCategory
}
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
let body = self.nodeAtPoint(location).physicsBody
if (body != nil && body.node != nil) {
if (body.node.name == self.p1Name && self.fingerOnP1 == true) {
let paddle = self.childNodeWithName(p1Name) as SKShapeNode
var paddleX: Int = Int(paddle.position.x) + Int(location.x - previousLocation.x)
let _x: Int = Int(paddle.frame.width/2)
paddleX = max(paddleX, _x)
paddleX = min(paddleX, Int(self.size.width)-_x)
var paddleY: Int = Int(paddle.position.y) + Int(location.y - previousLocation.y)
let _y: Int = Int(paddle.frame.height/2)
paddleY = max(paddleY, _y)
paddleY = min(paddleY, Int(self.size.height)-_y)
paddle.position = CGPointMake(CGFloat(paddleX), CGFloat(paddleY))
}
}
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
var onP1 = false
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if (self.nodeAtPoint(location) == p1) {
onP1 = true
}
}
if (onP1 == true) {
self.fingerOnP1 == false
self.p1.physicsBody.collisionBitMask = wallCategory | pCategory
}
}
I guess the problem is I'm not applying any force to the paddle, I'm just moving the paddle, too. How would I apply force while moving it?
I thought of apply impulse to the puck after contact, but i have no idea how to move it a proper angle or make it move at a proper speed.
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody!
var secondBody: SKPhysicsBody!
if (contact.bodyA.categoryBitMask > contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask == p1Category && secondBody.categoryBitMask == pCategory) {
if (self.fingerOnP1) {
NSLog("hit", nil)
secondBody.applyImpulse(CGVectorMake(20, 0))
}
}
}

Related

use two sprite objects at the same time with single touch?

Currently, I can only use one at a time, and not make the two paddles (one on each side of the screen) act independently when different finger movements are used with each one. Here is the class. Any ideas?
let BallCategoryName = "ball"
let GameMessageName = "gameMessage"
let BallCategory : UInt32 = 0x1 << 0
let LeftCategory : UInt32 = 0x1 << 1
let BlockCategory : UInt32 = 0x1 << 2
let PaddleCategory : UInt32 = 0x1 << 3
let BorderCategory : UInt32 = 0x1 << 4
class GameScene: SKScene, SKPhysicsContactDelegate {
var isFingerOnPaddleL = false
var isFingerOnPaddleR = false
override func didMove(to view: SKView) {
super.didMove(to: view)
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
physicsWorld.contactDelegate = self
let ball = childNode(withName: BallCategoryName) as! SKSpriteNode
ball.physicsBody!.applyImpulse(CGVector(dx: 2.0, dy: -2.0))
let paddleL = childNode(withName: "paddleL") as! SKSpriteNode
//let paddleR = childNode(withName: "paddleR") as! SKSpriteNode
let leftRect = CGRect(x: frame.origin.x, y: frame.origin.y, width: 1, height: frame.size.height)
let left = SKNode()
left.physicsBody = SKPhysicsBody(edgeLoopFrom: leftRect)
addChild(left)
left.physicsBody!.categoryBitMask = LeftCategory
ball.physicsBody!.categoryBitMask = BallCategory
paddleL.physicsBody!.categoryBitMask = PaddleCategory
borderBody.categoryBitMask = BorderCategory
ball.physicsBody!.contactTestBitMask = LeftCategory
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if let body = physicsWorld.body(at: touchLocation) {
if body.node!.name == "paddleL" {
print("Began touch on paddleL")
isFingerOnPaddleL = true
}
else if body.node!.name == "paddleR" {
print("Began touch on paddleR")
isFingerOnPaddleR = true
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
// 1
if isFingerOnPaddleL {
// 2
let touch = touches.first
let touchLocation = touch!.location(in: self)
let previousLocation = touch!.previousLocation(in: self)
// 3
let paddle = childNode(withName: "paddleL") as! SKSpriteNode
// 4
var paddleY = paddle.position.y + (touchLocation.y - previousLocation.y)
// 5
paddleY = max(paddle.size.height/2, paddleY)
paddleY = min(size.height - paddle.size.height/2, paddleY)
// 6
paddle.position = CGPoint(x: paddle.position.x, y: paddleY)
}
else if isFingerOnPaddleR {
// 2
let touch = touches.first
let touchLocation = touch!.location(in: self)
let previousLocation = touch!.previousLocation(in: self)
// 3
let paddle = childNode(withName: "paddleR") as! SKSpriteNode
// 4
var paddleY = paddle.position.y + (touchLocation.y - previousLocation.y)
// 5
paddleY = max(paddle.size.height/2, paddleY)
paddleY = min(size.height - paddle.size.height/2, paddleY)
// 6
paddle.position = CGPoint(x: paddle.position.x, y: paddleY)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (isFingerOnPaddleL == true) {
isFingerOnPaddleL = false
}
else if (isFingerOnPaddleR == true) {
isFingerOnPaddleR = false
}
}
func didBegin(_ contact: SKPhysicsContact) {
// 1
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
// 2
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// 3
if firstBody.categoryBitMask == BallCategory && secondBody.categoryBitMask == LeftCategory {
print("Hit left. First contact has been made.")
}
}
}
You can process the paddles independently by tracking each touch throughout its lifecycle (began/moved/ended). First, define a dictionary with the touch object as the key and the node's name as the value
var touchTracker:[UITouch:String] = [:]
then in touchesBegan, store the name of paddle(s) touched by the user
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchLocation = touch.location(in: self)
let node = atPoint(touchLocation)
if let name = node.name {
if name == "paddleL" {
print("Began touch on paddleL")
touchTracker[touch] = name
}
else if name == "paddleR" {
print("Began touch on paddleR")
touchTracker[touch] = name
}
}
}
}
In touchesMoved, use the touch object to retrieve the node's name from the dictionary, and use the name to access the appropriate paddle.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let touchLocation = touch.location(in: self)
if let name = touchTracker[touch] {
if let paddle = childNode(withName: name) as? SKSpriteNode {
// Move a paddle
paddle.position = CGPoint(x:paddle.position.x, y:touchLocation.y)
}
}
}
}
Lastly, remove the touch/name from the dictionary when a touch ends.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if touchTracker[touch] != nil {
touchTracker.removeValue(forKey: touch)
}
}
}

Trying to move / drag SKSpriteNode in y axis

I'm trying to create a small game ,and in there there is a pipe , and i want to move that pipe along with the y axis , i want to do thins for finger swipe , i tried to do this with touches began but its doesn't looks smooth , so i tried to use uipangesture , but in not familiar with the sprite integration ,
can someone help me to achive this
class GameScene: SKScene,SKPhysicsContactDelegate {
var pipeTextureUp:SKTexture!
var pipeTextureDown:SKTexture!
var pipeUp:SKSpriteNode!
var pipeDown:SKSpriteNode!
var circleTouch: UITouch?
var verticalPipeGap:Double = 60.0
var moveStatus: Bool!
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.whiteColor()
//set the gravity
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
self.physicsWorld.contactDelegate = self
//first create the pipe in a random location
pipeTextureUp = SKTexture(imageNamed: "PipeUp")
pipeTextureUp.filteringMode = .Nearest
pipeTextureDown = SKTexture(imageNamed: "PipeDown")
pipeTextureDown.filteringMode = .Nearest
pipeUp = SKSpriteNode(texture: pipeTextureUp)
pipeUp.setScale(2.0)
pipeUp.name = "pipeUp"
pipeUp.position = CGPoint(x: size.width * 0.9, y: 0.0)
pipeDown = SKSpriteNode(texture: pipeTextureDown)
pipeDown.setScale(2.0)
pipeDown.name = "pipeDown"
pipeDown.position = CGPoint(x: Double(size.width) * 0.9 , y: Double(pipeDown.size.height) + verticalPipeGap)
//addChild(pipeDown)
addChild(pipeUp)
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown" )
{
print("touches started")
moveStatus = true
circleTouch = touch
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
print(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown" )
{
//let fingerPoint = CGPoint(x: size.width * 0.9, y: 0.3)
let actionUpMove:SKAction = SKAction.moveToY(positionInScene.y, duration: 1.0)
pipeUp.runAction(actionUpMove)
pipeUp.position.y = positionInScene.y
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown" )
{
//let fingerPoint = CGPoint(x: size.width * 0.9, y: 0.3)
circleTouch = nil
}
}
}
}
If you want to intercept your swiping along Y coordinate you could put this code in didMoveToView:
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.swipedUp(_:)))
swipeUp.direction = .Up
swipeUp.cancelsTouchesInView = true
swipeUp.delaysTouchesBegan = true
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.swipedDown(_:)))
swipeDown.direction = .Down
swipeDown.cancelsTouchesInView = true
swipeDown.delaysTouchesBegan = true
view.addGestureRecognizer(swipeDown)
Then your methods are:
func swipedUp(sender:UISwipeGestureRecognizer){
print("swiped up")
}
func swipedDown(sender:UISwipeGestureRecognizer){
print("swiped down")
}
UPDATE : apparently this is what i had to do ,
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown" )
{
handleTouch(touches.first!, name: name)
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown" )
{
handleTouch(touches.first!, name: name)
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown" )
{
handleTouch(touches.first!, name: name)
}
}
}
func handleTouch(touch: UITouch , name : String) {
let location = touch.locationInNode(self) // get the current point
let node = self.nodeAtPoint(location) //get the current node based on the touched location
let previousLocation = touch.previousLocationInNode(self) //get the previous location in node
let diff = location.y - previousLocation.y; //get the different of location
let newPosition = CGPointMake(node.position.x, node.position.y + diff);
var newPipeDownPosition :CGPoint;
if (name == "pipeDown" )
{
newPipeDownPosition = CGPointMake(self.pipeUp.position.x, self.pipeUp.position.y + diff);
pipeDown.position.y = newPosition.y
pipeUp.position.y = newPipeDownPosition.y
}else{
newPipeDownPosition = CGPointMake(self.pipeDown.position.x, self.pipeDown.position.y + diff);
pipeUp.position.y = newPosition.y
pipeDown.position.y = newPipeDownPosition.y
}
}
now the pipe moves according to top and down direction

When I press run program my game loads but it won't start

I'm working with this tutorial: https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor
When I press run program in Xcode, the simulator loads the game and pops up showing the first frame of the game but it is frozen. When I click, the player sprite is supposed to move to where I clicked and the AI sprites are supposed to try and catch the player, but nothing happens. Clicks don't work and I've tried just letting it sit for a while to see if it just hadn't finished loading or something but that hasn't worked either.
All the code for the program so far is here:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let playerSpeed: CGFloat = 150.0
let zombieSpeed: CGFloat = 75.0
var goal: SKSpriteNode?
var player: SKSpriteNode?
var zombies: [SKSpriteNode] = []
var lastTouch: CGPoint? = nil
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
updateCamera()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
handleTouches(touches)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
handleTouches(touches)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
handleTouches(touches)
}
private func handleTouches(touches: Set<UITouch>) {
for touch in touches {
let touchLocation = touch.locationInNode(self)
lastTouch = touchLocation
}
}
override func didSimulatePhysics() {
if let _ = player {
updatePlayer()
updateZombies()
}
}
private func shouldMove(currentPosition currentPosition: CGPoint, touchPosition: CGPoint) -> Bool {
return abs(currentPosition.x - touchPosition.x) > player!.frame.width / 2 ||
abs(currentPosition.y - touchPosition.y) > player!.frame.height/2
}
func updatePlayer() {
if let touch = lastTouch {
let currentPosition = player!.position
if shouldMove(currentPosition: currentPosition, touchPosition: touch) {
let angle = atan2(currentPosition.y - touch.y, currentPosition.x - touch.x) + CGFloat(M_PI)
let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0)
player!.runAction(rotateAction)
let velocotyX = playerSpeed * cos(angle)
let velocityY = playerSpeed * sin(angle)
let newVelocity = CGVector(dx: velocotyX, dy: velocityY)
player!.physicsBody!.velocity = newVelocity;
updateCamera()
} else {
player!.physicsBody!.resting = true
}
}
}
func updateCamera() {
if let camera = camera {
camera.position = CGPoint(x: player!.position.x, y: player!.position.y)
}
}
func updateZombies() {
let targetPosition = player!.position
for zombie in zombies {
let currentPosition = zombie.position
let angle = atan2(currentPosition.y - targetPosition.y, currentPosition.x - targetPosition.x) + CGFloat(M_PI)
let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0.0)
zombie.runAction(rotateAction)
let velocotyX = zombieSpeed * cos(angle)
let velocityY = zombieSpeed * sin(angle)
let newVelocity = CGVector(dx: velocotyX, dy: velocityY)
zombie.physicsBody!.velocity = newVelocity;
}
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == zombies[0].physicsBody?.categoryBitMask {
gameOver(false)
} else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == goal?.physicsBody?.categoryBitMask {
gameOver(true)
}
player = self.childNodeWithName("player") as? SKSpriteNode
for child in self.children {
if child.name == "zombie" {
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
goal = self.childNodeWithName("goal") as? SKSpriteNode
}
private func gameOver(didWin: Bool) {
print("- - - Game Ended - - -")
let menuScene = MenuScene(size: self.size)
menuScene.soundToPlay = didWin ? "fear_win.mp3" : "fear_lose.mp3"
let transition = SKTransition.flipVerticalWithDuration(1.0)
menuScene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view?.presentScene(menuScene, transition: transition)
}
}
The rest of the things completed in the program such as adding sprites and such was done in GameScene.sks so there is no code for it.
Your setup code is in the wrong place. Move this from didBeginContact to didMoveToView:
player = self.childNodeWithName("player") as? SKSpriteNode
for child in self.children {
if child.name == "zombie" {
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
goal = self.childNodeWithName("goal") as? SKSpriteNode
At least that's the difference I see when comparing your code to the sample project in the tutorial.

Not detecting Collisions properly in Swift

I'm making a game that spawns cubes from the top of the screen, when they are touched they disappear. I'm setting an objects position to the point touched by the user. It runs the function DidBeginContact , but it doesn't seem to detect my two views:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
print("didbegincontact")
if ((firstBody.categoryBitMask == PhysicsCategory.RedSquare) && (secondBody.categoryBitMask == PhysicsCategory.touchlocation)) || ((firstBody.categoryBitMask == PhysicsCategory.touchlocation) && (secondBody.categoryBitMask == PhysicsCategory.RedSquare)) {
collisionwithredsquare(firstBody.node as! SKSpriteNode)
print("Done")
}
}
It's printing "didBegincontact" so I know that the function is working but the if statement is not.
Here's all the code:
import SpriteKit
struct PhysicsCategory {
static let RedSquare : UInt32 = 1
static let touchlocation : UInt32 = 2
static let BlueSquare : UInt32 = 3
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var animator : UIDynamicAnimator?
let blueSquare = SKSpriteNode()
let redSquare = SKSpriteNode()
var scorenumber = 0
var ground = SKSpriteNode()
var touchpoint = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector(dx: 1.0, dy: -9.0)
/* let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
// 2. Set the friction of that physicsBody to 0
borderBody.friction = 0
// 3. Set physicsBody of scene to borderBody
self.physicsBody = borderBody */
touchpoint = SKSpriteNode(imageNamed:"Spaceship")
touchpoint.physicsBody?.categoryBitMask = PhysicsCategory.touchlocation
touchpoint.physicsBody?.contactTestBitMask = PhysicsCategory.RedSquare
touchpoint.physicsBody?.affectedByGravity = false
touchpoint.xScale = 0.5
touchpoint.yScale = 0.5
self.addChild(touchpoint)
let width = UInt32(self.frame.size.width)
let X = arc4random() % width
ground = SKSpriteNode(imageNamed: "Ground")
ground.setScale(1.0)
ground.position = CGPoint(x: self.frame.width / 2, y: 5)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
ground.physicsBody?.affectedByGravity = false
ground.physicsBody?.dynamic = false
ground.zPosition = 3
self.addChild(ground)
//INizialize
animator = UIDynamicAnimator(referenceView: self.view!)
//Add Gravity
//animator?.addBehavior(gravity, sprite)
var test = arc4random() % 90
blueSquare.size = CGSize(width: 100, height: 100)
blueSquare.position = CGPoint(x: CGFloat(X), y: 1000)
blueSquare.zRotation = 34
blueSquare.name = "bluecube"
blueSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
blueSquare.physicsBody?.affectedByGravity = true
blueSquare.physicsBody?.dynamic = false
blueSquare.color = SKColor.blueColor()
// BlueSquare.physicsBody?.velocity = CGVectorMake(0, 0)
//BlueSquare.physicsBody?.applyImpulse(CGVectorMake(0, -90))
self.addChild(blueSquare)
let X2 = arc4random() % width
redSquare.size = CGSize(width: 100, height: 100)
redSquare.position = CGPoint(x: CGFloat(X2), y: 1000)
redSquare.name = "redcube"
redSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
redSquare.physicsBody?.affectedByGravity = true
redSquare.physicsBody?.dynamic = true
redSquare.hidden = false
redSquare.physicsBody?.categoryBitMask = PhysicsCategory.RedSquare
redSquare.physicsBody?.contactTestBitMask = PhysicsCategory.touchlocation
redSquare.color = SKColor.redColor()
self.addChild(redSquare)
let timerAction1 = SKAction.waitForDuration(0.5)
let timerAction2 = SKAction.runBlock(spawning1)
let timerSequence = SKAction.sequence([timerAction1, timerAction2])
runAction(SKAction.repeatActionForever(timerSequence))
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
print("didbegincontact")
if ((firstBody.categoryBitMask == PhysicsCategory.RedSquare) && (secondBody.categoryBitMask == PhysicsCategory.touchlocation)) || ((firstBody.categoryBitMask == PhysicsCategory.touchlocation) && (secondBody.categoryBitMask == PhysicsCategory.RedSquare)) {
collisionwithredsquare(firstBody.node as! SKSpriteNode)
print("Done")
}
}
func collisionwithredsquare(redsquare: SKSpriteNode) {
print("Hello")
}
func spawning1() {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
touchpoint.position = location
}
}
func score () {
scorenumber++
print(scorenumber)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if CGRectIntersectsRect(redSquare.frame, ground.frame) {
redSquare.position = CGPoint(x: redSquare.position.x, y: redSquare.position.y + 10)
}
}
}
Thanks in advance, Niall
You collision categories are wrong. It should be this because you are dealing with 32 bit integers.
struct PhysicsCategory {
static let RedSquare: UInt32 = 0x1 << 1
static let touchlocation : UInt32 = 0x1 << 2
static let BlueSquare : UInt32 = 0x1 << 3
}
If you use your way you would have to write it like this
struct PhysicsCategory {
static let RedSquare: UInt32 = 1
static let touchlocation : UInt32 = 2
static let BlueSquare : UInt32 = 4
static let somethingElse : UInt32 = 8
}
which is more confusing because you cannot just increment the last number by 1.
Than change your collision code to this. This way you dont have to check for both bodies in the if statement.
/// Did Begin Contact
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask == PhysicsCategory.RedSquare) && (secondBody.categoryBitMask == PhysicsCategory.touchlocation) {
collisionwithredsquare(firstBody.node) // I aim pretty sure you dont need to cast as SKSpriteNod, because the body its already a SKNode.
print("Done")
}
}
Also on the touchLocaiton sprite you forgot to give it a physics body.
touchpoint.pysicsBody = SKPhysicsBody(..) // FORGOT THIS LINE
touchpoint.physicsBody?.categoryBitMask = PhysicsCategory.touchlocation
touchpoint.physicsBody?.contactTestBitMask = PhysicsCategory.RedSquare
touchpoint.physicsBody?.affectedByGravity = false
This should work now if you are already getting the contact method to be called. Let me know how it goes.

How do I allow MULTIPLE images to be moved using touch in SpriteKit?

so I am making this game in Swift and I have included the code of what I have done on the 'GameScene.swift' file.
I have 4 different objects that will need to be recreated at run time after every 0.2 seconds (don't even know how to do that :/), however using the code I currently have, if I try to move the 'coin20', 'coin100', or 'coin50' image, they don't move, only 'coin10' gets moved to their location and is far too sensitive so it just moves around like crazy whilst the other images are static.
I hope this question is specific enough since my past questions weren't well received by answerers, if not please let me know how to improve, i'm just a beginner in programming.
Thank you
Code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var coin10 = SKSpriteNode(imageNamed: "10S.png")
var coin100 = SKSpriteNode(imageNamed: "100S.png")
var coin50 = SKSpriteNode(imageNamed: "50S.png")
var coin20 = SKSpriteNode(imageNamed: "20S.png")
var wall1 = SKSpriteNode(imageNamed: "Wall1.png")
var wall2 = SKSpriteNode(imageNamed: "Wall2.png")
var bar = SKSpriteNode(imageNamed: "Bar.png")
var scorelabel = SKLabelNode()
var score = 0
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
enum ColliderType:UInt32 {
case coin = 1
case wall = 2
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//Adding coin10
coin10.position = CGPointMake(self.size.width / 2, self.size.height / 5)
coin10.physicsBody = SKPhysicsBody(circleOfRadius: coin10.size.width/2)
coin10.physicsBody!.affectedByGravity = false
coin10.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin10.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin10.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin10.physicsBody!.dynamic = true
self.addChild(coin10)
//Adding coin100
coin100.position = CGPointMake(self.size.width / 1.7, self.size.height / 5.1)
coin100.physicsBody = SKPhysicsBody(circleOfRadius: coin100.size.width/2)
coin100.physicsBody!.affectedByGravity = false
coin100.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin100.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin100.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin100.physicsBody!.dynamic = true
self.addChild(coin100)
//Adding coin50
coin50.position = CGPointMake(self.size.width / 2.2, self.size.height / 4.9)
coin50.physicsBody = SKPhysicsBody(circleOfRadius: coin50.size.width/2)
coin50.physicsBody!.affectedByGravity = false
coin50.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin50.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.dynamic = true
self.addChild(coin50)
//Adding coin20
coin20.position = CGPointMake(self.size.width / 2.4, self.size.height / 5)
coin20.physicsBody = SKPhysicsBody(circleOfRadius: coin20.size.width/2)
coin20.physicsBody!.affectedByGravity = false
coin20.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
coin20.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
coin20.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
coin50.physicsBody!.dynamic = true
self.addChild(coin20)
//Adding wall1
wall1.position = CGPointMake(self.size.width / 1.72, self.size.height / 1.07)
wall1.physicsBody = SKPhysicsBody(rectangleOfSize: wall1.frame.size )
wall1.physicsBody!.affectedByGravity = false
wall1.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
wall1.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
wall1.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
wall1.physicsBody!.dynamic = false
self.addChild(wall1)
//Adding wall2
wall2.position = CGPointMake(self.size.width / 2.5, self.size.height / 1.07)
wall2.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.frame.size )
wall2.physicsBody!.affectedByGravity = false
wall2.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
wall2.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
wall2.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
wall2.physicsBody!.dynamic = false
self.addChild(wall2)
//Adding bar
bar.position = CGPointMake(self.size.width / 2, self.size.height / 1)
bar.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.frame.size)
bar.physicsBody!.affectedByGravity = false
bar.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
bar.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
bar.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
bar.physicsBody!.dynamic = false
self.addChild(bar)
//Adding physics world properties
self.physicsWorld.contactDelegate = self
var scenebody = SKPhysicsBody(edgeLoopFromRect: self.frame)
scenebody.friction = 0
self.physicsBody = scenebody
self.physicsWorld.gravity = CGVectorMake(0, -0.3)
//Scoreboard
scorelabel = SKLabelNode(text: "0")
scorelabel.position.y = (self.size.height/2)
scorelabel.position.x = (self.size.height/2.3)
addChild(scorelabel)
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let touch = touches.first as! UITouch
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location){
touchPoint = location
touching = true
}
else if coin100.containsPoint(location){
touchPoint = location
touching = true
}
else if coin20.containsPoint(location){
touchPoint = location
touching = true
}
else if coin50.containsPoint(location){
touchPoint = location
touching = true
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location){
touchPoint = location
}
else if coin100.containsPoint(location){
touchPoint = location
}
else if coin50.containsPoint(location){
touchPoint = location
}
else if coin20.containsPoint(location){
touchPoint = location
}
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
if touching {
let dt: CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-coin10.position.x, dy: touchPoint.y-coin10.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
coin10.physicsBody!.velocity = velocity
}
}
}
First, add this instance variable
var touchedCoin:SKSpriteNode?
Then, in touchesBegan, set touchedCoin to the coin that was touched:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if coin10.containsPoint(location) {
touchPoint = location
touching = true
touchedCoin = coin10
}
else if coin100.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin100
}
else if coin20.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin20
}
else if coin50.containsPoint(location){
touchPoint = location
touching = true
touchedCoin = coin50
}
}
}
Lastly, move the coin that was touched
override func update(currentTime: CFTimeInterval) {
if touching {
let dt: CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-touchedCoin!.position.x, dy: touchPoint.y-touchedCoin!.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
touchedCoin!.physicsBody!.velocity = velocity
}
}

Resources