检测一个 SKSpritenode 与许多 SKSpritenode 的接触 Swift
Detecting contact of a SKSpritenode with many SKSpritenodes Swift
我需要在我的项目中检测接触,但我有 9 个方块要检测接触。有没有一种方法可以在不创建 9 个不同的物理体或类似物理体阵列的情况下检测接触。此外,每当圆圈接触到一个正方形时,该正方形就会变色。我想用这样的数组来做到这一点:
for i in 1...9{
if firstBody.categoryBitMask == PhysicsCategory.square[i] && secondBody.categoryBitMask == PhysicsCategory.circle || firstBody.categoryBitMask ==
PhysicsCategory.circle && secondBody.categoryBitMask == PhysicsCategory.square[i] {
squares[i].node.color = squares[i].targetColor
//this is my array of structs containing the skspritenodes
squares[i].colorBlendFactor = 1.0
}
}
我试过制作 9 个物理体,但我遇到了很多错误。这是我到目前为止所做的。
import SpriteKit
var squares = Array<square>()
var positions = Array<CGPoint>()
var squareUnit = CGFloat()
var rows = Array<CGFloat>()
var columbs = Array<CGFloat>()
var circle = SKSpriteNode()
var physics = Array<UInt32>()
struct PhysicsCategory{
static let circle : UInt32 = 0x1 << 0
static let square1 : UInt32 = 0x1 << 1
static let square2 : UInt32 = 0x1 << 2
static let square3 : UInt32 = 0x1 << 3
static let square4 : UInt32 = 0x1 << 4
static let square5 : UInt32 = 0x1 << 5
static let square6 : UInt32 = 0x1 << 6
static let square7 : UInt32 = 0x1 << 7
static let square8 : UInt32 = 0x1 << 8
static let square9 : UInt32 = 0x1 << 9
}
struct square{
var startColor = UIColor()
var middleColor = UIColor()
var targetColor = UIColor()
var has3Colors = Bool()
var permanent = Bool()
var node = SKSpriteNode(imageNamed:"Square")
var currentState = Int()
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
createScene()
}
func createScene(){
self.physicsWorld.contactDelegate = self
self.anchorPoint = CGPoint(x: 0, y: 0)
createSquares()
createCircles()
}
func createCircles(){
circle = SKSpriteNode(imageNamed: "Circle")
circle.size.width = squares[1].node.size.width * 0.9
circle.size.height = squares[1].node.size.height * 0.9
circle.position = squares[4].node.position
circle.color = UIColor.blue
circle.colorBlendFactor = 1.0
circle.zPosition = 10
circle.physicsBody = SKPhysicsBody(circleOfRadius: circle.frame.width / 2)
circle.physicsBody?.categoryBitMask = PhysicsCategory.circle
circle.physicsBody?.affectedByGravity = false
circle.physicsBody?.isDynamic = false
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square1
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square2
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square3
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square4
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square5
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square6
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square7
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square8
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square9
self.addChild(circle)
}
func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
}
func createSquares(){
for i in 1...9{
// squares[i].currentState = 1
}
squareUnit = self.frame.width / 4
columbs = [squareUnit / 4 + squareUnit/2, self.frame.width/2,self.frame.width - squareUnit / 4 - squareUnit / 2]
rows = [squareUnit / 4 + squareUnit/2, self.frame.width/2,self.frame.width - squareUnit / 4 - squareUnit / 2]
for row in rows{
for columb in columbs{
positions.append(CGPoint(x: columb, y: row))
}
}
squares = (0...8).map { _ in square() }
for i in (0...8){
squares[i].node.position = positions[i]
squares[i].node.physicsBody = SKPhysicsBody(rectangleOf: squares[i].node.size )
squares[i].node.physicsBody?.affectedByGravity = false
squares[i].node.physicsBody?.isDynamic = false
}
for square in squares {
square.node.size = CGSize(width: squareUnit, height: squareUnit)
square.node.color = UIColor.white
}
squares.forEach { self.addChild([=12=].node) }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
circle.run(SKAction.move(to: CGPoint(x: location.x, y: location.y), duration: 0.2))
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
我发现我只是创建了 9 个物理体,然后将它们全部添加到一个 UInt32 数组中,并将它们分别分配给每个节点。
var physics = Array<UInt32>()
struct PhysicsCategory{
static let square1 : UInt32 = 0x1 << 1
static let square2 : UInt32 = 0x1 << 2
static let square3 : UInt32 = 0x1 << 3
static let square4 : UInt32 = 0x1 << 4
static let square5 : UInt32 = 0x1 << 5
static let square6 : UInt32 = 0x1 << 6
static let square7 : UInt32 = 0x1 << 7
static let square8 : UInt32 = 0x1 << 8
static let square9 : UInt32 = 0x1 << 9
}
override func didMove(to view: SKView) {
physics.append(PhysicsCategory.square1)
physics.append(PhysicsCategory.square2)
physics.append(PhysicsCategory.square3)
physics.append(PhysicsCategory.square4)
physics.append(PhysicsCategory.square5)
physics.append(PhysicsCategory.square6)
physics.append(PhysicsCategory.square7)
physics.append(PhysicsCategory.square8)
physics.append(PhysicsCategory.square9)
for i in (0...8){
squares[i].node.physicsBody = SKPhysicsBody(rectangleOf: squares[i].node.size )
squares[i].node.physicsBody?.categoryBitMask = physics[i]
squares[i].node.physicsBody?.affectedByGravity = false
squares[i].node.physicsBody?.isDynamic = false
squares[i].node.physicsBody?.contactTestBitMask = PhysicsCategory.Circle
squares[i].node.physicsBody?.collisionBitMask = 0
}
}
我需要在我的项目中检测接触,但我有 9 个方块要检测接触。有没有一种方法可以在不创建 9 个不同的物理体或类似物理体阵列的情况下检测接触。此外,每当圆圈接触到一个正方形时,该正方形就会变色。我想用这样的数组来做到这一点:
for i in 1...9{
if firstBody.categoryBitMask == PhysicsCategory.square[i] && secondBody.categoryBitMask == PhysicsCategory.circle || firstBody.categoryBitMask ==
PhysicsCategory.circle && secondBody.categoryBitMask == PhysicsCategory.square[i] {
squares[i].node.color = squares[i].targetColor
//this is my array of structs containing the skspritenodes
squares[i].colorBlendFactor = 1.0
}
}
我试过制作 9 个物理体,但我遇到了很多错误。这是我到目前为止所做的。
import SpriteKit
var squares = Array<square>()
var positions = Array<CGPoint>()
var squareUnit = CGFloat()
var rows = Array<CGFloat>()
var columbs = Array<CGFloat>()
var circle = SKSpriteNode()
var physics = Array<UInt32>()
struct PhysicsCategory{
static let circle : UInt32 = 0x1 << 0
static let square1 : UInt32 = 0x1 << 1
static let square2 : UInt32 = 0x1 << 2
static let square3 : UInt32 = 0x1 << 3
static let square4 : UInt32 = 0x1 << 4
static let square5 : UInt32 = 0x1 << 5
static let square6 : UInt32 = 0x1 << 6
static let square7 : UInt32 = 0x1 << 7
static let square8 : UInt32 = 0x1 << 8
static let square9 : UInt32 = 0x1 << 9
}
struct square{
var startColor = UIColor()
var middleColor = UIColor()
var targetColor = UIColor()
var has3Colors = Bool()
var permanent = Bool()
var node = SKSpriteNode(imageNamed:"Square")
var currentState = Int()
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
createScene()
}
func createScene(){
self.physicsWorld.contactDelegate = self
self.anchorPoint = CGPoint(x: 0, y: 0)
createSquares()
createCircles()
}
func createCircles(){
circle = SKSpriteNode(imageNamed: "Circle")
circle.size.width = squares[1].node.size.width * 0.9
circle.size.height = squares[1].node.size.height * 0.9
circle.position = squares[4].node.position
circle.color = UIColor.blue
circle.colorBlendFactor = 1.0
circle.zPosition = 10
circle.physicsBody = SKPhysicsBody(circleOfRadius: circle.frame.width / 2)
circle.physicsBody?.categoryBitMask = PhysicsCategory.circle
circle.physicsBody?.affectedByGravity = false
circle.physicsBody?.isDynamic = false
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square1
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square2
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square3
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square4
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square5
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square6
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square7
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square8
circle.physicsBody?.contactTestBitMask = PhysicsCategory.square9
self.addChild(circle)
}
func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
}
func createSquares(){
for i in 1...9{
// squares[i].currentState = 1
}
squareUnit = self.frame.width / 4
columbs = [squareUnit / 4 + squareUnit/2, self.frame.width/2,self.frame.width - squareUnit / 4 - squareUnit / 2]
rows = [squareUnit / 4 + squareUnit/2, self.frame.width/2,self.frame.width - squareUnit / 4 - squareUnit / 2]
for row in rows{
for columb in columbs{
positions.append(CGPoint(x: columb, y: row))
}
}
squares = (0...8).map { _ in square() }
for i in (0...8){
squares[i].node.position = positions[i]
squares[i].node.physicsBody = SKPhysicsBody(rectangleOf: squares[i].node.size )
squares[i].node.physicsBody?.affectedByGravity = false
squares[i].node.physicsBody?.isDynamic = false
}
for square in squares {
square.node.size = CGSize(width: squareUnit, height: squareUnit)
square.node.color = UIColor.white
}
squares.forEach { self.addChild([=12=].node) }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
circle.run(SKAction.move(to: CGPoint(x: location.x, y: location.y), duration: 0.2))
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
我发现我只是创建了 9 个物理体,然后将它们全部添加到一个 UInt32 数组中,并将它们分别分配给每个节点。
var physics = Array<UInt32>()
struct PhysicsCategory{
static let square1 : UInt32 = 0x1 << 1
static let square2 : UInt32 = 0x1 << 2
static let square3 : UInt32 = 0x1 << 3
static let square4 : UInt32 = 0x1 << 4
static let square5 : UInt32 = 0x1 << 5
static let square6 : UInt32 = 0x1 << 6
static let square7 : UInt32 = 0x1 << 7
static let square8 : UInt32 = 0x1 << 8
static let square9 : UInt32 = 0x1 << 9
}
override func didMove(to view: SKView) {
physics.append(PhysicsCategory.square1)
physics.append(PhysicsCategory.square2)
physics.append(PhysicsCategory.square3)
physics.append(PhysicsCategory.square4)
physics.append(PhysicsCategory.square5)
physics.append(PhysicsCategory.square6)
physics.append(PhysicsCategory.square7)
physics.append(PhysicsCategory.square8)
physics.append(PhysicsCategory.square9)
for i in (0...8){
squares[i].node.physicsBody = SKPhysicsBody(rectangleOf: squares[i].node.size )
squares[i].node.physicsBody?.categoryBitMask = physics[i]
squares[i].node.physicsBody?.affectedByGravity = false
squares[i].node.physicsBody?.isDynamic = false
squares[i].node.physicsBody?.contactTestBitMask = PhysicsCategory.Circle
squares[i].node.physicsBody?.collisionBitMask = 0
}
}