- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个太空射击游戏,它会随机生成敌方飞船。一旦玩家达到30级,就会产生一个“Boss” Sprite 。在最初的应用程序中,我将其设置为 Boss 出现的位置,并且仅左右移动,因为我遇到了 Sprite 在与子弹碰撞后消失的问题。
我没有将其设置为如果双方都进行了联系,则会将 Boss 从父级中删除。
我试图通过复制应用程序并删除除玩家和 boss Sprite 之外的所有内容来解决这个问题。
当设置PhysicsWorld.contactDelegate = self时, Sprite 会消失,因此这是一个碰撞问题。
这可能是 xcode 的错误吗?
这不是图像本身,因为我取出了图像,并且仅使用占位符图像(大红色 X)表示未找到该名称的图像。
这是它的样子的 gif bullet hitting boss
屏幕左侧显示的是从下往上发射的子弹从右到左的图像是老板 Sprite
这是我的 gamescene.swift
import SpriteKit
import UIKit
struct PhysicsCatagory {
static let Enemy : UInt32 = 1 //000000000000000000000000000001
static let Bullet : UInt32 = 0b1 //00000000000000000000000000010
static let Player : UInt32 = 4 //00000000000000000000000000100
static let tj: UInt32 = 0b10
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var Highscore = Int()
var Score = Int()
var BossCounter = Int()
var TJBossHIT = Int()
var Player = SKSpriteNode(imageNamed: "player")
var Enemy = SKSpriteNode(imageNamed: "Enemy.png")
var TJ = SKSpriteNode(imageNamed: "tjBoss9.png")
var Fake = SKSpriteNode(imageNamed: "tjBoss.png")
var ScoreLbl = UILabel()
let HealthBarWidth: CGFloat = 100
let HealthBarHeight: CGFloat = 40
var TJHealth = 20
override func didMoveToView(view: SKView) {
Score = 0
physicsWorld.contactDelegate = self
// self.scene?.backgroundColor = UIColor.darkGrayColor()
self.scene?.size = CGSize(width: 640, height: 1136)
self.scene?.zPosition = 0
Player.position = CGPointMake(self.size.width / 2, self.size.height / 5)
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCatagory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
Player.physicsBody?.dynamic = false
var Timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)
let enemy1SpawnDelay = SKAction.waitForDuration(0.5)
runAction(
SKAction.sequence([
enemy1SpawnDelay,
SKAction.runBlock({self.SpawnTJ()}),
SKAction.waitForDuration(0.4)]) , withKey: "SpawnStop")
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
//TJ Section for Coliision with bullets
if (((firstBody.categoryBitMask == PhysicsCatagory.tj) && (secondBody.categoryBitMask == PhysicsCatagory.Bullet)) ||
((firstBody.categoryBitMask == PhysicsCatagory.Bullet) && (secondBody.categoryBitMask == PhysicsCatagory.tj))){
// CollisionWithTJBoss(firstBody.node as! SKSpriteNode), Bullet: (secondBody.node as! SKSpriteNode)
CollisionWithTJBoss(firstBody.node as! SKSpriteNode, TJ: secondBody.node as! SKSpriteNode)
}
}
func CollisionWithBullet(Enemy: SKSpriteNode, Bullet:SKSpriteNode){
Enemy.removeFromParent()
Bullet.removeFromParent()
Score++
ScoreLbl.text = "\(Score)"
print(Score)
}
func SpawnTJ() {
TJ.physicsBody = SKPhysicsBody(rectangleOfSize: TJ.size)
TJ.physicsBody?.categoryBitMask = PhysicsCatagory.tj
TJ.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
TJ.physicsBody?.affectedByGravity = false
TJ.physicsBody?.dynamic = true
TJ.physicsBody?.allowsRotation = false
TJ.position = CGPoint(x: self.size.width / 2, y: self.size.height + 30)
TJ.zPosition = 0.1
//TJ.zPosition = 0
let moveDown = SKAction.moveToY(self.size.height / 2, duration: 5.0)
let moveRight = SKAction.moveByX(200, y: 0, duration: 2.0)
let moveLeft = SKAction.moveByX(-200, y: 0, duration: 3.0)
let reverseMoveRight = moveRight.reversedAction()
let reverseMoveLeft = moveLeft.reversedAction()
let sequence1 = SKAction.sequence([moveRight, reverseMoveRight, moveLeft, reverseMoveLeft])
// let sequence1 = SKAction.sequence([moveLeft, reverseMoveLeft, moveRight, reverseMoveRight])
// let endlessAction = SKAction.repeatActionForever(sequence)
TJ.runAction(SKAction.sequence([moveDown]))
TJ.runAction(SKAction.repeatActionForever(sequence1), withKey: "TJBossEnd")
self.addChild(TJ)
}
func CollisionWithTJBoss (Bullet: SKSpriteNode, TJ:SKSpriteNode)
{
Bullet.removeFromParent()
TJBossHIT++
NSLog("Hit")
print(TJBossHIT)
}
func SpawnBullets(){
let Bullet = SKSpriteNode(imageNamed: "Bullets")
Bullet.zPosition = -2
Bullet.position = CGPointMake(Player.position.x, Player.position.y)
let action = SKAction.moveToY(self.size.height + 15, duration: 0.8)
let actionDone = SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action, actionDone]))
Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
// Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.tj
Bullet.physicsBody?.affectedByGravity = false
Bullet.physicsBody?.dynamic = false
self.addChild(Bullet)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
最佳答案
你的子弹和你的敌人有相同的 categoryBitMAsk
值 1:
static let Enemy : UInt32 = 1 //000000000000000000000000000001
static let Bullet : UInt32 = 0b1 //00000000000000000000000000010
所以对 Enemy 的任何测试也将与 Bullet 匹配,反之亦然。
此外,在您的 didBeginContact 中,您通过调用另一个函数来处理碰撞,如下所示:
CollisionWithTJBoss(firstBody.node as! SKSpriteNode, TJ: secondBody.node as! SKSpriteNode)
和CollisionWithTJBoss
定义为:
func CollisionWithTJBoss (Bullet: SKSpriteNode, TJ:SKSpriteNode)
但是您没有做任何事情来保证 firstBody 是子弹而 secondBody 是 TJ(老板)。因此,当您在 CollisionWithTJBoss
中执行 Bullet.removeFromParent()
时,我认为您认为的 Sprite 是子弹,即 firstBody 实际上是 TJ。
为了调试目的,给你的 Sprite 命名,并在 didBeginContact
中做:
print("First body is \(firstBody.node.name) and second body is \(secondBody.node.name)")
要删除特定的 Sprite ,如果您不这样做,如果它是 firstBody 或 secondBody,请使用以下命令:
let bulletNode = contact.bodyA.categoryBitMask == PhysicsCatagory.Bullet ? contact.bodyA.node! : contact.bodyB.node!
bulletNode.removeFromParent()
这保证 bulletNode 是项目符号(如果您的 categoryBitMasks 设置正确)。
您可能会发现按照以下方式重构您的 didBeginContact
不会那么困惑:
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
// TJ and Bullet have contacted
case PhysicsCatagory.tj | PhysicsCatagory.Bullet :
let bulletNode = contact.bodyA.categoryBitMask == PhysicsCatagory.Bullet ? contact.bodyA.node! : contact.bodyB.node!
bulletNode.removeFromParent()
TJBossHIT -= 1 // ++ is deprecated
NSLog("Boss and Bullet have Hit")
print(TJBossHIT)
default :
//Some other contact has occurred
print("Some other contact")
}
}
您可以添加尽可能多的 PhysicsCategory.TJ | PhysicsCategory.Bullet
案例,因为您需要在游戏中采取行动的所有联系人。为每个潜在联系人单独编码,您就不会在 if...then...else 中迷失自己
关于ios - Swift Sprite 在与 Bullet 接触时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36669256/
我使用 vanilla JS 通过 AJAX 向数据库发送请求。数据写入数据库没有问题。在刷新页面之前,我看不到被放入数据库中的内容。当我删除内容时也会发生同样的情况,我必须刷新才能看到内容消失了。
抱歉,这篇文章很长,我试图提供所有信息并解释我已经尝试过的内容。 问题: 我将经典的 Phoenix View 放入实时 View 中。虽然一切看起来都很好,但 echart 一完成绘制就消失了。好像
搜索了一下,没有找到我要找的东西。我有两个按钮,单击一次即可拉出两个不同的选择框,第二次单击它们就会消失。但是,我希望选择框 1 在单击按钮 2 时消失,反之亦然:选择框 2 将在单击按钮 1 时消失
我正在尝试使用自动布局使用“浮动”标题构建滚动视图。更确切地说,我正在尝试建立包含几列的日历视图。这些列中的每个列都应具有自己的标题,该标题应浮动在顶部,而该列可以垂直滚动到其下方。 如果一切正常,则
我正在尝试模仿星星背景。 星星是在加载时创建并随机散布在整个网站上的。到目前为止我已经调整了一些代码。 var star="•"; var numStars=100; for(var x=1;x•";
我查遍了整个网络,但找不到解决方案。我对 jQuery 也很陌生。 我的案例: 我有一个导航栏,其中的每个链接都会激活/触发一个megamenu(每个链接都有自己的megamenu)。 我需要什么:
我有一个可扩展的 ListView ,当我点击一行时,它会触发 onChildClick 或 onGroupClick,具体取决于我点击的是子项还是组。 如果我在 xml 布局文件中添加可点击的内容(
在我的程序中,我有一个 NSMutableData 变量,用于收集来自 http://www.nhara.org/scored_races-2013.htm 的信息。 .大约第三次从网站获取信息后,当
我一直在完美地使用 Genymotion,但是从最近几天开始我的 Genymotion 并打开它的 GPS 开始,我的 Genymotion 突然消失了。但是我的 eclipse 显示 Genymot
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
所以我的 Item::return_all() 有问题,因为我在 main.cpp 中调用它。早些时候我在 read_file() 和 for (auto data : example_item) 中
所有 UITableCells 在 UITableView 中滚动和触摸时消失。一切都以编程方式完成,我使用标准的 UITableViewCell 类。 我在某处读到包含我的单元格数据的数组可能被清空
我对 Swift 还很陌生。我的问题是我的 UICollectionView 正在消失。在 Xcode 中,它显示一切就绪,但当我在模拟器或设备上启动时,它消失了,只剩下导航栏和标签栏。 有谁知道是什
这是 this one的续题。较早的问题是在复选框上。但是后来我根据要求把check box改成了radio buttons。 现在我需要显示子单选按钮并在母单选按钮处于事件状态时更改背景颜色。一旦它
我用 JS 写了一个简单的脚本。当我将它放入 html 文档(在 body 标签的底部)时,它工作正常。当我尝试将它放入外部文档并链接它时,脚本运行但 canvas 元素消失(刚刚检查:当我将脚本放在
我正在尝试创建一个带有固定元素的滚动页面,当用户向下滚动页面时,该元素“始终”可见。例如,带有道路背景的汽车图像。因此,当用户向下滚动页面时,汽车看起来就像在路上行驶。虽然汽车出现在道路的顶部,但它必
这个问题在这里已经有了答案: How can I change CSS display none or block property using jQuery? (15 个答案) 关闭 4 年前。
我在市场上有一个支持小部件的应用程序。我对应用程序进行了一些重大更改,重新设计了很多内部结构,替换/重命名 Activity 等。其他更改包括从不同的共享首选项键获取小部件配置数据,尽管我已经编写了转
我有一个 fragment alertsfragment.java,它有一个 GridView 布局,当我打开它时,它工作正常,但是当我在其他选项卡之间滑动时,它就消失了。为了解释更多,我有一个 Ac
我是一名优秀的程序员,十分优秀!