Move two paddles at same time in SpriteKit + Swift - ios

I am making a simple Pong game using SpriteKit + Swift. It is a multiplayer game, so I want two people to be able to move the two paddles. I want to be able to move the paddles at the same time. When I run the code below, which is the touchesBegan function, I am only able to move each paddle when one finger is pressing the display. When I try to touch another paddle, while my finger is already on the screen, it does not respond.
let touch = touches.first as UITouch?
let touchLocation = touch?.locationInNode(self)
let body: SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation!)
if body?.node!.name == PaddleCategoryName {
print("Paddle Touched!")
fingerIsOnPaddle = true
} else if body?.node!.name == PaddleCategoryName2 {
print("Paddle2 Touched!")
fingerIsOnPaddle2 = true
}

I know this is a bit late but if you still dont know the answer I believe you need to set this line in your viewController when loading your first scene
skView.multipleTouchEnabled = true

Related

SKActions Doesn't works sometimes, but if minimise the game and reopen it, It Works. How that could happen?

I'm having problem with 2 actions both of the actions are move to actions, that moves the pieces to empty node's position(Empty sknodes are added from sks file for). The problem only happens some times not always, in first one when player reaches loc:25 means wins it moves to empty node's position but it some times doesn't moves to there but the game goes on and if I minimise the game and open it they are already there where they should have moved previously.
Second problem this one is more important because with this skaction player moves to next Position. But very few times it doesn't works everything works fine but when it comes to execute that skaction it Stops totally, only touches works. but if I just minimise the game and open it, it moves to position and then game works fine. No issue then, How can this Happen?
I run the second skaction with sequence with 2 other actions and first one with skspritenode.run() action. and I don't have iOS device so I'm testing it on simulator.
Here are the both of SKActions that I use just the same thing nothing new :
for node in children {
if (node.name == String(nextSpace)) {
let moveAction:SKAction = SKAction.move(to: node.position, duration: 0.5)
moveAction.timingMode = .easeOut
let wait:SKAction = SKAction.wait(forDuration: 0.1)
let runAction:SKAction = SKAction.run({
if (self.movesRemaining == 1) {
self.KillHim(self.whosTurn, nextspace: nextSpace)
}
self.movesRemaining = self.movesRemaining - 1
self.setThePlayerSpace(space: nextSpace, player:self.whosTurn)
self.movePiece()
})
if whosTurn == .Player1 {
touchedNode.run(SKAction.sequence([moveAction, wait, runAction]))
} else {
playerPiece.run(SKAction.sequence([moveAction, wait, runAction]))
}
}
}
code for moving when player won :
if (currentSpacePlayer1Piece1 == 25) {
let loc:SKNode = childNode(withName: "c1")!
Player1Piece1.run(SKAction.move(to: loc.position, duration: 0.2))
currentSpacePlayer1Piece1 = 26
OneMoreMove += 1
} else if (currentSpacePlayer1Piece2 == 25) {
let loc:SKNode = childNode(withName: "c2")!
Player1Piece2.run(SKAction.move(to: loc.position, duration: 0.2))
currentSpacePlayer1Piece2 = 26
OneMoreMove += 1
}
is it could be happening because of using it in simulator?
As knightOfDragon said the node was being paused with some logic some times after removing, it works perfectly. If you have same issue search isPaused if you are using it somewhere not intended.

Using 3D touch in Sprite Kit to measure pressure

