Ios 如何在SpriteKit中制作简单的角色控制器?

Ios 如何在SpriteKit中制作简单的角色控制器?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在用精灵套件做一个游戏,为了移动我的角色,我施加了一个冲动,但这是一个问题,因为它每50像素左右移动一次。我的问题是,有人能告诉我如何制作一个简单的角色控制器,这样我的角色就可以一直移动,直到用户不再按住屏幕为止 My GameSecene.swift文件: import SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { let character = SKSpriteNode(texture: SKTextu

我正在用精灵套件做一个游戏,为了移动我的角色,我施加了一个冲动,但这是一个问题,因为它每50像素左右移动一次。我的问题是,有人能告诉我如何制作一个简单的角色控制器,这样我的角色就可以一直移动,直到用户不再按住屏幕为止

My GameSecene.swift文件:

import SpriteKit


class GameScene: SKScene, SKPhysicsContactDelegate {

let character = SKSpriteNode(texture: SKTexture(imageNamed: "character"))
var move = false

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    //world
    self.physicsWorld.contactDelegate = self
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)


    //character
    character.position = CGPointMake(self.frame.size.width * 0.6, self.frame.size.height * 0.6)
    character.setScale(0.2)
    character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
    character.physicsBody?.dynamic = true
    character.physicsBody?.allowsRotation = false
    self.addChild(character)
    character.physicsBody?.affectedByGravity = true




    //platform 1
    var platform = SKSpriteNode(texture: SKTexture(imageNamed: "platform"))
    platform.position = CGPointMake(self.frame.size.width * 0.6, CGRectGetMidY(self.frame))
    platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
    platform.physicsBody?.dynamic = false
    platform.setScale(0.25)
    platform.physicsBody?.friction = 1
    platform.physicsBody?.restitution = 0
    platform.physicsBody?.linearDamping = 0
    self.addChild(platform)

    //platform 2
    var platformTexture2 = SKTexture(imageNamed: "platform")
    var platform2 = SKSpriteNode(texture: platformTexture2)
    platform2.position = CGPointMake(self.frame.size.width * 0.4, self.frame.size.height * 0.3)
    platform2.physicsBody = SKPhysicsBody(rectangleOfSize: platform2.size)
    platform2.physicsBody?.dynamic = false
    platform2.setScale(0.25)
    platform2.physicsBody?.friction = 1
    platform2.physicsBody?.restitution = 0
    platform2.physicsBody?.linearDamping = 0
    self.addChild(platform2)


    //platform main
    var platformTexture3 = SKTexture(imageNamed: "platform")
    var platform3 = SKSpriteNode(texture: platformTexture2)
    platform3.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + platform3.size.height / 3)
    platform3.physicsBody = SKPhysicsBody(rectangleOfSize: platform3.size)
    platform3.physicsBody?.dynamic = false
    platform3.setScale(1)
    platform3.size.width = platform3.size.width * CGFloat(2.0)
    platform3.physicsBody?.friction = 1
    platform3.physicsBody?.restitution = 0
    platform3.physicsBody?.linearDamping = 0
    self.addChild(platform3)

    }

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */


   for touch: AnyObject in touches {
    let location = touch.locationInNode(self)

        if location.x < CGRectGetMidX(self.frame){
            character.physicsBody?.applyImpulse(CGVector(dx: -50, dy: 0))
        } else if location.x > CGRectGetMidX(self.frame){
            character.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 0))
                        }
    }



    func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        character.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
}




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


    }
    };
}
导入SpriteKit
类游戏场景:SKScene,SKPhysicContactDelegate{
让character=SKSpriteNode(纹理:SKTexture(ImageName:“character”))
var move=false
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
//世界
self.physicsWorld.contactDelegate=self
self.physicsBody=SKPhysicsBody(edgeLoopFromRect:self.frame)
//性格
character.position=CGPointMake(self.frame.size.width*0.6,self.frame.size.height*0.6)
字符设置刻度(0.2)
character.physicsBody=SKPhysicsBody(矩形大小:character.size)
character.physicsBody?.dynamic=true
character.physicsBody?.allowsRotation=false
self.addChild(字符)
character.physicsBody?.affectedByGravity=true
//第一站台
var platform=SKSpriteNode(纹理:SKTexture(ImageName:“platform”))
platform.position=CGPointMake(self.frame.size.width*0.6,CGRectGetMidY(self.frame))
platform.physicsBody=SKPhysicsBody(矩形尺寸:platform.size)
platform.physicsBody?.dynamic=false
平台设置刻度(0.25)
平台.physicsBody?摩擦力=1
平台.physicsBody?.restitution=0
平台.physicsBody?.linearDamping=0
self.addChild(平台)
//站台2
var platformTexture2=SKTexture(图像名为:“平台”)
var platform2=SKSpriteNode(纹理:platformTexture2)
platform2.position=CGPointMake(self.frame.size.width*0.4,self.frame.size.height*0.3)
platform2.physicsBody=SKPhysicsBody(矩形尺寸:platform2.size)
平台2.physicsBody?.dynamic=false
平台2.设置刻度(0.25)
平台2.物理体?摩擦力=1
平台2.physicsBody?恢复=0
平台2.physicsBody?.linearDamping=0
self.addChild(平台2)
//主站台
var platformTexture3=SKTexture(图像名为:“平台”)
var platform3=SKSpriteNode(纹理:platformTexture2)
platform3.position=CGPointMake(CGRectGetMidX(self.frame)、CGRectGetMinY(self.frame)+platform3.size.height/3)
platform3.physicsBody=SKPhysicsBody(矩形尺寸:platform3.size)
平台3.physicsBody?.dynamic=false
平台3.设置刻度(1)
platform3.size.width=platform3.size.width*CGFloat(2.0)
平台3.物理体?摩擦力=1
平台3.physicsBody?恢复=0
平台3.physicsBody?.linearDamping=0
self.addChild(平台3)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
如果location.xCGRectGetMidX(self.frame),则为else{
字符.physicsBody?.applyImpulse(CGVector(dx:50,dy:0))
}
}
func touchesend(触摸:设置,withEvent事件:UIEvent){
character.physicsBody?.velocity=CGVector(dx:0,dy:0)
}
func更新(当前时间:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
};
}

