有时精灵不会从物理边界反弹
sometimes sprites will not bounce off the physics boundary
我正在开始一个新游戏,但马上就遇到了一个奇怪的问题。在我的 didMoveToView 函数中,我有以下内容可以在框架(整个屏幕)
内对我的精灵设置边界
self.physicsBody=SKPhysicsBody(edgeLoopFromRect: self.frame)
下面的代码在触摸点添加了一个SKSpriteNode,并添加了一个rotatebyangle动作,并且永远重复
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(balloonWorld)
let nodeAtPoint = self.nodeAtPoint(location)
if(nodeAtPoint.name == nil) {
let location = touch.locationInNode(self)
let randNum:Int = Int.random(min: 0, max: 7)
var stringColor:String = (balloonColors.objectAtIndex(randNum) as String)
stringColor = stringColor.stringByReplacingOccurrencesOfString("face",
withString :"")
let sprite = Balloon(theColor:stringColor)
//let spriteFileName:String = balloonColors.objectAtIndex(randNum) as String
//let sprite = SKSpriteNode(imageNamed:spriteFileName)
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = location
sprite.zPosition = SceneLevel.hero.rawValue
balloonWorld!.addChild(sprite)
let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
sprite.runAction(SKAction.repeatActionForever(action))
} else {
nodeAtPoint.removeFromParent()
println(nodeAtPoint.name)
}
}
}
我按如下方式设置了气球世界:
balloonWorld = SKNode()
self.addChild(balloonWorld!)
我的问题是有时气球精灵不会从边缘反弹,而是继续穿过边缘,再也看不到了。
有什么建议吗?
谢谢,
肯
根据请求,这里是设置物理体的代码
气球:
class Balloon: SKNode {
// properties
var myColor:String?
var objectSprite:SKSpriteNode?
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(theColor:String){
super.init()
self.myColor=theColor
let imageFileName = "\(theColor)face"
let objectSprite:SKSpriteNode = SKSpriteNode(imageNamed:imageFileName)
objectSprite.physicsBody = SKPhysicsBody(circleOfRadius: objectSprite.size.height / 2.0)
objectSprite.physicsBody?.affectedByGravity = true
objectSprite.name = theColor + " balloon"
addChild(objectSprite)
}
}
我没有在边界上设置物理体,因为我只需要线 self.physicsBody = SKPhysicsBody(edgeLoopFrom Rect : self:frame)
因为边界具有相同的框架。
我尝试添加以下内容,但这并没有改变行为:
let borderShape=SKShapeNode(rect: CGRectMake(self.frame.origin.x+2, self.frame.origin.y+2, self.frame.size.width-4, self.frame.size.height-4))
borderShape.fillColor=SKColor.clearColor()
borderShape.strokeColor=SKColor.blackColor()
borderShape.lineWidth=1
borderShape.physicsBody?.categoryBitMask=BodyType.boundary.rawValue
borderShape.zPosition=SceneLevel.border.rawValue
borderShape.physicsBody=SKPhysicsBody(edgeLoopFromRect: borderShape.frame)
balloonWorld!.addChild(borderShape)
创建一个枚举来区分两种类型的精灵:
enum ColliderType {
case sprite1 = 1
case sprite2 = 2
}
您需要像这样添加接触和碰撞位掩码:
borderShape.physicsBody?.contactTestBitMask = ColliderType.sprite2.rawValue
同时添加一个collisionBitMask:
borderShape.physicsBody?.collisionBitMask = ColliderType.sprite2.rawValue
对 sprite2 做同样的事情,将它的 collisionBitMask 和 contactTestBitMask 设置为 sprite1 的原始值。确保将此 class 设置为 contactDelegate,以便通过一个函数收到警告,该函数会在两个对象发生碰撞时通知您:
self.physicsWorld?.contactDelegate = self
func didBeginContact(contact: SKPhysicsContact) {
[handle the contact however you would like to here]
}
希望对您有所帮助!
LearnCocos2D(Steffen Itterheim)在他的评论中为我指出了正确的方向。我正在使用 moveTo/moveBy 并且物理引擎无法正确处理它。一旦我简单地根据冲动做出动作,一切都很好。
谢谢史蒂芬
我正在开始一个新游戏,但马上就遇到了一个奇怪的问题。在我的 didMoveToView 函数中,我有以下内容可以在框架(整个屏幕)
内对我的精灵设置边界self.physicsBody=SKPhysicsBody(edgeLoopFromRect: self.frame)
下面的代码在触摸点添加了一个SKSpriteNode,并添加了一个rotatebyangle动作,并且永远重复
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(balloonWorld)
let nodeAtPoint = self.nodeAtPoint(location)
if(nodeAtPoint.name == nil) {
let location = touch.locationInNode(self)
let randNum:Int = Int.random(min: 0, max: 7)
var stringColor:String = (balloonColors.objectAtIndex(randNum) as String)
stringColor = stringColor.stringByReplacingOccurrencesOfString("face",
withString :"")
let sprite = Balloon(theColor:stringColor)
//let spriteFileName:String = balloonColors.objectAtIndex(randNum) as String
//let sprite = SKSpriteNode(imageNamed:spriteFileName)
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = location
sprite.zPosition = SceneLevel.hero.rawValue
balloonWorld!.addChild(sprite)
let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
sprite.runAction(SKAction.repeatActionForever(action))
} else {
nodeAtPoint.removeFromParent()
println(nodeAtPoint.name)
}
}
}
我按如下方式设置了气球世界:
balloonWorld = SKNode()
self.addChild(balloonWorld!)
我的问题是有时气球精灵不会从边缘反弹,而是继续穿过边缘,再也看不到了。
有什么建议吗?
谢谢, 肯
根据请求,这里是设置物理体的代码
气球:
class Balloon: SKNode {
// properties
var myColor:String?
var objectSprite:SKSpriteNode?
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(theColor:String){
super.init()
self.myColor=theColor
let imageFileName = "\(theColor)face"
let objectSprite:SKSpriteNode = SKSpriteNode(imageNamed:imageFileName)
objectSprite.physicsBody = SKPhysicsBody(circleOfRadius: objectSprite.size.height / 2.0)
objectSprite.physicsBody?.affectedByGravity = true
objectSprite.name = theColor + " balloon"
addChild(objectSprite)
}
}
我没有在边界上设置物理体,因为我只需要线 self.physicsBody = SKPhysicsBody(edgeLoopFrom Rect : self:frame)
因为边界具有相同的框架。
我尝试添加以下内容,但这并没有改变行为:
let borderShape=SKShapeNode(rect: CGRectMake(self.frame.origin.x+2, self.frame.origin.y+2, self.frame.size.width-4, self.frame.size.height-4))
borderShape.fillColor=SKColor.clearColor()
borderShape.strokeColor=SKColor.blackColor()
borderShape.lineWidth=1
borderShape.physicsBody?.categoryBitMask=BodyType.boundary.rawValue
borderShape.zPosition=SceneLevel.border.rawValue
borderShape.physicsBody=SKPhysicsBody(edgeLoopFromRect: borderShape.frame)
balloonWorld!.addChild(borderShape)
创建一个枚举来区分两种类型的精灵:
enum ColliderType {
case sprite1 = 1
case sprite2 = 2
}
您需要像这样添加接触和碰撞位掩码:
borderShape.physicsBody?.contactTestBitMask = ColliderType.sprite2.rawValue
同时添加一个collisionBitMask:
borderShape.physicsBody?.collisionBitMask = ColliderType.sprite2.rawValue
对 sprite2 做同样的事情,将它的 collisionBitMask 和 contactTestBitMask 设置为 sprite1 的原始值。确保将此 class 设置为 contactDelegate,以便通过一个函数收到警告,该函数会在两个对象发生碰撞时通知您:
self.physicsWorld?.contactDelegate = self
func didBeginContact(contact: SKPhysicsContact) {
[handle the contact however you would like to here]
}
希望对您有所帮助!
LearnCocos2D(Steffen Itterheim)在他的评论中为我指出了正确的方向。我正在使用 moveTo/moveBy 并且物理引擎无法正确处理它。一旦我简单地根据冲动做出动作,一切都很好。
谢谢史蒂芬