- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于某种原因,在下面的代码中,敌方细菌在相互接触时移动了食物(豌 bean )。我没有对它们进行编码来测试彼此的联系。有什么帮助吗?我正在努力使两种类型的 Sprite 不可能发生碰撞。
import SpriteKit
var apple=SKSpriteNode()
class GameScene: SKScene, SKPhysicsContactDelegate {
var bg=SKSpriteNode()
var peas=SKSpriteNode()
var bacteria=SKSpriteNode()
var bactys_Y=[SKSpriteNode]()
var bactys_X=[SKSpriteNode]()
var border=CGRect()
// var gandolf=SKSpriteNode()
enum ColliderType:UInt32{
case Bug=1
case Food=2
case Enemy=4
case Gandolf=8
}
override func didMoveToView(view: SKView) {
border=CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: self.frame.height-200)
//gandolf.physicsBody=SKPhysicsBody(edgeLoopFromRect: border)
let bactPic=SKTexture(imageNamed: "bacteria.png")
/* Setup your scene here */
//bg info
self.physicsWorld.contactDelegate=self
let bg_texture=SKTexture(imageNamed: "wood-texture-pattern.jpg")
bg=SKSpriteNode(texture: bg_texture)
bg.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
bg.size.height=self.frame.height
bg.size.width=self.frame.width
// self.addChild(bg)
//bug info
let applePic=SKTexture(imageNamed:"bug.png")
apple=SKSpriteNode(texture:applePic)
apple.position=CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
print(self.frame.width)
// let bactPic=SKTexture(imageNamed: "bacteria.png")
//peas info
let peas_texture=SKTexture(imageNamed: "peas.png")
peas=SKSpriteNode(texture: peas_texture)
// peas.position=CGPointMake(200,300)
peas.physicsBody=SKPhysicsBody(circleOfRadius: peas_texture.size().width/4)
apple.physicsBody=SKPhysicsBody(circleOfRadius:applePic.size().width/2)
peas.physicsBody?.affectedByGravity=false
apple.physicsBody?.affectedByGravity=false
bg.zPosition=1
apple.zPosition=2
peas.zPosition=3
apple.physicsBody!.categoryBitMask=ColliderType.Bug.rawValue
apple.physicsBody!.contactTestBitMask=ColliderType.Food.rawValue
peas.physicsBody!.categoryBitMask=ColliderType.Food.rawValue
peas.physicsBody!.contactTestBitMask=ColliderType.Bug.rawValue
bacteria.physicsBody=SKPhysicsBody(circleOfRadius: bactPic.size().width*0.06/2)
bacteria.physicsBody!.categoryBitMask=ColliderType.Enemy.rawValue
bacteria.physicsBody!.contactTestBitMask=ColliderType.Bug.rawValue
// gandolf.physicsBody?.categoryBitMask=ColliderType.Gandolf.rawValue
// gandolf.physicsBody?.categoryBitMask=ColliderType.Enemy.rawValue
peas.setScale(0.4)
apple.setScale(0.5)
// bacteria info
// gandolf.color=UIColor.blackColor()
// self.addChild(gandolf)
self.addChild(apple)
// self.addChild(peas)
_=NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("spawnBacteria"), userInfo: nil, repeats: true)
spawnBacteria()
spawnFood()
}
func didBeginContact(contact: SKPhysicsContact) {
// peas.removeFromParent()
// spawnFood()
let first=contact.bodyA
let second=contact.bodyB
if first.categoryBitMask==ColliderType.Bug.rawValue || second.categoryBitMask==ColliderType.Bug.rawValue{
if first.categoryBitMask==ColliderType.Food.rawValue || second.categoryBitMask==ColliderType.Food.rawValue{
peas.removeFromParent()
spawnFood()
}
}
if first.categoryBitMask==ColliderType.Bug.rawValue || second.categoryBitMask==ColliderType.Bug.rawValue{
if first.categoryBitMask==ColliderType.Enemy.rawValue || second.categoryBitMask==ColliderType.Enemy.rawValue{
print("DEATH")
}
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
// print("x: "+String(apple.position.x))
// print("y: "+String(apple.position.y))
for node:SKSpriteNode in bactys_X{
if node.position.x<=0{
print("collide")
node.physicsBody?.velocity=CGVector(dx:0, dy:0)
node.physicsBody?.applyImpulse(CGVector(dx:6, dy:0))
}else if node.position.x>=self.frame.size.width-3{
node.physicsBody?.velocity=CGVector(dx:0,dy:0)
node.physicsBody?.applyImpulse(CGVector(dx:-6,dy:0))
}
}
for node:SKSpriteNode in bactys_Y{
if node.position.y>=self.frame.size.height-3{
node.physicsBody?.velocity=CGVector(dx:0,dy:0)
node.physicsBody?.applyImpulse(CGVector(dx:0,dy:-6))
}else if node.position.y<=200{
node.physicsBody?.velocity=CGVector(dx:0,dy:0)
node.physicsBody?.applyImpulse(CGVector(dx:0,dy:6))
}
}
}
func spawnFood(){
let peas_texture=SKTexture(imageNamed: "peas.png")
peas=SKSpriteNode(texture: peas_texture)
peas.physicsBody=SKPhysicsBody(circleOfRadius: peas_texture.size().width/4)
peas.zPosition=3
peas.physicsBody?.affectedByGravity=false
peas.physicsBody?.categoryBitMask=ColliderType.Food.rawValue
peas.physicsBody?.contactTestBitMask=ColliderType.Bug.rawValue
let x=CGFloat(arc4random_uniform(UInt32(self.frame.size.width-40)+20))
let y=CGFloat(arc4random_uniform(UInt32(self.frame.size.height-20-200))+200)
//print(self.frame.size.width)
print("height: "+String(self.frame.size.height))
print("x: "+String(x))
print("y: "+String(y))
peas.position=CGPointMake(x,y)
peas.setScale(0.4)
self.addChild(peas)
}
func spawnBacteria(){
let bactPic=SKTexture(imageNamed: "bacteria.png")
bacteria=SKSpriteNode(texture: bactPic)
bacteria.physicsBody=SKPhysicsBody(circleOfRadius: bactPic.size().width*0.06/2)
bacteria.physicsBody?.affectedByGravity=false
bacteria.physicsBody?.categoryBitMask=ColliderType.Enemy.rawValue
bacteria.physicsBody?.contactTestBitMask=ColliderType.Bug.rawValue
// bacteria.physicsBody?.contactTestBitMask=ColliderType.Gandolf.rawValue
bacteria.zPosition=4
let x=CGFloat(arc4random_uniform(UInt32(self.frame.size.width-40)+20))
let y=CGFloat(arc4random_uniform(UInt32(self.frame.size.height-20-200))+200)
bacteria.position=CGPointMake(x,y)
bacteria.setScale(0.06)
let decide=Int(arc4random_uniform(3))
self.addChild(bacteria)
if decide==0{
//move left
// let move=SKAction.moveByX(-6, y: 0, duration: 0.1)
// let start=SKAction.repeatActionForever(move)
//bacteria.runAction(start)
bacteria.physicsBody?.applyImpulse(CGVector(dx: -6,dy: 0))
bactys_X.append(bacteria)
}else if decide==1{
//move right
// let move=SKAction.moveByX(6, y: 0, duration: 0.1)
// let start=SKAction.repeatActionForever(move)
// bacteria.runAction(start)
bacteria.physicsBody?.applyImpulse(CGVector(dx: 6, dy: 0))
bactys_X.append(bacteria)
}else if decide==2{
//move up
// let move=SKAction.moveByX(0, y: 6, duration: 0.1)
// let start=SKAction.repeatActionForever(move)
// bacteria.runAction(start)
bacteria.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 6))
bactys_Y.append(bacteria)
}else{
//move down
// let move=SKAction.moveByX(0, y: -6, duration: 0.1)
// let start=SKAction.repeatActionForever(move)
// bacteria.runAction(start)
bacteria.physicsBody?.applyImpulse(CGVector(dx: 0,dy: -6))
bactys_Y.append(bacteria)
}
}
}
最佳答案
默认情况下,物理体的碰撞位掩码设置为 0xFFFFFFFF(与所有对象交互),其动态属性设置为 true。查看您的代码,似乎您尚未设置它们,因此它使用默认值。
您必须设置这些属性才能达到您想要的结果
...physicsBody.dynamic = false
...physicsBody.collisionBitMask = //set to something
关于swift - 如何禁用两个 SKSpriteNode 之间的接触?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35640272/
我的SKPhysicsbody有一个奇怪的行为。 首先让我向您展示我的主节点: self.physicsWorld.contactDelegate = self self.physicsWorld.g
嘿,我有一个选择器,它在mainBundle中搜索所有.aif文件,并允许用户选择它们。效果很好,但是后来我从项目和文件夹中删除了一些文件,但是它们并没有消失。有没有办法重新编译它或其他东西,因为它现
我需要说的是,一位开发人员告诉您应该创建一个 skphysicals 主体,以便该函数主体能够正常工作,那么是否有任何特定代码来创建该 Sprite 的 skphysicals 主体?因为我已经在物理
所以我在屏幕中央有一个 150 x 150 的主 Imageview,我以编程方式创建具有随机 x 和 y 值以及随机宽度/高度(这些值相同)的 View 。我希望 View 围绕主 ImageVie
我的 Contact 7 Form 样式的宽度一直有问题,这让我抓狂。 http://www.sportshosts.com/ 顶部表单 - 按钮不会与字段垂直对齐 顶部表单 - 当您缩小窗口时,它会
我正在尝试制作棋盘,但正方形的外观有点问题。棋盘配色方案工作正常,但由于某种原因,每个方 block 的 Angular 似乎没有接触。 这是我的 2 行代码片段,其中每个数据条目属于“light”类
标题解释得很清楚。这个怎么做?两个 div 的大小不同。两个 div 都在移动(我正在制作一个 Jquery 小游戏)。如果一个撞到另一个,我希望事情发生。 谢谢 最佳答案 没有onOverlap的J
我正在使用 cocos2dx 并设置两个具有相同 Physics Body 定义的 Sprites,如下所示 PhysicsBody *physicsBody=PhysicsBody::createB
我正在开发一个带有两个 div 的简单气球游戏。问题是当两个 div 相互接触时我无法触发功能。 最佳答案 您正在寻找边界框碰撞检测。如果你想引发一个事件,你必须反复测试,但最好只在游戏循环中对所有游
问题 我有很多 .php 文件,大部分包含 HTML,但顶部还有一些 PHP 行(例如表单触发代码或类似代码)。所以他们看起来像 My Website 目标 我的
我在 PostGIS 和 PostgreSQL 中有一个包含 LINESTRING 列的表。我正在构建一个查询,该查询是将此表与其自身连接的结果,并且我知道每个连接的几何列(称它们为 geom),即第
我正在尝试编写一个 python 脚本来修改我正在解析的文件中 标记的内容。由于它的速度,我为此使用 lxml.html(而不是 BeautifulSoup 等)。 script 标签的内容包含在注
block ?
Prettier 通常在 HTML 上做得非常好,但会缩进 pre>code blocks。 我总是希望预>代码块不缩进。 似乎我应该能够在 .prettierignore 中配置它,但找不到可行的方
我已成功将 pdf 文件从 URL 加载到 Web View 。我想在触摸 webview 后获取坐标,将其数据发送到服务器,以在服务器端的该位置添加一些文本。问题是当 webview 缩放或滚动到第
我目前想要获取的只是 gd:email 的属性,即“address=”,目前仅此而已。我可以得到 xml 部分,哎呀,只要它在标签内,我什至可以得到任何给定的想法,但要获得任何给定的属性,就像我的情况
我是一名优秀的程序员,十分优秀!