- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当物体在两个事物之间通过时,我试图增加我的分数但是我的函数 didBeginContact 没有被调用我试图添加一个断点但是调试但它仍然没有被调用。
游戏场景代码
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let birdGroup: UInt32 = 0x1 << 0
let enemeyObjectsGroup: UInt32 = 0x1 << 1
let openingGroup: UInt32 = 0x1 << 2
enum objectsZPositions: CGFloat {
case backgorund = 0
case ground = 1
case pipes = 2
case bird = 3
case score = 4
case gameOver = 5
}
var movingGameObjects = SKNode()
var backgorund = SKSpriteNode()
var bird = SKSpriteNode()
var pipeSpeed: NSTimeInterval = 10 // pipe speed
var pipeSpawned: Int = 0
var gameOver: Bool = false
var scoreLabelNode = SKLabelNode()
var score: Int = 0
var gameOverLabelNode = SKLabelNode()
var gameOverStatusNode = SKLabelNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
/*
let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 45;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)
*/
self.addChild(movingGameObjects)
// Creating Background
createBackgorund()
// Physics
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -12) // gravity of the bird
// Adding the bird
let birdTexture1 = SKTexture(imageNamed: "bird1.png")
let birdTexture2 = SKTexture(imageNamed: "bird2.png")
let birdTexture3 = SKTexture(imageNamed: "bird3.png")
let birdTexture4 = SKTexture(imageNamed: "bird4.png")
let birdTexture5 = SKTexture(imageNamed: "bird5.png")
let birdTexture6 = SKTexture(imageNamed: "bird6.png")
let birdTexture7 = SKTexture(imageNamed: "bird7.png")
let birdTexture8 = SKTexture(imageNamed: "bird8.png")
let flyAnimation = SKAction.animateWithTextures([birdTexture1,birdTexture2,birdTexture3,birdTexture4,birdTexture5,birdTexture6,birdTexture7,birdTexture8], timePerFrame: 0.1)
let flyForever = SKAction.repeatActionForever(flyAnimation)
bird = SKSpriteNode(texture: birdTexture1)
bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
bird.zPosition = objectsZPositions.bird.rawValue
bird.runAction(flyForever, withKey: "birdFly")
bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/2)
bird.physicsBody?.categoryBitMask = birdGroup
bird.physicsBody?.categoryBitMask = openingGroup | enemeyObjectsGroup
bird.physicsBody?.collisionBitMask = enemeyObjectsGroup
bird.physicsBody?.allowsRotation = false
self.addChild(bird)
// Adding ground
let ground = SKNode()
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, 1))
ground.physicsBody?.dynamic = false // dynamic property we dont want the ground to be affected by the gravity
ground.position = CGPoint(x: CGRectGetMidY(self.frame), y: 0) // ground on the border fo the screen
ground.zPosition = objectsZPositions.ground.rawValue
ground.physicsBody?.categoryBitMask = enemeyObjectsGroup
ground.physicsBody?.collisionBitMask = birdGroup
ground.physicsBody?.contactTestBitMask = birdGroup
self.addChild(ground)
let pipesTimer = NSTimer.scheduledTimerWithTimeInterval(2.5, target: self, selector: "loadPipes", userInfo: nil, repeats: true)
// Score Label Node
scoreLabelNode = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
scoreLabelNode.fontSize = 50
scoreLabelNode.fontColor = SKColor.whiteColor()
scoreLabelNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.height - 50) // position of the Score Label
scoreLabelNode.text = "0"
scoreLabelNode.zPosition = objectsZPositions.score.rawValue
self.addChild(scoreLabelNode)
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == openingGroup || contact.bodyB.categoryBitMask == openingGroup {
score += 1
scoreLabelNode.text = "\(score)"
}
}
func loadPipes() {
pipeSpawned += 2
if pipeSpawned % 10 == 0 {
pipeSpeed -= 0.5 // making the game thougher
}
let gap: CGFloat = bird.size.height * 2.1 // the gap between 2 pipes
let pipe1Texture = SKTexture(imageNamed: "pipe1.png")
let pipe2Texture = SKTexture(imageNamed: "pipe2.png")
let pipe1 = SKSpriteNode(texture: pipe1Texture)
let pipe2 = SKSpriteNode(texture: pipe2Texture)
let randomY: CGFloat = CGFloat(arc4random_uniform(UInt32(self.frame.height * 0.7)))
pipe1.position = CGPoint(x: self.frame.width + pipe1.size.width, y: pipe1.size.height/2 + 170 + randomY + gap/2)
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: pipe1.size)
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.categoryBitMask = enemeyObjectsGroup
pipe1.physicsBody?.collisionBitMask = birdGroup
pipe1.physicsBody?.contactTestBitMask = birdGroup
pipe1.zPosition = objectsZPositions.pipes.rawValue
movingGameObjects.addChild(pipe1)
let movePipe = SKAction.moveToX(-pipe1.size.width, duration: pipeSpeed)
let removePipe = SKAction.removeFromParent()
pipe1.runAction(SKAction.sequence([movePipe,removePipe]))
pipe2.position = CGPoint(x: self.frame.width + pipe2.size.width, y: -pipe2.size.height/2 + 100 + randomY - gap/2)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: pipe2.size)
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.categoryBitMask = enemeyObjectsGroup
pipe2.physicsBody?.collisionBitMask = birdGroup
pipe2.physicsBody?.contactTestBitMask = birdGroup
pipe2.zPosition = objectsZPositions.pipes.rawValue
movingGameObjects.addChild(pipe2)
pipe2.runAction(SKAction.sequence([movePipe,removePipe]))
// crossing between pipes
let crossing = SKNode()
crossing.position = CGPoint(x: pipe1.position.x + pipe1.size.width/2, y: CGRectGetMidY(self.frame))
crossing.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.height))
crossing.physicsBody?.dynamic = false
crossing.physicsBody?.categoryBitMask = openingGroup
crossing.physicsBody?.contactTestBitMask = birdGroup
movingGameObjects.addChild(crossing)
crossing.runAction(SKAction.sequence([movePipe, removePipe]))
}
func createBackgorund() {
let backgorundTexture = SKTexture(imageNamed: "background")
let moveBakground = SKAction.moveByX(-backgorundTexture.size().width, y: 0, duration: 25) // background speed
let replaceBackground = SKAction.moveByX(backgorundTexture.size().width, y: 0, duration: 0)
let backgorundSequence = SKAction.sequence([moveBakground, replaceBackground])
let moveBackgroundForever = SKAction.repeatActionForever(backgorundSequence)
for var i: CGFloat = 0; i < 2; i++ {
backgorund = SKSpriteNode(texture: backgorundTexture)
backgorund.position = CGPoint(x: backgorundTexture.size().width/2 + i * backgorundTexture.size().width, y:CGRectGetMidY(self.frame))
backgorund.size.height = self.frame.height
backgorund.zPosition = objectsZPositions.backgorund.rawValue
backgorund.runAction(moveBackgroundForever)
movingGameObjects.addChild(backgorund)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
if gameOver == false {
bird.physicsBody?.velocity = CGVectorMake(0, 0)
bird.physicsBody?.applyImpulse(CGVectorMake(0, 60))
// making the bird face up and down when tapped
let rotateUp = SKAction.rotateToAngle(0.3, duration: 0)
bird.runAction(rotateUp)
} else {
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as SKView!
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameOver == false {
let rotateDown = SKAction.rotateToAngle(-0.2, duration: 0)
bird.runAction(rotateDown)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
最佳答案
你在这一行中有一个错误:
bird.physicsBody?.categoryBitMask = openingGroup | enemeyObjectsGroup
应该是这样的:
bird.physicsBody?.categoryBitMask = birdGroup
也尝试像这样使用 didBeginContact:
- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
//Handle contacts here
}
How you can handle contacts 已经在这里评论了很多次,所以我将跳过那个:
https://stackoverflow.com/a/20604762/3402095
https://stackoverflow.com/a/26723392/3402095
提示:
请注意,不会检测到两个静态物体之间的接触 (node.physicsBody.dynamic = false
)。我不确定您对哪些接触感兴趣(可能在鸟和交叉/管道之间),但即使您当前的设置很好(鸟是动态的,交叉和管道是静态的),您也应该意识到这一点。
关于ios - 未调用 Swift func didBeginContact,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240121/
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前等待bepreEach()块中的两个异步函数
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前,在beforeEach()块中等待两个Ja
为什么是Func<>从 Expression> 创建通过 .Compile() 比仅使用 Func<> 慢得多直接声明? 我刚从使用 Func 更改为直接声明为从 Expression> 创建的一个在
我正在创建一个 Validator类(class)。我正在尝试实现 Linq SelectMany我的验证器的扩展方法能够使用 Linq 查询组合表达式并验证最终结果,即使基础值发生变化也是如此。 下
function sum(a) { let currentSum = a; function f(b) { currentSum += b; return f; }
我只知道i = i++;是未定义的行为,但是如果一个表达式中调用了两个或多个函数,并且所有功能是一样的。是未定义吗?例如: int func(int a) { std::cout << a <
我如何定义一个对象,以便作用于它的任何函数都作用于它的一个字段?这可能吗? class Mydata(object): def __init__(self, val): sel
这个问题一直很有趣,尽管它不一定很整洁。我有以下代码: import random def d(m): return random.randint(1, m) print(3*d(6)) 这将
能否请您解释一下使用 func.apply(null, arr) 的区别?和 func.apply(this, arr)在下面的代码示例中? var Foo = function() { fu
我想收集/运行任务,然后对它们执行 Task.WhenAll。 var tasks = new List(); foreach (var thing in things) { tasks.Add(
我有以下代码: static Func s_objToString = (x) => x.ToString(); static Func s_stringToString = s_objToStrin
相关主题: Create Expression> dynamically 我在互联网上搜索但所有样本都解释了 Expression来自 T ? 谢谢 编辑 1) T输入我的代码在运行时确定,例如我想用
我正在尝试使用 LinqKit 动态生成 linqtosql 查询.在将表达式发送到 LinqKit 之前,我想检查要为预测添加的字段。所以我想出了一些想法,比如 Expression> GetPr
我遇到了一些麻烦,我写了一个 Func,IDE 不喜欢我在 Func 体内调用 Func ,我不太明白为什么,因为如果我将这个确切的代码放在方法体中,并使用相同的返回类型和参数,那么它就可以工作。 代
我现在正在学习使用 Class 语法来创建 React 组件,请注意我现在必须声明这样的方法: class Foo extends React.Component { ... bar
下面两种说法有区别吗?他们都工作。 if ( ((Func)(()=>true))() ) { .... }; if ( new Func(()=>true)()) { .... }; 最佳答案 不,
这个问题在这里已经有了答案: Difference between func() and (*this).func() in C++ (4 个答案) 关闭 6 年前。 如果我有一个带有虚函数而没有自
主要问题是“是否可以将任何类型的 func 作为参数传递以及如何传递?”。我正在学习 Go 并且想像这样制作我自己的异步包装函数: func AsyncFunc(fn func(), args ...
有没有简单的转换方法 Expression> 到 Expression> T从哪里继承自TBase? 最佳答案 只要 T 派生自 TBase,您就可以使用原始表达式的主体和参数直接创建所需类型的表达式
我有以下方法,其中 T 在 Func 中使用: public void DoSomething(string someString, Func someMethod) { if(some
我是一名优秀的程序员,十分优秀!