gpt4 book ai didi

ios - 框架的接触方法

转载 作者:行者123 更新时间:2023-11-30 13:43:39 25 4
gpt4 key购买 nike

我正在尝试实现一种联系方法来了解我的玩家 block 何时触及黄色框。下面是我到目前为止的代码。当我将玩家 block 拖到黄色框时,什么也没有发生。

enter image description here

import SpriteKit
import iAd

struct BitMask {
static let player: UInt32 = 0x1 << 0
static let obstacle: UInt32 = 0x1 << 1
static let frame: UInt32 = 0x1 << 2
}

class GameScene: SKScene, SKPhysicsContactDelegate {

let player = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30))

override func didMoveToView(view: SKView) {
super.didMoveToView(view)

// setup scene's physics body (setup the walls)
physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)

// get current screen size
let screenSize: CGRect = UIScreen.mainScreen().bounds
print("screenWidth: \(screenSize.width) screenHeight: \(screenSize.height)")

// setup play scene
let playScreen = SKSpriteNode(color: .clearColor(), size: CGSize(width: 370, height: 370))

// y position adjustment
let yFrame = 65
playScreen.position = CGPoint(x: frame.midX, y: frame.midY - CGFloat(yFrame))

// create the rectangle which will represent physics body.
let rect = CGRect(origin: CGPoint(x: -playScreen.size.width/2, y: -playScreen.size.height/2), size: playScreen.size)

// apply physics conditions to the play scene
playScreen.physicsBody = SKPhysicsBody(edgeLoopFromRect: rect)
playScreen.physicsBody?.friction = 0

// add play scene to the frame
addChild(playScreen)

let playerScreenFrame = SKShapeNode(rectOfSize: CGSize(width: playScreen.size.width, height: playScreen.size.height))
playerScreenFrame.position = CGPoint(x: frame.midX, y: frame.midY - CGFloat(yFrame))
playerScreenFrame.fillColor = .clearColor()
playerScreenFrame.strokeColor = UIColor(rgba: "#E9BD00")
playerScreenFrame.lineWidth = 3;
addChild(playerScreenFrame)

let bottomRect = CGRectMake(frame.midX, frame.midY - CGFloat(yFrame), playScreen.size.width, playScreen.size.height)
let bottom = SKNode()
bottom.physicsBody = SKPhysicsBody(edgeLoopFromRect: bottomRect)
addChild(bottom)
bottom.physicsBody!.categoryBitMask = BitMask.frame
bottom.physicsBody!.contactTestBitMask = BitMask.player
bottom.physicsBody!.collisionBitMask = BitMask.player

// set up player block
player.name = "player"
player.fillColor = UIColor(rgba: "#E9BD00")
player.strokeColor = .blackColor()
player.position = CGPoint(x:frame.size.width/2, y: frame.size.height/2 - CGFloat(yFrame))
player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 30, height: 30))
player.physicsBody!.dynamic = false
player.physicsBody!.friction = 0
player.physicsBody!.affectedByGravity = false
player.physicsBody!.allowsRotation = false
addChild(player)
player.physicsBody!.categoryBitMask = BitMask.player
player.physicsBody!.contactTestBitMask = BitMask.obstacle | BitMask.frame
player.physicsBody!.collisionBitMask = BitMask.obstacle | BitMask.frame

// set gravity to 0
physicsWorld.gravity = CGVectorMake(0,0)

// set the scene as the delegate to be notified when two bodies collide
physicsWorld.contactDelegate = self

}

我的联系方式如下:

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 == BitMask.player) && (secondBody.categoryBitMask == BitMask.obstacle) {
// do your thing
print("Player contacted with obstacle")
self.view?.paused = true
}

if (firstBody.categoryBitMask == BitMask.player) && (secondBody.categoryBitMask == BitMask.frame) {
// do your thing
print("Player contacted with frame")
self.view?.paused = true
}
}

最佳答案

当前您正在尝试检查两个静态物体之间的接触,但除非至少有一个物体是动态的,否则 SpriteKit 不会生成任何接触事件。

就您而言,其原因与基于边缘的物理体默认情况下是静态的这一事实有关,并且您的玩家也是静态的,意味着没有接触。

要解决此问题,您应该设置 dynamic将玩家物理 body 上的属性设置为 true,如下所示:

player.physicsBody!.dynamic = true

关于ios - 框架的接触方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248245/

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