gpt4 book ai didi

Swift 多点触控对象

转载 作者:行者123 更新时间:2023-11-28 09:14:25 25 4
gpt4 key购买 nike

这是我在 Swift 中的第一个游戏。我想用 SpriteKit 制作一个更高级的 Breakout Game,就像这个 tutorial

我想创建 4 个桨叶并用 4 个手指分别移动它们,我尝试这样做,但是当我移动一个桨叶时,第二个桨叶也会移动...我该怎么办?

import SpriteKit
import AVFoundation

class GameScene: SKScene {

var fingerIsOnPaddle1 = false
var fingerIsOnPaddle2 = false
var fingerISOnPaddle3 = false
var fingerIsOnPaddle4 = false

let shurikenCategoryName = "shuriken"
let paddleCategoryName = "paddle"
let paddle2CategoryName = "paddle2"
let paddle3CategoryName = "paddle3"
let paddle4CategoryName = "paddle4"
let brickCategoryName = "brick"

let backgroundMusicPlayer = AVAudioPlayer()

override init(size: CGSize){
super.init(size: size)

let bgMusicURL = NSBundle.mainBundle().URLForResource("bgMusicMP3", withExtension: "mp3")

backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: bgMusicURL, error: nil)
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()

let backgroundImage = SKSpriteNode(imageNamed: "bg")
backgroundImage.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3)
self.addChild(backgroundImage)

self.physicsWorld.gravity = CGVectorMake(0, 0)

let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = worldBorder
self.physicsBody?.friction = 0

let shuriken = SKSpriteNode(imageNamed: "shuriken")
shuriken.name = shurikenCategoryName
shuriken.position = CGPointMake(self.frame.size.width/2, self.frame.size.height / 2)
self.addChild(shuriken)

shuriken.physicsBody = SKPhysicsBody(circleOfRadius: shuriken.frame.width / 2)
shuriken.physicsBody?.friction = 0
shuriken.physicsBody?.restitution = 1
shuriken.physicsBody?.linearDamping = 0
shuriken.physicsBody?.applyImpulse(CGVectorMake(2, 2))
shuriken.physicsBody?.allowsRotation = true

let paddle = SKSpriteNode(imageNamed: "paddle")
paddle.name = paddleCategoryName
paddle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)/4)
self.addChild(paddle)

paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
paddle.physicsBody?.friction = 0.4
paddle.physicsBody?.restitution = 0.1
paddle.physicsBody?.dynamic = false

//paddle.position = CGPointMake(CGRectGetMidX(self.frame)/2, CGRectGetMidY(self.frame)/4)
//self.addChild(paddle)



let paddle2 = SKSpriteNode(imageNamed: "paddle")
paddle2.name = paddle2CategoryName
paddle2.position = CGPointMake(CGRectGetMidX(self.frame)/5, CGRectGetMidY(self.frame))
paddle2.zRotation = CGFloat(M_PI_2)
self.addChild(paddle2)
paddle2.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
paddle2.physicsBody?.friction = 0.4
paddle2.physicsBody?.restitution = 0.1
paddle2.physicsBody?.dynamic = false

let paddle3 = SKSpriteNode(imageNamed: "paddle")
paddle3.name = paddle2CategoryName
paddle3.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)/4*7)
self.addChild(paddle3)

let paddle4 = SKSpriteNode(imageNamed: "paddle")
paddle4.name = paddle2CategoryName
paddle4.position = CGPointMake(CGRectGetMidX(self.frame)/5*9, CGRectGetMidY(self.frame))
paddle4.zRotation = CGFloat(M_PI_2)
self.addChild(paddle4)

}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)

let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)

if body?.node?.name == paddleCategoryName {
println("Paddle 1 touched")
fingerIsOnPaddle1 = true
}

if body?.node?.name == paddle2CategoryName{
println("Paddle 2 touched")
fingerIsOnPaddle2 = true
}
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if fingerIsOnPaddle1{
let touch = touches.anyObject() as UITouch
let touchLoc = touch.locationInNode(self)
let prevTouchLoc = touch.previousLocationInNode(self)

let paddle = self.childNodeWithName(paddleCategoryName) as SKSpriteNode

var newXPos = paddle.position.x + (touchLoc.x - prevTouchLoc.x)

paddle.position = CGPointMake(newXPos, paddle.position.y)
}

if fingerIsOnPaddle2{
let touch2 = touches.anyObject() as UITouch
let touchLoc2 = touch2.locationInNode(self)
let prevTouchLoc2 = touch2.previousLocationInNode(self)

let paddle2 = self.childNodeWithName(paddle2CategoryName) as SKSpriteNode

var newYPos = paddle2.position.y + (touchLoc2.y - prevTouchLoc2.y)

paddle2.position = CGPointMake(paddle2.position.y, newYPos)
}

}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {


}

required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
}

最佳答案

这里问题的症结在于 touchesBegan 函数,它采用 Set 触摸。使用 anyObject() 只获取第一个是一种非常常见的模式,但您可以循环遍历它们,而不是这样做:

   override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches as? UITouch {
let touchLocation = touch.locationInNode(self)

let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)

if body?.node?.name == paddleCategoryName {
println("Paddle 1 touched")
fingerIsOnPaddle1 = true
}

if body?.node?.name == paddle2CategoryName{
println("Paddle 2 touched")
fingerIsOnPaddle2 = true
}
}
}

关于Swift 多点触控对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28377877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com