In my main app I'm trying to use 3D touch to activate a bullet fireing, but the results seem to be very inconsistant and it didn't always give my the write results. Also my iPhone 6S was crashing when I used this code, This is the code I used for a test.
for touch in touches {
let location = touch.locationInNode(self)
var force = touch.force * 10
var maxForce = touch.maximumPossibleForce
print ("The force is")
print (maxForce*4)
print (force*10000000)
if force != 0{
myLabel.text = "Force"
myLabel.fontSize = 45
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(myLabel)
myLabel.zPosition = 100000
}
else {
myLabel.removeFromParent()
}
I am a going to guess it is crashing because you add label to the parent multiple times (Your if condition is broken, no matter what function you call, (touchesBegan,touchesMoved,touchesEnded) only one of those conditions will always be true, in touchesBegan and touchesMoved, your force will always be > 0, in your touchEnded, your force should be 0. (I am going to assume this code is your touchMoved) Instead, add your label in the your viewDidLoad code, and use the .hidden field to show/hide your label on your moves. (Make sure on touchesEnded to hide it)

SKSpriteNode has given itself teleporting capabilities and it can't be stopped

If anyone has ever played league of legends, they know that you click an area to make a player move to that spot. They don't get there instantly, it takes a little bit of time. I've tried to add this functionality to a game that I am trying to make. The problem is, the character tends to sometimes move slowly towards the touched area, which is great because that is what I want, but some times there is an inconsistency where it teleports instantaneously to the touched spot. Can anyone see the problem?
let touch = touches.anyObject()! as UITouch
let location = touch.locationInNode(self)
let moveAction = SKAction.moveTo(location, duration: 3.0)
if(tankTurn) // Tank's turn when tankTurn is true.
{
tank.runAction(moveAction)
tank.position = location
tankTurn = false
}// if statement

Views doesn't collides while moving ( Swift iOS )

i am trying to use some views in my app and i am trying to animate and collide these view by hitting each other but problem is that when i use gravity these views collides
but when i drag/move any view collision doesn't works. whats wrong with my code here goes my code
For moving views
func tappedMe(gr: UIPanGestureRecognizer)
{
if gr.view?.tag == 1{
let point = gr.locationInView(self.view);
square.center.x=point.x
square.center.y=point.y
}else if gr.view?.tag == 2{
let point = gr.locationInView(self.view);
square2.center.x=point.x
square2.center.y=point.y
}
}
for Gravity
animator = UIDynamicAnimator(referenceView: view)
gravity = UIGravityBehavior(items: [square, square2])
let direction = CGVectorMake(0, -1)
gravity!.gravityDirection = direction
animator.addBehavior(gravity)
For Collision
collision = UICollisionBehavior(items: [ square, square2])
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collision)
thanks in advance
I don't think you are meant to directly manipulate views while they are attached to a UIDynamicAnimator. I think that you either need to remove all of the behaviours or add attachment behaviours at the touch point and move the attachment point during the pan gesture.
(This is from memory, I haven't used UIDynamics very recently).

Prevent dragged object from leaving view its assigned to

I'm trying to write a piece of code in iOS using swift that creates a square where the user touches and lets them drag it around. The catch is I want the area it can move around in to be confined to the UIView it was created it.
The code below almost works. You can only create the square by pressing within the box, but then you can just drag it where you want. I'm not picky about if the box stays in the "fence" and tracks with your finger or just disappears until you move your finger back in, but I can't have it all over the screen.
I'm pretty new to this, so if there's a better way to go about it, I'm happy to be corrected.
class ViewController: UIViewController {
var dragableSquare = UIView() // a square that will appear on press and be dragged around
var fence = UIView() // a view that the square should be confined within
override func viewDidLoad() {
super.viewDidLoad()
// define the fence UIView and it to view
fence.frame = CGRectMake(view.frame.width/2 - 100, view.frame.height/2 - 100, 200, 200)
fence.backgroundColor = UIColor.grayColor()
view.addSubview(fence)
// give the fence a gesture recognizer
var pressRecog = UILongPressGestureRecognizer(target: self, action: "longPress:")
pressRecog.minimumPressDuration = 0.001
fence.addGestureRecognizer(pressRecog)
}
func longPress(gesture: UILongPressGestureRecognizer) {
print("press!")
// get location of the press
var touchPoint = gesture.locationInView(fence)
// When the touch begins place the square at that point
if gesture.state == UIGestureRecognizerState.Began {
print("began")
// create and add square to fence view
dragableSquare.frame = CGRectMake(touchPoint.x-5, touchPoint.y-5, 10, 10)
dragableSquare.backgroundColor = UIColor.blueColor()
self.fence.addSubview(dragableSquare)
// While the press continues, update the square's location to the current touch point
} else {
print("moving")
dragableSquare.center = touchPoint
}
}
I just joined stack overflow and I've been really impressed with how generous and helpful the community is. I hope I'll get enough experience to start helping others out soon too.
You can use CGRectIntersection to get the size of the intersection rectangle between to views. In your case, you want to keep moving the square as long as the intersection rectangle between the square and the fence is the same size as the square (meaning the square is still wholly within the fence). So your else clause should look like this,
} else {
print("moving")
if CGRectIntersection(dragableSquare.frame, fence.bounds).size == dragableSquare.frame.size {
dragableSquare.center = touchPoint
}
}

Resources