- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经浏览了所有我可以理解的示例,但没有任何内容与我的问题相关。我有 SKPhysicsContactDelegate 并且添加了 self.physicalsworld.contactDelegate = self.我几乎已经做了我能想到的一切。这是我的项目的所有代码。
//
// GameScene.swift
// arrow jump
//
// Created by Joy Cafiero on 3/29/16.
// Copyright (c) 2016 3rd Dimension Studios inc. All rights reserved.
//
import SpriteKit
var timer = NSTimer()
var condition = 1
var arrow = SKSpriteNode()
var man = SKSpriteNode()
var bg = SKSpriteNode()
var ground = SKSpriteNode()
var buttonRight = SKSpriteNode()
var buttonLeft = SKSpriteNode()
var buttonJump = SKSpriteNode()
let moveGrounRight = SKAction.moveByX(200, y: 0, duration: 1)
let repeatMoveGroundRight = SKAction.repeatActionForever(moveGrounRight)
let moveGrounLeft = SKAction.moveByX(-200, y: 0, duration: 1)
let repeatMoveGroundLeft = SKAction.repeatActionForever(moveGrounLeft)
let runningMan1 = (SKTexture (imageNamed: "running man1.png"))
let runningMan2 = (SKTexture (imageNamed: "running man2.png"))
let runningMan3 = (SKTexture (imageNamed: "running man3.png"))
let runningMan4 = (SKTexture (imageNamed: "running man4.png"))
let runningMan5 = (SKTexture (imageNamed: "running man5.png"))
let runningMan6 = (SKTexture (imageNamed: "running man6.png"))
let nuetralMan = (SKTexture (imageNamed: "running man nuetral.png"))
let jumpingMan1 = (SKTexture (imageNamed: "jumping man1"))
let jumpingMan2 = (SKTexture (imageNamed: "jumping man2"))
let jumpingMan3 = (SKTexture (imageNamed: "jumping man3"))
let animation = SKAction.animateWithTextures([ runningMan1, runningMan2, runningMan3, runningMan4, runningMan5, runningMan6], timePerFrame: 0.15)
let jumpingAnimation = SKAction.animateWithTextures([jumpingMan1, jumpingMan2, jumpingMan3], timePerFrame: 0.1)
let nuetralAnimation = SKAction.animateWithTextures([nuetralMan], timePerFrame: 0.05)
let repeatAnimation = SKAction.repeatActionForever(animation)
let manGroup:UInt32 = 0x1 << 1
let groundGroup:UInt32 = 0x1 << 2
let arrowGroup:UInt32 = 0x1 << 3
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
man = SKSpriteNode(texture: nuetralMan)
man.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
man.size = CGSize(width: man.size.width * 2, height: man.size.height * 2)
man.zPosition = 15
man.physicsBody = SKPhysicsBody(rectangleOfSize: man.size)
man.physicsBody?.dynamic = true
man.physicsBody?.allowsRotation = false
man.physicsBody?.categoryBitMask = manGroup
man.physicsBody?.contactTestBitMask = groundGroup | arrowGroup
self.addChild(man)
let bgTexture = (SKTexture(imageNamed: "wild west landscape.png"))
bg = SKSpriteNode(texture: bgTexture)
bg.size = CGSize(width: self.frame.width, height: self.frame.height)
bg.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 100)
bg.zPosition = -1
self.addChild(bg)
let groundtexture = (SKTexture(imageNamed: "sandy ground.png"))
ground = SKSpriteNode(texture: groundtexture)
ground.size = CGSize(width: CGRectGetMaxX(self.frame), height: ground.size.height)
ground.position = CGPointMake(CGRectGetMidX(self.frame), 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, ground.size.height-255))
ground.physicsBody?.dynamic = false
ground.zPosition = 20
ground.physicsBody?.categoryBitMask = groundGroup
ground.physicsBody?.contactTestBitMask = manGroup
self.addChild(ground)
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("timerUpdate"), userInfo: nil, repeats: true)
buttonRight.color = SKColor.redColor()
buttonRight.position = CGPointMake(200, 200)
buttonRight.size = CGSize(width: 100, height: 100)
buttonRight.zPosition = 25
self.addChild(buttonRight)
buttonLeft.color = SKColor.redColor()
buttonLeft.position = CGPointMake(100, 200)
buttonLeft.size = CGSize(width: 100, height: 100)
buttonLeft.zPosition = 25
self.addChild(buttonLeft)
buttonJump.color = SKColor.greenColor()
buttonJump.position = CGPointMake(CGRectGetMaxX(self.frame) - 200, 200)
buttonJump.size = CGSize(width: 100, height: 100)
buttonJump.zPosition = 25
self.addChild(buttonJump)
}
func timerUpdate() {
let random = Int(arc4random_uniform(3))
var timeIncrease:NSTimeInterval = 0
var randomArrow = [self.frame.height / 20 + CGRectGetMidY(self.frame) - 65, self.frame.height / 20 + self.frame.height / 20 + CGRectGetMidY(self.frame) - 65, self.frame.height / 20 + self.frame.height / 20 + self.frame.height / 20 + CGRectGetMidY(self.frame) - 65, self.frame.height / 20 + self.frame.height / 20 + self.frame.height / 20 + CGRectGetMidY(self.frame) - 65]
timeIncrease = timeIncrease + 7
let arrowTexture = (SKTexture(imageNamed: "arrow.png"))
let movingArrow = SKAction.moveByX(-self.frame.size.width, y: 0, duration: 10-timeIncrease)
let removeArrows = SKAction.removeFromParent()
let repeatmoveArrows = SKAction.sequence([movingArrow, removeArrows])
arrow = SKSpriteNode(texture: arrowTexture)
arrow.size = CGSize(width: arrow.size.width, height: arrow.size.height)
arrow.position = CGPointMake(CGRectGetMaxX(self.frame), randomArrow[random])
arrow.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(arrowTexture.size().width - 100, arrow.size.height / 2))
arrow.physicsBody?.dynamic = false
arrow.zPosition = 10
arrow.physicsBody?.categoryBitMask = arrowGroup
arrow.physicsBody?.contactTestBitMask = manGroup
arrow.runAction(repeatmoveArrows)
self.addChild(arrow)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if buttonRight.containsPoint(location) {
man.xScale = 1
man.runAction(repeatMoveGroundRight, withKey: "MoveRight")
man.runAction(repeatAnimation, withKey: "Run")
}
if buttonLeft.containsPoint(location) {
man.xScale = -1
man.runAction(repeatMoveGroundLeft, withKey: "MoveLeft")
man.runAction(repeatAnimation, withKey: "Run")
}
if buttonJump.containsPoint(location) {
if(condition == 1) {
man.physicsBody?.velocity = CGVectorMake(0, 0)
man.physicsBody?.applyImpulse(CGVectorMake(0, 400))
man.runAction(jumpingAnimation)
condition = 0
}
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if buttonRight.containsPoint(location) {
man.removeActionForKey("MoveRight")
man.removeActionForKey("Run")
man.texture = nuetralMan
}
if buttonLeft.containsPoint(location) {
man.removeActionForKey("MoveLeft")
man.removeActionForKey("Run")
man.texture = nuetralMan
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if buttonRight.containsPoint(location) {
man.removeActionForKey("MoveRight")
man.removeActionForKey("Run")
man.texture = nuetralMan
}
if buttonLeft.containsPoint(location) {
man.removeActionForKey("MoveLeft")
man.removeActionForKey("Run")
man.texture = nuetralMan
}
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
var thirdBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
thirdBody = contact.bodyB
} else {
firstBody = contact.bodyB
thirdBody = contact.bodyA
}
if firstBody.categoryBitMask == manGroup || secondBody.categoryBitMask == groundGroup {
print("contact")
condition = 1
man.texture = nuetralMan
}
if firstBody.categoryBitMask == manGroup || thirdBody.categoryBitMask == arrowGroup {
print("dead")
}
}
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
}
我尝试在 didBeginContact 方法中打印内容,但没有任何反应。我花了三天时间试图解决这个问题。据我所知,我所有的categoryBitMasks和CollisionTestBitMask都设置正确。非常感谢任何帮助。
最佳答案
您对 didBeginContact() 的定义隐藏在touchesEnded() 函数中。如果您拉直支撑,可能会有所帮助:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
// for touches loop (skipped)
}
} // Add this closing brace
func didBeginContact(contact: SKPhysicsContact) {
// contact body (skipped)
}
您还需要在 update() 之后删除多余的大括号:
// ...skipped
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
// } // get rid of this one
}
希望有帮助。
关于swift - didBeginContact 函数未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379294/
我有两个节点和一个 bool 值。很简单。当节点 A 联系节点 B 并且 bool 值为 0 时,什么也没有发生。但是,如果 bool 值为 1,则通过 didBeganContact 方法删除节点
这里是 swift(和编程)菜鸟。我正在 swift spriteKit 中制作一个简单的突破游戏。它正在工作,但现在我想为某些 block 添加“健康”,但遇到了一些问题。我搜索过谷歌,但什么也没找
所以我已经在这个游戏上工作了一段时间,所有的工作都按照我想要的方式进行,但是我已经在 didBeginContact 上停留了几天,我想我无法理解它它看起来不错,我检查了几个论坛并得到了几乎相同的结果
我已经浏览了所有我可以理解的示例,但没有任何内容与我的问题相关。我有 SKPhysicsContactDelegate 并且添加了 self.physicalsworld.contactDelegat
我的 didBeginContact 方法应该在球击中时移除我的砖 block Sprite 。但是,它没有发挥作用。当我运行它时,当我击中桨和击中砖 block 时,我会得到 NSLog“桨”。我已
知道为什么我的 didBeginContact 方法从不触发吗?球体和矩形实际上相互接触...... 这里我声明了两个类别 static const uint32_t rectangleCategor
当我将 println() 放入我的 didBeginContact 时,没有任何打印。因此,我假设我的 didBeginContact 不起作用。为什么?我还有一个枚举(对撞机类型),其中列出了 C
当我的两个 SKSpriteNode 接触时,didBeginContact 函数没有被调用: func didBegin(_ contact: SKPhysicsContact) { let
在我的游戏中,我有从一个点向前发射的激光。我使用此代码拉伸(stretch)激光 Sprite 并移动它以模拟更新函数中的运动: let height = size.height yScale +=
我正在尝试创建一个程序,无论我的宇宙飞船越过一个圆圈,它都会打印出一些东西,但是当我把宇宙飞船放在圆圈上时,它没有打印任何东西。我是否构建了错误的 didBeginContact 方法?我是否设置了错
我有 2 个 SKSpriteNode 需要检测其接触。我尝试了各种方法并查找了很多东西,但似乎无法得到答案。下面是我的代码。 controlCircle 是一个类级别的变量,因为它需要在其他方法中使
我有两个 sprite 节点,我已确保 self.size 和它们各自的物理体的大小相同,但我仍然遇到这样一个非常奇怪的行为: 这张照片是在检测到碰撞时拍摄的,我暂停了场景。为什么会这样? 这里是设置
我为每个想要相互交互的不同对象创建了一个 struct PhysicsCatagory struct PhysicsCatagory { static let Blade : UInt32 =
我正在制作一个需要我的 spriteNodes 发生碰撞的游戏。我遵循了一个教程,但它对我不起作用。 当我运行我的游戏并且两个对象发生碰撞时,输出监视器中没有 println("beginContac
在一款使用 Sprite Kit 以及 Sprite Kit 内置物理引擎中的接触检测功能的 iOS 游戏中,每当英雄与敌人接触时,我都会将英雄的生命值减一。这是通过 didBeginContact
假设两次接触同时发生。它们的 didBeginContact 函数调用会在完全相同的时间发生(我猜是在不同的线程中),还是一个会在另一个之后发生? 最佳答案 否 - 为场景中接触的每对节点调用 did
我是 swift 语言的新手,遇到了一些问题。我猜这是一个简单的错误,但无论如何我似乎找不到问题。我正在制作一个游戏。当你输了之后,我会在屏幕上创建一个重播按钮。单击后,我开始重置游戏。 我基本上重置
我正在解决 SpriteKit 的一些碰撞检测问题。我在 didBeginContact 中触发了正确的碰撞,这很棒。 现在我遇到了一个范围问题,我无法根据碰撞命中之一在场景中删除和添加子项。第一次检
func didBeginContact(contact: SKPhysicsContact) { if contact.bodyA.categoryBitMask == Physic
我一直在开发一款游戏,每次我的测试节点与我的检查节点接触时都必须执行测试。由于这两个节点是游戏中唯一检查碰撞的节点,因此我决定使用此方法来启动测试。 问题是当 2 个节点物理交叉时,我一直无法让碰撞发
我是一名优秀的程序员,十分优秀!