我正在Swift和SpriteKit中创建一个Side Scroller Mario游戏。我目前正在移动播放器,但你必须继续点击然后移动。我所希望的是,如果你握住它它会移动,你不必快速点击屏幕。提前谢谢!!!!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.y > 400{
move = true
}else{
if location.x < CGRectGetMidX(self.frame){
player.physicsBody?.applyImpulse(CGVector(dx: -30, dy: 0))
//tap somewhere above this to make character jump
if(location.y > 250) {
player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200))
}
} else if location.x > CGRectGetMidX(self.frame){
player.physicsBody?.applyImpulse(CGVector(dx: 30, dy: 0))
//tap somewhere above this to make character jump
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
move = false
}
答案 0 :(得分:0)
我真的不太明白你想要什么,但我尝试写一些代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.y > 400{
move = true
}else{
if location.x < CGRectGetMidX(self.frame){
movePlayer(-30,dy:0)
//tap somewhere above this to make character jump
if(location.y > 250) {
player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200))
}
} else if location.x > CGRectGetMidX(self.frame){
movePlayer(30,dy:0)
//tap somewhere above this to make character jump
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.y > 400{
move = false
}else {
if location.x < CGRectGetMidX(self.frame) || if location.x > CGRectGetMidX(self.frame) {
movePlayer(0,dy:0)
}
}
}
}
func movePlayer(dx:CGFloat,dy:CGFloat) {
if (player.physicsBody.resting) {
player.physicsBody?.applyImpulse(CGVector(dx, dy: dy))
}
}
答案 1 :(得分:0)
就个人而言,我会创建一个控制器类,它具有您在场景更新中轮询的更新功能,以确定按钮状态是向上还是向下,然后关闭(触摸开始将按钮置于关闭状态,触摸结束)将它置于up状态。更新轮询状态,并根据状态执行操作)但是在你的情况下,为了简单而不改变很多代码,我只想使用SKAction.moveBy
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if location.y > 400{
move = true
}else{
//tap somewhere above this to make character jump
if(location.y > 250) {
player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200))
}
else
{
let direction = (location.x > CGRectGetMidX(self.frame)) ? 30 : -30
let move = SKAction.moveBy(x:direction,y:0,duration:0)
let rep = SKAction.repeatActionForever(move)
player.runAction(rep,forKey:"Moving")
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
player.removeActionForKey("Moving")
move = false
}
答案 2 :(得分:0)
我建议使用下面的代码,它很简单。只需在触摸结束时删除移动动作即可。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let touchPoint = touch.location(in: self)
let leftmoveAction = SKAction.move(to: CGPoint(x: player.position.x - 200, y: player.position.y), duration: 1)
let rightMoveAction = SKAction.move(to: CGPoint(x: player.position.x + 200, y: player.position.y), duration: 1)
if touchPoint.y > 80 {
}else{
print(touchPoint)
if touchPoint.x < self.size.width / 2 {
if player.position.x > 70 {
player.run(leftmoveAction)
}
}else{
if player.position.x < 350 {
player.run(rightMoveAction)
}
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
player.removeAllActions()
}