sam*_*ndo 4 ios sprite-kit skspritenode swift
我现在正在使用SpriteKit,我遇到了一个看起来很简单但我在互联网上找不到的问题.我有三个按钮形状像平行四边形堆叠在一起,它看起来像这样: 按钮的屏幕截图
let button = SKSpriteNode(imageNamed: "playbutton")
let leaderButton = SKSpriteNode(imageNamed: "leaderbutton")
let homeButton = SKSpriteNode(imageNamed: "homebutton")
button.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(button)
leaderButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height/2)
addChild(leaderButton)
homeButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height)
addChild(homeButton)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if button.containsPoint(location) {
button.runAction(SKAction.scaleTo(0.8, duration: 0.1))
}
else if leaderButton.containsPoint(location) {
leaderButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
}
else if homeButton.containsPoint(location) {
homeButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我检测触摸的方式.问题是它们重叠,因为精灵实际上是一个矩形,所以当我尝试点击第二个按钮的左上角时,顶部按钮会检测到它.我想知道有一种方法只能在纹理中检测触摸,就像你可以将物理体设置为纹理一样.感谢你给与我的帮助!
链接现在工作.
所以我尝试了这个:
button.position = CGPoint(x: size.width/2, y: size.height/2)
button.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "playbutton"), size: button.size)
button.physicsBody?.dynamic = false
addChild(button)
leaderButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height/2)
leaderButton.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "leaderbutton"), size: leaderButton.size)
leaderButton.physicsBody?.dynamic = false
addChild(leaderButton)
homeButton.position = CGPoint(x: size.width/2, y: size.height/2 - button.size.height)
homeButton.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "homebutton"), size: homeButton.size)
homeButton.physicsBody?.dynamic = false
addChild(homeButton)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if physicsWorld.bodyAtPoint(location)?.node == button {
button.runAction(SKAction.scaleTo(0.8, duration: 0.1))
print("play")
}
if physicsWorld.bodyAtPoint(location)?.node == leaderButton {
leaderButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
print("leader")
}
if physicsWorld.bodyAtPoint(location)?.node == homeButton {
homeButton.runAction(SKAction.scaleTo(0.8, duration: 0.1))
}
}
}
Run Code Online (Sandbox Code Playgroud)
它仍然记录整个帧而不仅仅是物理体.请参阅链接以查看按钮及其坐标的交叉方式.
如果将物理主体附加到每个按钮,则可以检测触摸所在的物理主体.
您可以SKSpriteNode使用SKPhysicsBody(texture:size:)或从按钮的纹理(假设按钮是)生成物理实体SKPhysicsBody(texture:alphaThreshold:size:),或者您可以创建CGPath描述按钮的形状和使用SKPhysicsBody(polygonFromPath:).将正文分配给按钮的physicsBody属性.假设您实际上并不希望物理模拟器移动按钮,请将每个主体的dynamic属性设置为false.
然后你可以physicsWorld.bodyAtPoint(_:)用来获得一个触摸的物体(physicsWorld你的属性在哪里SKScene).使用body的node属性返回按钮节点.如果实体重叠,则bodyAtPoint返回任意实体.
physicsWorld.enumerateBodiesAtPoint(_:usingBlock:)如果你需要触摸所有的身体,你可以使用.
一个完全不同的方法,如果你可以创建CGPath描述按钮的形状,是使用SKScene.convertPointFromView(_:)然后SKNode.convertPoint(_:fromNode:_)将点转换为按钮的坐标系,然后使用CGPathContainsPoint(一个全局函数)来检测该点是否在描述按钮的路径中形状.
| 归档时间: |
|
| 查看次数: |
1577 次 |
| 最近记录: |