实现这一点的一种方法是维护一个变量来跟踪触摸事件的状态(左、右或无)。以下是基本步骤:

  • 在触摸开始时,将触摸状态变量设置为左或右
  • 在更新中,当用户触摸屏幕时,持续施加脉冲
  • touchesend
    中,重置touch state变量
  • 下面是一个如何实现此功能的示例

    游戏场景
    类定义上方,添加以下内容

    enum TouchState {
        case Left
        case Right
        case None
    }
    
    游戏场景中
    ,添加以下变量

    var touchLocation:TouchState = .None
    
    游戏场景中
    ,添加或替换以下方法

    override func update(currentTime: CFTimeInterval) {
        // Apply impulse to physics body while users is touching the screen
        switch (touchState) {
        case .Left:
            character.physicsBody?.applyImpulse(CGVector(dx: -1, dy: 0))
        case .Right:
            character.physicsBody?.applyImpulse(CGVector(dx: 1, dy: 0))
        case .None:
            break
        }
    }
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch:AnyObject in touches {
            let location = touch.locationInNode(self)
            if location.x < CGRectGetMidX(self.frame) {
                touchState = .Left
            } else if location.x > CGRectGetMidX(self.frame) {
                touchState = .Right
            }
        }
    }
    
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        // Update touch status
        touchState = .None
    }
    
    覆盖函数更新(currentTime:CFTimeInterval){
    //当用户触摸屏幕时,向身体施加脉冲
    开关(触摸状态){
    案例左:
    字符.physicsBody?.applyImpulse(CGVector(dx:-1,dy:0))
    案例.右侧:
    字符.physicsBody?.applyImpulse(CGVector(dx:1,dy:0))
    案例:无:
    打破
    }
    }
    覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
    用于触摸:触摸中的任何对象{
    let location=touch.locationInNode(自)
    如果location.xCGRectGetMidX(self.frame),则为else{
    touchState=。对
    }
    }
    }
    覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
    //更新触摸状态
    touchState=.None
    }
    
    这是游戏场景。斯威夫特

    import SpriteKit
    
    enum TouchLocation {
        case Left
        case Right
        case None
    }
    
    class GameScene: SKScene, SKPhysicsContactDelegate {
        var character = SKSpriteNode(imageNamed: "character")
    
        var touchLocation:TouchLocation = .None
    
        override func didMoveToView(view: SKView) {
            /* Setup your scene here */
            scaleMode = .ResizeFill
    
            //character
            character.position = CGPointMake(view.frame.size.width * 0.5, view.frame.size.height * 0.5)
            character.setScale(0.1)
            character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size)
            character.physicsBody?.dynamic = true
            character.physicsBody?.allowsRotation = false
            self.addChild(character)
            character.physicsBody?.affectedByGravity = true
    
    
            //platform 1
            var platform = SKSpriteNode(texture: SKTexture(imageNamed: "platform"))
            platform.position = CGPointMake(view.frame.size.width * 0.6, CGRectGetMidY(view.frame))
            platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
            platform.physicsBody?.dynamic = false
            platform.setScale(0.25)
            platform.physicsBody?.friction = 1
            platform.physicsBody?.restitution = 0
            platform.physicsBody?.linearDamping = 0
            self.addChild(platform)
    
            //platform 2
            var platformTexture2 = SKTexture(imageNamed: "platform")
            var platform2 = SKSpriteNode(texture: platformTexture2)
            platform2.position = CGPointMake(view.frame.size.width * 0.4, view.frame.size.height * 0.3)
            platform2.physicsBody = SKPhysicsBody(rectangleOfSize: platform2.size)
            platform2.physicsBody?.dynamic = false
            platform2.setScale(0.25)
            platform2.physicsBody?.friction = 1
            platform2.physicsBody?.restitution = 0
            platform2.physicsBody?.linearDamping = 0
            self.addChild(platform2)
    
    
            //platform main
            var platformTexture3 = SKTexture(imageNamed: "platform")
            var platform3 = SKSpriteNode(texture: platformTexture2)
            platform3.position = CGPointMake(CGRectGetMidX(view.frame), CGRectGetMinY(view.frame) + platform3.size.height / 3)
            platform3.physicsBody = SKPhysicsBody(rectangleOfSize: platform3.size)
            platform3.physicsBody?.dynamic = false
            platform3.setScale(1)
            platform3.size.width = platform3.size.width * CGFloat(2.0)
            platform3.physicsBody?.friction = 1
            platform3.physicsBody?.restitution = 0
            platform3.physicsBody?.linearDamping = 0
            self.addChild(platform3)
        }
    
        override func update(currentTime: CFTimeInterval) {
            /* Called before each frame is rendered */
            switch (touchLocation) {
            case .Left:
                character.physicsBody?.applyImpulse(CGVector(dx: -1, dy: 0))
            case .Right:
                character.physicsBody?.applyImpulse(CGVector(dx: 1, dy: 0))
            case .None:
                break
            }
        }
    
        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
            for touch:AnyObject in touches {
                let location = touch.locationInNode(self)
                if location.x < CGRectGetMidX(self.frame) {
                    touchLocation = .Left
                } else if location.x > CGRectGetMidX(self.frame) {
                    touchLocation = .Right
                }
            }
        }
    
        override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
            touchLocation = .None
        }
    }
    
    导入SpriteKit
    枚举位置{
    案例左
    案例权利
    无案例
    }
    类游戏场景:SKScene,SKPhysicContactDelegate{
    var character=SKSpriteNode(图像名为:“字符”)
    变量touchLocation:touchLocation=.None
    覆盖func didMoveToView(视图:SKView){
    /*在这里设置场景*/
    scaleMode=.ResizeFill
    //性格
    character.position=CGPointMake(view.frame.size.width*0.5,view.frame.size.height*0.5)
    字符设置刻度(0.1)
    character.physicsBody=SKPhysicsBody(矩形大小:character.size)
    character.physicsBody?.dynamic=true
    character.physicsBody?.allowsRotation=false
    self.addChild(字符)
    character.physicsBody?.affectedByGravity=true
    //第一站台
    var platform=SKSpriteNode(纹理:SKTexture(ImageName:“platform”))
    正中平台