如何在Sprite Kit中制作2d平台角色控制器? (Xcode 6)

时间:2015-07-30 15:46:58

标签: ios xcode sprite-kit

我问过几次,但还没找到我想要的东西。我需要一个简单的角色控制器,它允许我的角色在屏幕左侧和右侧的情况下连续向左移动。放手时我想让角色停止移动。

我的GameScene.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.gravity = CGVector(dx: 0.0, dy: -5.0)
    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 */


    }
};
}

1 个答案:

答案 0 :(得分:0)

使用UIGestureRecognizer跟踪手指按下和按住的时间。 UILongPressGestureRecognize就是您所需要的。查看官方文档: Long Press Gesture Recognizer

当调用手势识别器时,您可以按照与现在相同的方式应用脉冲。但是,您可能应该在物理机构的任何地方关闭friction,因为无论您想要移动什么,这都会明显减速。如果你真的想要保持摩擦,那么你可以使用SKAction并让节点移动某个X.然后,重复运行该动作。

Long Press GestureRecognizer将在用户抬起手指时改变其状态。当发生这种情况时,您所要做的就是 - (如果您使用物理方式):将节点的velocity转为零或(如果您使用的是SKAction):停止操作。

编辑:这或多或少是你应该拥有的:

  1. 实施UILongPressGestureRecognizer:

    UILongPressGestureRecognizer * pressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(performLongPress)];

  2. 然后创建一个方法:

    -(int)performLongPress(UILongPressGestureRecognizer *){
        if (gesture.state == UIGestureRecognizerStateBegan){
            //you know that the user just started pressing screen.
            if([gesture locationInView:self] == wherever you want it to be...){
                 //apply your impulse here instead
            }
        }
    }
    

    然后将摩擦力设置为零。