- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
嘿伙计们,我在碰撞检测方面遇到了一个大问题,这艘船似乎能够与 objectGroup 和 missileGroup 正确碰撞。问题是我希望当导弹通过墙节点时它会在分数上加 1 但它不起作用,我已经尝试修复它超过 5 个小时但没有找到任何解决方案。
class GameScene: SKScene , SKPhysicsContactDelegate {
var spaceShip = SKSpriteNode ()
var bgSpace = SKSpriteNode ()
var score = 0
var scoreLabel = SKLabelNode()
var gameOverLabel = SKLabelNode()
var labelHolder = SKSpriteNode()
let shipGroup:UInt32 = 1 << 1
let objectGroup:UInt32 = 1 << 2
let wallGroup:UInt32 = 1 << 3
let missileGroup:UInt32 = 1 << 4
var gameOver = 0
var movingObjects = SKNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)
makeBackground()
self.addChild(labelHolder)
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 150)
scoreLabel.zPosition = 1000
self.addChild(scoreLabel)
//add spaceship
var spaceShipTexture = SKTexture (imageNamed: "productImgs/F5S4.png")
spaceShip = SKSpriteNode (texture: spaceShipTexture)
spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
spaceShip.xScale = 0.7
spaceShip.yScale = 0.7
spaceShip.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(spaceShip.frame.size.width, spaceShip.frame.size.height))
spaceShip.physicsBody?.dynamic = true
spaceShip.physicsBody?.allowsRotation = false
spaceShip.physicsBody?.categoryBitMask = shipGroup
spaceShip.physicsBody?.collisionBitMask = objectGroup | missileGroup
spaceShip.zPosition = 10
self.addChild(spaceShip)
//add ground
let ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 250)) //180
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
ground.physicsBody?.contactTestBitMask = shipGroup
self.addChild(ground)
//add top
let top = SKNode()
top.position = CGPointMake(0, 1180)
top.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 1200)) // 1000
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
top.physicsBody?.contactTestBitMask = shipGroup
self.addChild(top)
var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makeMissile"), userInfo: nil, repeats: true)
// this is the wall on the left... enable skView.showsPhysics = true in viewcontroller
var wall = SKNode()
wall.position = CGPoint(x:10 , y: CGRectGetMidY(self.frame))
wall.physicsBody = SKPhysicsBody (rectangleOfSize: CGSizeMake(3, self.frame.size.height))
wall.physicsBody?.dynamic = false
wall.physicsBody?.categoryBitMask = wallGroup
wall.physicsBody?.contactTestBitMask = missileGroup
wall.physicsBody?.collisionBitMask = 0
self.addChild(wall)
}
func makeBackground(){
//add bg
if gameOver == 0 {
var bgTexture = SKTexture(imageNamed: "productImgs/1820925.jpg")
var backgroundAnimated = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 12)
var replaceBackground = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var backgroundMoveForever = SKAction.repeatActionForever(SKAction.sequence([backgroundAnimated,replaceBackground]))
for var i:CGFloat=0; i<3; i++ {
bgSpace = SKSpriteNode(texture: bgTexture)
bgSpace.position = CGPoint (x: CGRectGetMidX(self.frame)/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
bgSpace.size.height = self.frame.height
bgSpace.runAction(backgroundMoveForever)
movingObjects.addChild(bgSpace)
//check this part needed
}
}
}
func makeMissile(){
if gameOver == 0 {
var missile = SKSpriteNode(imageNamed: "productImgs/missile-hi.png")
missile.setScale(0.38)
// Adding SpriteKit physics body for collision detection
missile.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(missile.frame.size.width,missile.frame.size.height))
missile.physicsBody?.dynamic = false
missile.physicsBody?.categoryBitMask = missileGroup
missile.physicsBody?.collisionBitMask = shipGroup
missile.physicsBody?.contactTestBitMask = shipGroup | wallGroup
missile.physicsBody?.usesPreciseCollisionDetection = true
missile.name = "missile"
// Selecting random y position for missile
var random = arc4random_uniform(UInt32(self.frame.size.height)) + 1
var randomPosition = CGFloat(random)
missile.position = CGPointMake(self.frame.size.width + 20, randomPosition)
var movementAmount = arc4random_uniform(UInt32(self.frame.size.height / 2))
var moveMissile = SKAction.moveByX(-self.frame.size.width * 5, y: 0, duration: NSTimeInterval(self.frame.size.width / 100))
var removeMissile = SKAction.removeFromParent()
var moveAndRemoveMissile = SKAction.sequence([moveMissile, removeMissile])
missile.runAction(moveAndRemoveMissile)
movingObjects.addChild(missile)
//add wall
} else if gameOver == 0 {
gameOver = 1
movingObjects.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 40
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame) , CGRectGetMidY(self.frame))
gameOverLabel.zPosition = 100000
labelHolder.addChild(gameOverLabel)
}
}
// part for adding score not working
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == wallGroup || contact.bodyB.categoryBitMask == wallGroup {
score++
scoreLabel.text = "\(score)"
}
else if gameOver == 0 {
gameOver = 1
movingObjects.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 30
gameOverLabel.text = "Game Over! Tap to play again."
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
labelHolder.addChild(gameOverLabel)
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if (gameOver == 0) {
spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
spaceShip.physicsBody?.applyImpulse(CGVectorMake(0, 300))
} else {
score = 0
scoreLabel.text = "0"
movingObjects.removeAllChildren()
makeBackground()
spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
labelHolder.removeAllChildren()
gameOver = 0
movingObjects.speed = 1
}
}
最佳答案
如果你愚蠢你的碰撞正确,删除 didBeginContact
中的 if ,否则 if。它应该工作。我不知道你所说的 workspaceShip.physicsBody?.collisionBitMask = objectGroup | missileGroup
。设置 workspaceShip.physicsBody?.collisionBitMask = 2
以确保正常工作。
编辑
class GameScene: SKScene , SKPhysicsContactDelegate {
var spaceShip = SKSpriteNode ()
var bgSpace = SKSpriteNode ()
var wall = SKNode()
var score = 0
var scoreLabel = SKLabelNode()
var gameOverLabel = SKLabelNode()
var labelHolder = SKSpriteNode()
let shipGroup:UInt32 = 1 << 1
let objectGroup:UInt32 = 1 << 2
let wallGroup:UInt32 = 1 << 3
let missileGroup:UInt32 = 1 << 4
var gameOver = 0
var movingObjects = SKNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)
makeBackground()
self.addChild(labelHolder)
scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 150)
scoreLabel.zPosition = 1000
self.addChild(scoreLabel)
//add spaceship
var spaceShipTexture = SKTexture (imageNamed: "productImgs/F5S4.png")
spaceShip = SKSpriteNode (texture: spaceShipTexture)
spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
spaceShip.xScale = 0.7
spaceShip.yScale = 0.7
spaceShip.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(spaceShip.frame.size.width, spaceShip.frame.size.height))
spaceShip.physicsBody?.dynamic = true
spaceShip.physicsBody?.allowsRotation = false
spaceShip.physicsBody?.categoryBitMask = shipGroup
spaceShip.physicsBody?.collisionBitMask = objectGroup | missileGroup
spaceShip.zPosition = 10
self.addChild(spaceShip)
//add ground
let ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 250)) //180
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
ground.physicsBody?.contactTestBitMask = shipGroup
self.addChild(ground)
//add top
let top = SKNode()
top.position = CGPointMake(0, 1180)
top.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 1200)) // 1000
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
top.physicsBody?.contactTestBitMask = shipGroup
self.addChild(top)
var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makeMissile"), userInfo: nil, repeats: true)
wall.position = CGPoint(x:10 , y: CGRectGetMidY(self.frame))
wall.physicsBody = SKPhysicsBody (rectangleOfSize: CGSizeMake(3, self.frame.size.height))
wall.physicsBody?.dynamic = false
wall.physicsBody?.categoryBitMask = wallGroup
wall.physicsBody?.contactTestBitMask = missileGroup
wall.physicsBody?.collisionBitMask = 0
self.addChild(wall)
}
func makeBackground(){
//add bg
if gameOver == 0 {
var bgTexture = SKTexture(imageNamed: "productImgs/1820925.jpg")
var backgroundAnimated = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 12)
var replaceBackground = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var backgroundMoveForever = SKAction.repeatActionForever(SKAction.sequence([backgroundAnimated,replaceBackground]))
for var i:CGFloat=0; i<3; i++ {
bgSpace = SKSpriteNode(texture: bgTexture)
bgSpace.position = CGPoint (x: CGRectGetMidX(self.frame)/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
bgSpace.size.height = self.frame.height
bgSpace.runAction(backgroundMoveForever)
movingObjects.addChild(bgSpace)
//check this part needed
}
}
}
func makeMissile(){
if gameOver == 0 {
var missile = SKSpriteNode(imageNamed: "productImgs/missile-hi.png")
missile.setScale(0.38)
// Adding SpriteKit physics body for collision detection
missile.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(missile.frame.size.width,missile.frame.size.height))
missile.physicsBody?.dynamic = false
missile.physicsBody?.categoryBitMask = missileGroup
missile.physicsBody?.collisionBitMask = shipGroup
missile.physicsBody?.contactTestBitMask = shipGroup | wallGroup
missile.physicsBody?.usesPreciseCollisionDetection = true
missile.name = "missile"
// Selecting random y position for missile
var random = arc4random_uniform(UInt32(self.frame.size.height)) + 1
var randomPosition = CGFloat(random)
missile.position = CGPointMake(self.frame.size.width + 20, randomPosition)
var movementAmount = arc4random_uniform(UInt32(self.frame.size.height / 2))
var moveMissile = SKAction.moveByX(-self.frame.size.width * 5, y: 0, duration: NSTimeInterval(self.frame.size.width / 100))
var removeMissile = SKAction.removeFromParent()
var moveAndRemoveMissile = SKAction.sequence([moveMissile, removeMissile])
missile.runAction(moveAndRemoveMissile)
movingObjects.addChild(missile)
//add wall
if wall.position.x < missile.position.x{
score++
scoreLabel.text = "\(score)"
}
} else if gameOver == 0 {
gameOver = 1
movingObjects.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 40
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame) , CGRectGetMidY(self.frame))
gameOverLabel.zPosition = 100000
labelHolder.addChild(gameOverLabel)
}
}
// part for adding score not working
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == wallGroup || contact.bodyB.categoryBitMask == wallGroup {
score++
scoreLabel.text = "\(score)"
}
else if gameOver == 0 {
gameOver = 1
movingObjects.speed = 0
gameOverLabel.fontName = "Helvetica"
gameOverLabel.fontSize = 30
gameOverLabel.text = "Game Over! Tap to play again."
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
labelHolder.addChild(gameOverLabel)
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if (gameOver == 0) {
spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
spaceShip.physicsBody?.applyImpulse(CGVectorMake(0, 300))
} else {
score = 0
scoreLabel.text = "0"
movingObjects.removeAllChildren()
makeBackground()
spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
labelHolder.removeAllChildren()
gameOver = 0
movingObjects.speed = 1
}
}
它有点管用,但它从一开始就加 1,因为我想不通。左边的墙是谁
关于swift - 碰撞检测不适用于为得分加 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29733294/
我正在用 jQuery Collision 编写这个游戏,它使用键盘按键来移动 div,当一个 div 接触另一个 div 时,它应该防止重叠。 我到底该怎么做? HTML ----
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java 2D Collision? 嘿,大家好,我有另一篇关于这个问题的帖子刚刚消失了,所以我想我会尝试得到一些关
嘿伙计们,我正在制作一个 2D java 游戏,我正在尝试找出如何制作一个好的碰撞代码。我目前正在使用以下代码: public void checkCollision() { Rect
我的意思是,当我与实体的侧面碰撞并想要跳跃时,我无法向右/向左移动,因为当我与右侧/左侧的实体碰撞时,我有一个标志可以防止这种情况发生,例如所以: 这是我用来检测碰撞的代码: public void
所以我正在运行 collide_mask 检查,以删除与玩家 Sprite 碰撞时的生物实例。它工作得很好。 pygame.sprite.spritecollide(player, mobs, Tru
我正在研究我的砖 block splinter 机,并制作一个适当的碰撞系统,以便使球逻辑地切换方向,我必须检测球与砖 block 的哪一侧碰撞。这是我当前的脚本: int sprite_collid
我做了一个类似颜色切换的游戏。唯一的问题是玩家与每种颜色发生碰撞...... 这是我从github上获取的代码: https://github.com/prometheon/MLNimbleNinja
测试我的游戏,当用户和怪物发生碰撞时,我希望弹出警报但没有成功: function die() { for (var i = 0; i < monster.length; i++) { i
我对 vector 很陌生,这是我第一次真正使用它们进行碰撞检查。这是我的项目,我对如何实现碰撞感到困惑。我目前的碰撞检查和响应代码似乎是……糟糕的设计。 这是我的代码: for(auto it =
我是 javascript 的新手,正在尝试找出如何与球和木板发生碰撞,这将停止游戏并提醒玩家“你输了”。但我只想让红球击中木板,让蓝球不接触地继续前进。这是我正在处理的代码。 (我不介意你是否可以帮
很抱歉提出奇怪的问题,我还是 Android 编程的新手。 我有以下代码: import android.content.DialogInterface.OnClickListener; import
我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,它们都连接到相同的方法。方法是: - (IBAction)handlePan:(UIPanGestur
我想根据某些对象的轴对齐边界框检查视锥体,以粗略检查这些对象是否在视野中。速度不是什么大问题。 最佳答案 我发现构建视锥体的世界空间模型并检查与它的 bbox 碰撞是错误的方法。 一个更简单的方法是以
我项目中的所有这些代码都运行良好,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我不知道如何让它在碰到屏幕边界时不旋转。只有在我使用时才会出现这个问题: plane.physic
在应用程序启动时,我在后台线程中删除旧的 CoreData 行,下面是我的代码。我的问题类似于城市街道问题。所以,我有两个实体,Street 和 City,我有一个关系 City > Street,因
我试图不接触穴居人和其他带有碰撞位掩码的图像,但我的穴居人击中了一切。 func addCaveManBitMasks(){ caveManNode.physicsBody?.category
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
如何在 PyGame 中找到字符和图像之间的碰撞?我已经从图像中绘制了一个玩家,并从瓷砖中绘制了墙壁,那么我如何检测这些碰撞? 最佳答案 如果你使用pygame Rect类来表示对象的边界,您可以使用
我正在使用 ftok() 为 C 应用程序使用的共享内存段生成标识符。我有问题,在一个盒子上我与 root 使用的标识符发生冲突。在这种情况下,我可以通过破解代码来修复它,但我想要一个更强大的解决方案
这个问题在这里已经有了答案: JavaScript: Collision detection (10 个回答) 10 个月前关闭。 检测 2 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我是一名优秀的程序员,十分优秀!