sprite kit : contact not working - ios

I am trying to make one simple game and this problem happen first my paddle can touch the ball but now it cant do that and
this code also should give me message or in simulator but it is not showing any idea?
import SpriteKit
class GameScene: SKScene , SKPhysicsContactDelegate{
var istouchingpaddle = false
let ballcatagery:UInt32 = 0 * 1 << 0
let paddlecategary :UInt32 = 0 * 1 << 1
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let border = SKPhysicsBody(edgeLoopFromRect: self.frame)
border.friction = 0
self.physicsBody = border
self.physicsWorld.gravity = CGVectorMake(0,-9.8)
self.physicsWorld.contactDelegate = self
let ball = childNodeWithName ("ball") as SKSpriteNode
ball.physicsBody?.applyImpulse(CGVectorMake(30, -30))
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.restitution = 1
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody!.categoryBitMask = ballcatagery
let paddle = childNodeWithName("paddle") as SKSpriteNode
paddle.physicsBody!.categoryBitMask = paddlecategary
ball.physicsBody?.contactTestBitMask = paddlecategary
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == ballcatagery && contact.bodyB.categoryBitMask == paddlecategary{
println("working ")
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
var touch = touches.anyObject() as UITouch
var location = touch.locationInNode(self)
if let body = self.physicsWorld.bodyAtPoint(location){
if body.node!.name == "paddle" {
istouchingpaddle = true
}
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if istouchingpaddle{
var touch = touches.anyObject() as UITouch
var location = touch.locationInNode(self)
var prevlocation = touch.previousLocationInNode(self)
var paddle = childNodeWithName("paddle") as SKSpriteNode
var position = paddle.position.x + (location.x - prevlocation.x)
position = max(position,paddle.size.width/2)
position = min(position, size.width - paddle.size.width/2)
paddle.position = CGPoint(x: position, y: paddle.position.y)
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
istouchingpaddle = false
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
} }

I can produce good results with your code if I change the way of defining categories. Try setting categories like this :
let ballcatagery: UInt32 = 0x1 << 0 // Not that you have misspelled word "category", which can lead to errors if you expect somewhere a word "category" instead of your actual "categary".
let paddlecategary : UInt32 = 0x1 << 1
I can post you the whole code , but you should be good with this.

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)
}
}
}

How to make an object (SKSprite node) disappear on click

I want the node to disappear when I click on the object. The object is moving around the screen and currently is removed when you click anywhere on the screen. However, I want it to be removed only when the object has been clicked on.
The code is as follows:
import SpriteKit
import GameplayKit
struct Physics {
static let Enemy: UInt32 = 0x1 << 1
let BorderCategory : UInt32 = 0x1 << 2
let BottomCategory : UInt32 = 0x1 << 3
let BallCategory : UInt32 = 0x1 << 4
}
class GameScene: SKScene {
var Enemy = SKSpriteNode()
var gameStarted = Bool()
var gameState = "running"
var destX : CGFloat = 0.0
var destY : CGFloat = 0.0
var score = 0
override func didMove(to view: SKView) {
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
Enemy.name = "Enemys"
Enemy = SKSpriteNode(imageNamed: "red2")
Enemy.size = CGSize(width: 60, height: 70)
Enemy.position = (CGPoint(x: self.frame.width / 6 - Enemy.frame.width, y: self.frame.height / 10))
Enemy.physicsBody = SKPhysicsBody(circleOfRadius: Enemy.frame.height / 2)
Enemy.physicsBody?.categoryBitMask = Physics.Enemy
//Enemy.physicsBody?.categoryBitMask = Physics.Ground | Physics.wall
//Enemy.physicsBody?.contactTestBitMask = Physics.Ground | Physics.wall
Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.isDynamic = true
Enemy.physicsBody?.restitution = 1
Enemy.physicsBody?.friction = 0
Enemy.physicsBody?.angularDamping = 0
Enemy.physicsBody?.linearDamping = 0
self.addChild(Enemy)
let force = SKAction.applyForce(CGVector(dx: 300, dy: 300) , duration: 0.1)
Enemy.run(force)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as UITouch!
let touchLocation = touch?.location(in: self)
let targetNode = atPoint(touchLocation!) as! SKSpriteNode
if(targetNode.name == "Enemys"){
Enemy.removeFromParent()
}
}
I personally find that the easiest way to handle this is to subclass SKSpriteNode and dig into the fact that it is descendant of UIResponder.
class AlienSprite: SKSpriteNode {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.removeFromParent()
}
}
You also need to set the isUserInteractionEnabled = true on the instance somewhere.
You have to determine which node is under the touch, SKNode has methods about this: nodeAtPoint and nodesAtPoint:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as UITouch!
let touchLocation = touch?.location(in: self)
if let hit = yourRootNode.nodeAtPoint(touchLocation) {
if hit.name=="Enemy" {
hit.removeFromParent();
}
}
}

