vendredi 31 juillet 2015

How can I make a 2d platformer character controller in Sprite Kit? (Xcode 6)

I've asked a few times but haven't really found what I'm looking for. I need a simple character controller that allows my character to continuously move left if the left side of the screen is held and right side as well. When let go I would like the character to stop moving.

My GameScene.swift file:

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 */


    }
};
}

Aucun commentaire:

Enregistrer un commentaire