ios - 如何使用触摸快速制作角色 Controller ?

标签 ios swift sprite-kit

我想使用 Swift 在 Xcode 中制作一个简单的平台游戏,这样当我按住屏幕左侧时,它会不断向左移动直到放开,反之亦然。我已经添加了跳转功能,但如果您有任何其他建议,我愿意接受建议。

这是我的 GameController.swift 代码: 导入SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
let character = SKSpriteNode(imageNamed:"square.png")
let block = SKSpriteNode(imageNamed: "square.png")
    override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    //Physics
    self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
    self.physicsWorld.contactDelegate = self
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    //Character
    character.position = CGPoint(x:CGRectGetMidX(self.frame),   
y:CGRectGetMidY(self.frame))
    character.setScale(0.25)
    character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
        self.addChild(character)

        //block

        block.position = CGPoint(x: CGRectGetMidX(self.frame), y:  
        CGRectGetMidY(self.frame)*0.3)
        block.setScale(0.2)
        block.physicsBody = SKPhysicsBody(rectangleOfSize: block.size)
        block.physicsBody?.dynamic = false
        self.addChild(block)


}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */
    for touch:AnyObject in touches {
        let location = touch.locationInNode(self)

        if location.x < CGRectGetMidX(self.frame) && location.y <    
    CGRectGetMidY(self.frame)*0.7{
            character.physicsBody?.applyImpulse(CGVector(dx: -50, dy: 0))
        } else if location.x > CGRectGetMidX(self.frame) && location.y <           
    CGRectGetMidY(self.frame)*0.7{
            character.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 0))
        } else if location.y > character.position.y + 15  {
            character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
        }





        func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

        }
func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
                }
        }
}
}

最佳答案

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    character.removeActionForKey("moveAction")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch:AnyObject in touches {
        let location = touch.locationInNode(self)

        if location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{
            moveAction = SKAction.repeatActionForever(SKAction.moveByX(-20, y: 0, duration: 0.1))
            let character.runAction(moveAction, withKey: "moveAction")
        } else if location.x > CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{
            let moveAction = SKAction.repeatActionForever(SKAction.moveByX(20, y: 0, duration: 0.1))
            character.runAction(moveAction, withKey: "moveAction")
        } else if location.y > character.position.y + 15  {
            let moveAction = SKAction.repeatActionForever(SKAction.moveByX(0, y: 20, duration: 0.1))
            character.runAction(moveAction, withKey: "moveAction")
        }
    }
}

我编辑了 touchesBegan 方法,并添加了 touchesEnded 方法。您似乎尝试将 嵌入 touchesEnded (和 update)到 touchesBegan 中,但您绝对不能这样做。

好吧,代码非常不言自明,但无论如何:

我添加了一个永远运行的 SKAction,而不是向量。更改持续时间将改变对象移动的速度。持续时间越短,物体速度越快,反之亦然(因为持续时间越短意味着到达某处的时间越短,因此速度越快)。当调用 touchesEnded 时,键为 "moveAction" 的操作将从 character 中删除,从而停止它。

差不多就这些了。如果您有任何疑问,请告诉我!

顺便说一句,我不确定您是否有意这样做,但是当您运行使对象向上移动的 Action 时,角色最终会由于重力而上下弹跳.

关于ios - 如何使用触摸快速制作角色 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427546/

相关文章:

ios - 如何设置背景节点的位置移动

ios - NSKeyedArchiver.archivedData 在 Swift 3 iOS 中不起作用

iphone - 将 ZXing 连接到 xcode 中的按钮

ios - AutoLayout 内容不适合 UITableView 单元格

ios - 将长按添加到注释图钉而不是标注气泡?

ios - 将用户文本字段输入附加到其他 View Controller 中的数组中

ios - 如何在 Interface Builder 中实现这种布局?

ios - 在不同机器上运行 Sonar 扫描仪和 Sonar 服务器时出错

sprite-kit - 使用 'bodyWithTexture' (SpriteKit) 时更改 SKPhysicsBody 的中心点

ios - SpriteKit 动画问题