Giving properties of a UIButton to a SKSpriteNode in SpriteKit

I was wondering if there was a way to give the properties of a UIButton like darkening the button once it has been pressed,... to a SKSpiteNode since an SKSpiteNode has more customization and because I am using SpriteKit. I have seen 1 other question like this but none of the answers worked. Here is the code I have to create the SKSpriteNode and to detect a touch on it:
import SpriteKit
class StartScene: SKScene {
var startButton = SKSpriteNode()
override func didMoveToView(view: SKView) {
startButton = SKSpriteNode(imageNamed: "playButton")
startButton.size = CGSize(width: 100, height: 100)
startButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 50)
self.addChild(startButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
if startButton.containsPoint(location){
// When it has been selected
}
}
}
//...
Pleas help. Thanks in advance... Anton
I have always achieved this by adding code to the touches began, and touches ended method. Inside of these methods I simply set the sprites color to black, and then change its color blend factor. Let me know if this works for you!
//This will hold the object that gets darkened
var target = SKSpriteNode()
//This will keep track if an object is darkened
var have = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var first = touches.first as! UITouch
var location:CGPoint = first.locationInNode(self)
touchP = location
mouse.position = touchP
var node:SKNode = self.nodeAtPoint(location)
if let button = node as? SKSpriteNode
{
target = button
have = true
}
else
{
have = false
}
if (have == true)
{
target.color = UIColor.blackColor()
target.colorBlendFactor = 0.2
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if (havet == true)
{
target.color = UIColor.blackColor()
target.colorBlendFactor = 0
target = SKSpriteNode()
have = false
}
}

Sprite Kit stop Impulse

