【发布时间】:2016-06-13 11:20:51
【问题描述】:
我想这样做,当玩家按下标签时,他必须再次按下屏幕上的一个点来选择建造房屋的地方。到现在为止,这就是我所得到的:当玩家按下标签“build”时,布尔值(“wantsToBuild”)被设置为 true,但现在显然房子是建在标签之上的。我不知道如何检查那个地方是否已经很忙,或者玩家是否可以在那里建造。我想过有一些占位符,但我不知道如何正确设置它们。你能帮我解决这个问题吗?谢谢你。 编辑:我已经按照答案中的建议更改了问题中的代码,但现在我遇到了几个问题:即使我',操纵杆(我之前没有提到过)也会移动m 不要触摸操纵杆本身。另外,即使我已经用打印语句检查了想要构建的值,并且它似乎设置为 false,但每当我按下屏幕时,总是会建造房子。你能帮我进一步吗?
类 GameScene:SKScene {
var ship = LCDShip()
var shipSpeed : CGFloat = 0.08
var base = SKSpriteNode(imageNamed: "base")
var joystick = SKSpriteNode(imageNamed: "joystick")
var joystickActive = false
var length:CGFloat! = nil
var xDist:CGFloat! = nil
var yDist:CGFloat! = nil
var deltaVector:CGVector! = nil
let build = SKLabelNode()
var wantsToBuild = false
override func didMoveToView(view: SKView) {
build.name = "build"
build.fontName = "Chalkduster"
build.fontSize = 25
build.text = "Build a house"
build.zPosition = 2
build.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
addChild(build)
backgroundColor = SKColor.blackColor()
ship.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
addChild(ship)
base.position = CGPoint(x: 150, y: 200)
base.setScale(2)
base.alpha = 0.3
addChild(base)
joystick.position = base.position
joystick.setScale(2)
joystick.alpha = 0.4
joystick.name = "base"
addChild(joystick)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let house = SKSpriteNode(imageNamed: "house")
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
let label = self.childNodeWithName("build")!
if node.name == "build"{
print("where should i build the hosue?")
wantsToBuild = true
} else if wantsToBuild == true && node.name != "house" && location.x <= label.position.x - 15 || location.x >= label.position.x + 15 || location.y >= label.position.y + 15 || location.y <= label.position.y - 15 {
house.position = location
house.name = "house"
addChild(house)
wantsToBuild = false
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
deltaVector = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(deltaVector.dy, deltaVector.dx)
length = base.frame.size.height/2
xDist = sin(angle - 1.57079633) * length
yDist = cos(angle - 1.57079633) * length
if(CGRectContainsPoint(base.frame, location)){
joystick.position = location
} else {
joystick.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)
}
ship.zRotation = angle - 1.57079633
}
}
override func update(currentTime: NSTimeInterval) {
if deltaVector != nil {
ship.position.x += deltaVector.dx * shipSpeed
ship.position.y += deltaVector.dy * shipSpeed
}
}
}
【问题讨论】:
标签: ios xcode swift touchesbegan