Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用touch在swift中制作角色控制器?_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 如何使用touch在swift中制作角色控制器?

Ios 如何使用touch在swift中制作角色控制器?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我想用Swift在Xcode中制作一个简单的platformer游戏,这样当我握住屏幕的左侧时,它会一直向左移动,直到松开为止,反之亦然。我已经添加了一个跳转功能,但如果你有任何其他建议,我愿意接受建议 这是我的GameController.swift代码: 进口SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { let character = SKSpriteNode(imageNamed:"square.png") l

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

这是我的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 */
                }
        }
}
}
类游戏场景:SKScene,skphysiccontactdelegate{
让character=SKSpriteNode(图像名为:“square.png”)
let block=SKSpriteNode(图像名为:“square.png”)
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
//物理学
self.physicsWorld.gravity=CGVectorMake(0.0,-5.0)
self.physicsWorld.contactDelegate=self
self.physicsBody=SKPhysicsBody(edgeLoopFromRect:self.frame)
//性格
character.position=CGPoint(x:CGRectGetMidX(self.frame),
y:CGRectGetMidY(self.frame))
字符设置刻度(0.25)
character.physicsBody=SKPhysicsBody(矩形大小:character.size)
self.addChild(字符)
//挡块
block.position=CGPoint(x:CGRectGetMidX(self.frame),y:
CGRectGetMidY(自帧)*0.3)
块设置刻度(0.2)
block.physicsBody=SKPhysicsBody(矩形尺寸:block.size)
block.physicsBody?.dynamic=false
self.addChild(块)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
如果location.xCGRectGetMidX(self.frame)和&location.y<
CGRectGetMidY(自帧)*0.7{
字符.physicsBody?.applyImpulse(CGVector(dx:50,dy:0))
}否则,如果location.y>character.position.y+15{
字符.physicsBody?.applyImpulse(CGVector(dx:0,dy:50))
}
func touchesend(触摸:设置,withEvent事件:UIEvent){
}
func更新(当前时间:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}
}
}
覆盖函数touchesend(touchs:Set,withEvent-event:UIEvent?){
character.removeActionForKey(“moveAction”)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
如果location.xCGRectGetMidX(self.frame)和&location.ycharacter.position.y+15{
让moveAction=SKAction.repeatActionForever(SKAction.moveByX(0,y:20,持续时间:0.1))
character.runAction(moveAction,带键:“moveAction”)
}
}
}
我编辑了
touchesbeated
方法,并添加了
touchesend
方法。您似乎试图将
touchseended
(和
update
)嵌入
touchseended
,但这是您绝对无法做到的

好的,代码是很自然的,但无论如何:

我添加了一个永远运行的SKAction,而不是向量。更改持续时间将更改对象移动的速度。持续时间越短,对象速度越快,反之亦然(因为持续时间越短,到达某处的时间越短,因此速度越快)。调用
touchesend
时,键为
的操作将从
字符
中删除,从而停止该操作

差不多就是这样。如果你有任何问题,请告诉我


顺便说一句,我不确定您是否打算这样做,但当您运行对象向上移动的动作时,
角色
最终会因为重力而上下弹跳。

只需将其设置为当
touchesbreated
中的触摸位置位于屏幕左侧时(例如),开始一个永远运行的SKAction,将角色向左移动,直到释放触摸–在
touchesend
中,删除该SKAction。这同样适用于屏幕的右半部分,等等。这是有意义的,还是你想让我发布一个完整的答案?–这就是你要问的,是吗?你在
physicsBody
上使用
.applyImpulse
而不是添加
SKAction
有什么特别的原因吗?我不知道我唯一的原因是什么真的…你可以发布一个完整的答案吗?我已经为此挣扎了几个月。谢谢,我现在正在打一个答案:)
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")
        }
    }
}