连续 Touch/Moving Swift & SpriteKit
Continuous Touch/Moving Swift & SpriteKit
我正在 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
}
我真的不太明白你想要什么,但我尝试写一些代码:
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))
}
}
就我个人而言,我会创建一个控制器 class,它具有更新功能,您可以轮询场景更新以确定按钮状态是打开还是关闭,然后关闭(触摸开始放置按钮在向下状态下,touch end 将其置于向上状态。更新轮询状态,并根据状态执行操作)但在您的情况下,为了在不更改大量代码的情况下保持简单,我只使用 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
}
我建议使用下面的代码,它很简单。当您的触摸结束时,只需删除移动动作。
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()
}
我正在 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
}
我真的不太明白你想要什么,但我尝试写一些代码:
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))
}
}
就我个人而言,我会创建一个控制器 class,它具有更新功能,您可以轮询场景更新以确定按钮状态是打开还是关闭,然后关闭(触摸开始放置按钮在向下状态下,touch end 将其置于向上状态。更新轮询状态,并根据状态执行操作)但在您的情况下,为了在不更改大量代码的情况下保持简单,我只使用 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
}
我建议使用下面的代码,它很简单。当您的触摸结束时,只需删除移动动作。
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()
}