Jump to content

Hey guys,

 

Im currently dabbling in swift to create an application, I have my player, background and movement down pat however I want to introduce some animation when the player moves.

 

I currently have the below: (please note that the physics components are there as im adding some obstacles in that I will soon include)

 

I am not very familiar with texture atlas-ing and change according to movement.

Thanks in advance

 

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
	
    var player = SKSpriteNode()
    var actionMoveLeft = SKAction()
    var actionMoveRight = SKAction()
	
	//gets screens dimension for scaling
	let screenSize: CGRect = UIScreen.mainScreen().bounds 
	let playerCategory = 0x1 << 1
	let obstacleCategory = 0x1 << 2
	
	
	
    override func didMoveToView(view: SKView) {
        self.backgroundColor = SKColor.whiteColor()
		self.initializingBackground()

        // Making self delegate of physics world
        self.physicsWorld.gravity = CGVectorMake(0, 0)
        self.physicsWorld.contactDelegate = self
		
  }
  
  
  override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
      /* Called when a touch begins */
      
      for touch: AnyObject in touches {
          let location = touch.locationInNode(self)
          
          if location.x > screenSize.width * 0.5 {
              if player.position.x > 5660 {
			  return
			  } else
			  bg.position.x -= 60
              }
          } else {
              if player.position.x > 60 {
                  bg.position.x += 60
              }
          }
      }
  }
  
  
  
  
  
  func addPlayer() {
    // Initializing player node
    player = SKSpriteNode(imageNamed: "player1")
    player.setScale(0.5)

    // Adding SpriteKit physics body for collision detection
    player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
    player.physicsBody?.categoryBitMask = UInt32(playerCategory)
    player.physicsBody?.dynamic = true
    player.physicsBody?.contactTestBitMask = UInt32(obstacleCategory)
    player.physicsBody?.collisionBitMask = 0
    player.name = "player"
    player.position = CGPointMake(120, 200)

    self.addChild(player)
}
  
  
   func initializingBackground() {
         let bg = SKSpriteNode(imageNamed: "bg")
         bg.position = CGPoint(x: 0, y: 0)
         bg.anchorPoint = CGPointZero
         bg.name = "background"
         self.addChild(bg)
     }
 }
  
  
  
}

 

 

Scrapyard Build Total Cost: $268AUD


C2Q E8200 | 4 x 1gb DDR2 | GA-EP45-DS3 r1 | Gammax 200 | 320gb 2.5" | 7870LE PCS | Litepower 500w | CISCO Aironet 350

Link to comment
https://linustechtips.com/topic/605490-ios-animations-in-swift/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×