- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我有一个非常具体的问题,希望可以帮助您。我将附上一张图片来澄清我在说什么。
redball.physicalsBody
在 boss.physicalsBody
上方生成 - 掉落(穿过)boss。与 whitebar.physicalsBody
碰撞 - 弹回并再次与 boss.physicalsBody
碰撞。这次,我想触发一个事件,该事件会注意到球何时弹回并击中 Boss。
目前。 Boss和Ball共享相同的collisionBitMask,这样它们就可以互相穿过。当球与杆碰撞时,我尝试将 ball.physicalsBody?.contactTestBitMask =PhysicsCategory.boss.rawValue
添加到球中。这样我就可以注意到球和老板之间返回途中的碰撞。然而这似乎不起作用。
有人能解决这个奇怪的问题吗?非常感谢您的帮助。
旁注:如果有人能想出一个聪明的标题,请告诉我,我会编辑它!
编辑:添加代码
enum PhysicsCategory : UInt32 {
case bar = 1
case ball = 2
case boss = 4
case noCollision = 8
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var score = Int()
var background = SKSpriteNode(imageNamed: "background.png")
var bar = SKSpriteNode(imageNamed: "bar.png")
var boss1 = SKSpriteNode(imageNamed: "boss1.png")
override func didMoveToView(view: SKView) {
self.scene?.size = CGSize(width: 640, height:1136)
physicsWorld.contactDelegate = self
background.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
background.zPosition = -20
self.addChild(background)
bar.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 8)
bar.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: bar.size.width, height: bar.size.height))
bar.physicsBody?.categoryBitMask = PhysicsCategory.bar.rawValue
bar.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
bar.physicsBody?.collisionBitMask = PhysicsCategory.bar.rawValue
bar.physicsBody?.affectedByGravity = false
bar.physicsBody?.dynamic = false
self.addChild(bar)
boss1.position = CGPoint(x: self.frame.width, y: self.frame.height - boss1.size.height / 2)
boss1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: boss1.size.width, height: boss1.size.height))
boss1.physicsBody?.categoryBitMask = PhysicsCategory.boss.rawValue
boss1.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
boss1.physicsBody?.collisionBitMask = PhysicsCategory.noCollision.rawValue
boss1.physicsBody?.affectedByGravity = false
boss1.physicsBody?.dynamic = false
boss1.zPosition = 5
self.addChild(boss1)
boss1.runAction(SKAction.moveToX(self.frame.width / 2, duration: 1))
let spawnBallsAction = SKAction.sequence([SKAction.waitForDuration(2), SKAction.runBlock(spawnBalls)])
self.runAction(SKAction.repeatActionForever(spawnBallsAction))
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {
CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)
}
if (firstBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.boss.rawValue == PhysicsCategory.boss.rawValue) {
CollisionWithBoss(firstBody.node as! SKSpriteNode, boss: secondBody.node as! SKSpriteNode)
}
}
func CollisionWithBar(bar: SKSpriteNode, ball: SKSpriteNode) {
ball.physicsBody?.applyImpulse(CGVectorMake(0, 500))
ball.physicsBody?.contactTestBitMask = PhysicsCategory.boss.rawValue //Trying to solve the problem, aint working
}
func CollisionWithBoss(ball: SKSpriteNode, boss: SKSpriteNode) {
NSLog("Ball hit the boss")
}
func spawnBalls(){
let ball = SKSpriteNode(imageNamed: "ball.png")
let MinValue = self.frame.width / 8
let MaxValue = self.frame.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
ball.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.frame.height - 128)
ball.zPosition = 50
//Physics
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.height / 2)
ball.physicsBody?.categoryBitMask = PhysicsCategory.ball.rawValue
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue
ball.physicsBody?.collisionBitMask = PhysicsCategory.noCollision.rawValue
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.dynamic = true
self.addChild(ball)
}
最佳答案
这就是联系方式:
创建球后,将联系人类别设置为:(您拥有的)
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue
确保老板没有联系任何人:
boss1.physicsBody?.contactTestBitMask = PhysicsCategory.noCollision.rawValue
然后在“联系”上,您想要执行以下操作:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
let secondBody : SKPhysicsBody = (contact.bodyA.categoryBitMask >= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {
//This should be inside CollisionWithBar
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue | PhysicsCategory.boss.rawValue
//the ball has hit the bar, so lets enable hitting on the boss
//normally we would only want to do this once, but since this is
//tiny, it would be more time to wrap it in ifs and putting guards on it
boss1.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)
}
if (firstBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.boss.rawValue == PhysicsCategory.boss.rawValue) {
//This should be inside CollisionWithBoss
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue
CollisionWithBoss(firstBody.node as! SKSpriteNode, boss: secondBody.node as! SKSpriteNode)
}
}
这基本上是说,如果球击中横杆,则启用 Boss 击球,如果球击中 Boss,则禁用 Boss 击球
编辑:在意识到拥有多个球后,我们必须改变一些事情
确保 Boss 在创建时再次接触球:
boss1.physicsBody?.contactTestBitMask = PhysicsCategory.ball.rawValue
现在我们需要创建一个新类别
enum PhysicsCategory : UInt32 {
case bar = 1
case ball = 2
case boss = 4
case noCollision = 8
case initialBall = 16
}
首先,将球的类别分配给此:
ball.physicsBody?.categoryBitMask = initialBall
然后你的 CollissionWithBar 将如下所示:
func CollisionWithBar(bar: SKSpriteNode, ball: SKSpriteNode) {
ball.physicsBody?.applyImpulse(CGVectorMake(0, 500))
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue | PhysicsCategory.boss.rawValue
ball.physicsBody?.categoryBitMask = PhysicsCategory.ball.rawValue
}
你的 CollissionWithBolls 将如下所示:
func CollisionWithBoss(ball: SKSpriteNode, boss: SKSpriteNode) {
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue
}
最后,在“联系”上,您需要执行以下操作:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
let secondBody : SKPhysicsBody = (contact.bodyA.categoryBitMask >= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {
CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)
}
if (firstBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.boss.rawValue == PhysicsCategory.boss.rawValue) {
CollisionWithBoss(firstBody.node as! SKSpriteNode, boss: secondBody.node as! SKSpriteNode)
}
}
关于xcode - 物理物体与另一个物理物体碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401249/
我一直在寻找游戏/模拟解决方案,以便在时间流逝时寻找距离,但这不是我要找的。 我正在寻找一个 O(1) 公式来计算(0 或 1 或 2)时钟时间,其中两个圆圈彼此之间的距离恰好为 r1+r2。负时间是
我究竟做错了什么? > crossprod(1:3,4:6) [,1] [1,] 32 根据本网站:http://onlinemschool.com/math/assistance/ve
嗨,我目前正在为类开发一个迷你游戏(第一次做这样的事情),我根本不知道如何开始碰撞检测。好吧,我正在创建的游戏是一款在冰冷的圆形竞技场上自上而下的相扑格斗游戏,您可以四处移动以获得动力和速度,并尝试击
这段代码取自使用 XNA 框架构建的游戏。我想从三角学和物理学的角度解释一下它是如何工作的。 ball.velocity = new Vector2((float)Math.Cos(cannon.ro
因此,我正在努力自学 Canvas (HTML5) 并编写了大部分简单的游戏引擎代码。它是空间场景(行星、恒星、天体等)的二维表示。我的默认“Sprite”类有一个像这样的帧监听器: “baseCla
这个问题在这里已经有了答案: Are the physical memory addresses of an array also stored in order like the virtual o
我正在尝试阅读英特尔软件开发人员手册以了解操作系统的工作原理,这四个寻址术语让我感到困惑。以上是我的理解,如有不对请指正。 线性地址 : 对一个孤立的程序来说,似乎是一长串以地址0开头的内存。该程序的
我尝试在 AndEngine 示例包中复制并粘贴物理示例。 没有出现错误,但当我运行它时,模拟器显示“不幸的是,PhysicsActivity 已停止”。 模拟器使用 API 15,GPU 已开启,磁
当我运行此代码时,第一行 CollisionWithplayer 给了我一个错误的指令错误。该错误不会每次都会出现,只是偶尔出现一次,并且没有类似的条件来确定导致该错误的原因。 func didBeg
您好,我有以下 Canvas 应用程序:http://dev.driz.co.uk/canvas/ 正如您将看到的,它渲染了一堆球。我遇到的问题是当应用程序首次启动时,球被 Canvas 边缘切断。他
我有两个 3d 物理 vector ,带有 (x,y,z) 和方向。我想对它们做一些操作。但我有一些问题: 我应该如何在 C++ 中表示这个 vector ?换句话说,我在下面写了类,但我不知道如何表
我有一个有 body 的 Sprite 。我想通过路径移动 Sprite 。我已经尝试使用 PathModifier 执行此操作, Sprite 会按原样移动,但它的 body 不会跟随 Sprite
我开发了类似投币推土机的游戏。为了硬币的平稳移动,我为每个硬币添加了一种物理 Material ,但这样做之后我的游戏速度非常慢。有没有其他选择,或者我如何在不使用物理 Material 的情况下使硬
我正在开发一款简单的平台游戏,例如 super 马里奥。我将 Java 与 LibGdx 引擎一起使用。我的物理问题与帧率无关。在我的游戏中,角色可以跳跃,跳跃高度显然取决于帧率。 在我的桌面上,游戏
我正在开发一个可能包含数学、物理和化学符号的问答应用程序,因为这是一个实时游戏应用程序,每次问题将从服务器下载并针对特定主题显示。它需要是一个原生的 Android 应用程序,并且性能非常重要(两人游
我的任务是编写一个对象,该对象可以接收不同类型的路径/url,并返回它是什么类型的路径/url。例如路径可以是 1. [drive]:\Temp 2. \\Temp 3. Temp (assuming
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
注意:当我提到层时,我指的是物理层。此站点上与“层”相关的许多问题都指的是逻辑层,这不是我要问的。 我正在设计一个使用标准“3 层”架构的应用程序,包括表示层、业务逻辑 (BLL) 层和数据访问层 (
如何检查设备上的屏幕或物理/电容式导航按钮 最佳答案 您可以使用 ViewConfiguration.get(context).hasPermanentMenuKey() 仅适用于 API 级别 14
我在我的 android 游戏中使用 AndEngine,我从 github 下载了主 AndEngine,但是没有主的 Physics Box2D 扩展。我不知道在哪里下载它或我可以使用它的哪个版本
我是一名优秀的程序员,十分优秀!