I want to increase a CGFloat every time while the Screen is tapped.
The float has a set maximum and minimum value.
I tried to go through this suggestion: StackOverflow
However, this only increases the CGFloat after the touch is made, or the maximum is reached. I want to increase the CGFloat during the touch, meaning the longer you touch the higher the Jump/CGFloat.
The problem probably lies within the impulse, that you cant change it after it was applied. That means, after the 'Player' gets an impulse of 20, and the screen is longer touched, the Float may increase, but the impulse won't.
If you look at my current code, the impulse is set at maximum while the screen is touched, but if released the action should be removed. However, it doesn't work, the impulse does not stop.
I know that you can set the velocity of the body at a value after the press is made, and if the press has ended the velocity back to 0 so it stops it 'jump', but that doesn't look quite smooth as it would be with an impulse.
Has anybody a solution?
struct Constants {
static let minimumJumpForce:CGFloat = 20.0
static let maximumJumpForce:CGFloat = 60.0
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var force: CGFloat = 20.0
func longPressed(longPress: UIGestureRecognizer) {
if (longPress.state == UIGestureRecognizerState.Began) {
println("Began")
self.pressed = true
let HigherJump = SKAction.runBlock({Player.physicsBody?.applyImpulse(CGVectorMake(0, Constants.maximumJumpForce))})
self.runAction(HigherJump , withKey:"HighJump")
}else if (longPress.state == UIGestureRecognizerState.Ended) {
println("Ended")
self.pressed = false
self.removeActionForKey("HighJump")
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
1.Create ‘Game’ from Xcode template based on SpriteKit
2.Copy paste listed code to GameScene class
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var location = CGPoint()
var floorSize = CGSize()
var floorColor = UIColor()
var player = SKSpriteNode()
override func didMoveToView(view: SKView) {
view.showsFPS = true;
view.showsNodeCount = true;
view.showsDrawCount = true;
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = 1
self.physicsBody?.contactTestBitMask = 1
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsWorld.contactDelegate = self;
location = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
player = SKSpriteNode(imageNamed:"Spaceship")
player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 320, height: 320))
player.physicsBody?.categoryBitMask = 1
player.physicsBody?.collisionBitMask = 1
player.physicsBody?.contactTestBitMask = 1
player.physicsBody?.linearDamping = 0;
player.xScale = 1
player.yScale = 1
player.position = location
self.addChild(player)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.physicsWorld.gravity = CGVectorMake(0, 0)
let direction = Float(1.5708)//Float(player.zRotation) + Float(M_PI_2)
player.physicsBody?.applyForce(CGVector(dx: 150000*CGFloat(cosf(direction)), dy: 150000*CGFloat(sinf(direction))))
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.physicsWorld.gravity = CGVectorMake(0, -7.9)
}
}
3.Run the app
This should give you start point for you 'Jump' game :)
Try changing this:
if(self.pressed){
let HigherJump = SKAction.runBlock({if(self.force < Constants.maximumJumpForce){
self.force += 2.0
}else{
self.force = Constants.maximumJumpForce
}})
self.runAction(HigherJump)
}
to this:
if(self.pressed){
if(self.force < Constants.maximumJumpForce) {
self.force += 2.0
}
else {
self.force = Constants.maximumJumpForce
}
}
Theres no need to use a runBlock SKAction here.

didBeginContact not being called

I'm trying to create a program that prints out something whoever my spaceship goes over a circle, but it's not printing anything when I put the spaceship over the circle. Did I build my didBeginContact method wrong? Did I set up the BitMasks wrong?
import SpriteKit
class GameScene: SKScene {
var spaceship: SKNode!
var circ: SKNode!
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
spaceship = SKSpriteNode(imageNamed: "Spaceship")
spaceship.setScale(0.4)
spaceship.position.x = self.frame.width/2
spaceship.position.y = spaceship.frame.height/2
spaceship.physicsBody = SKPhysicsBody(circleOfRadius: spaceship.frame.height/2)
spaceship.physicsBody?.categoryBitMask = 1
spaceship.physicsBody?.contactTestBitMask = 2
spaceship.physicsBody?.collisionBitMask = 0
spaceship.physicsBody?.dynamic = true
circ = SKShapeNode(circleOfRadius: 50)
circ.position.y = self.frame.height/2
circ.position.x = self.frame.width/2
circ.physicsBody = SKPhysicsBody(circleOfRadius: 50)
circ.physicsBody?.categoryBitMask = 2
circ.physicsBody?.contactTestBitMask = 1
circ.physicsBody?.collisionBitMask = 0
circ.physicsBody?.dynamic = true
self.addChild(circ)
self.addChild(spaceship)
}
func didBeginContact(contact: SKPhysicsContact){
println("colliding!")
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
spaceship.position = location
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
spaceship.position = location
}
}
}
You need to declare yourself as the contact delegate of your physics world:
// add conformance to SKPhysicsContactDelegate:
class GameScene: SKScene, SKPhysicsContactDelegate {
// ...
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
// set as delegate:
self.physicsWorld.contactDelegate = self
// ..
}
// should be called now
func didBeginContact(contact: SKPhysicsContact){
println("colliding!")
}
}

Resources