大佬教程收集整理的这篇文章主要介绍了swift – Sprite Kit physicsBody.resting行为,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
import SpriteKit class GameScene: SKScene,SKPhysicsContactDelegate { var hero:SKSpriteNode! override func didMoveToView(view: SKView) { self.physicsWorld.gravity = CGVectorMake(0,0) self.physicsWorld.contactDelegate = self self.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.framE) hero = SKSpriteNode(imagenamed: "Spaceship") hero.position = CGPoint(x:CGRectGetMidX(self.framE),y:CGRectGetMidY(self.framE)) hero.zPosition = 10.0 hero.physicsBody = SKPhysicsBody(circLeofRadius: hero.size.width/2) hero.physicsBody.allowsRotation = false hero.physicsBody.linearDamping = 0.5 self.addChild(hero) } override func update(currentTime: CFTimeInterval) { if hero.physicsBody.resTing { println("resTing") } else { println("moving") } } }
令我惊讶的是,结果如下:
移动
休息
移动
(n次相同)
移动
休息
那么为什么英雄在移动,尽管我什么也没做.节点移动N次并休息(休息),之后继续移动.
func speed(veLocity:CGVector) -> Float { let dx = Float(veLocity.dX); let dy = Float(veLocity.dy); return sqrtf(dx*dx+dy*dy) } func angularSpeed(veLocity:CGFloat) -> Float { return abs(Float(veLocity)) } // This is a more reliable test for a physicsBody at "rest" func nearlyAtRest(node:SKNodE) -> Bool { return (self.speed(node.physicsBody.veLocity)<self.verysmallValue && self.angularSpeed(node.physicsBody.angularVeLocity) < self.verysmallvalue) } override func update(_ currentTime: TimeInterval) { /* Enumerate over child nodes with names starTing with "circle" */ enumerateChildNodesWithName("circle*") { node,stop in if (node.physicsBody.resTing) { println("\(node.Name) is resTing") } if (self.nearlyAtRest(nodE)) { println("\(node.Name) is nearly resTing") } } }
以上是大佬教程为你收集整理的swift – Sprite Kit physicsBody.resting行为全部内容,希望文章能够帮你解决swift – Sprite Kit physicsBody.resting行为所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。