iOS SpriteKit框架初探

2021-12-09  本文已影响0人  凉风起君子意如何

提要

SpriteKit框架是做什么的:

SpriteKit is a framework that’s used to create high-performance 2D games while rendering graphics and animation textures.
SpriteKit has an optimized animation system and advances physics effects that are ideal to create engaging 2D games.

SpriteKit框架的优势:

Advantages of SpriteKit

  • It’s built right into iOS. There is no need to download extra libraries or have external dependencies. You can also seamlessly use other iOS APIs like iAd, In-App Purchases, etc. without having to rely on extra plugins.
  • It leverages your existing skills. If you already know Swift and iOS development, you can pick up SpriteKit extremely quickly.
  • It’s written by Apple. This gives you some confidence that it will be well supported moving forward on all of Apple’s new products. For example, you can use the same SpriteKit code to make your game work on iOS, macOS, and tvOS without a hitch.
  • It’s free. Maybe one of the best reasons for small indies! You get all of SpriteKit’s functionality at no cost. Unity does have a free version but it doesn’t have all of the features of the Pro version. You’ll need to upgrade if you want to avoid the Unity splash screen, for example.

SpriteKit基础类

以上截图出自该文章,里面的小demo很适合一点基础都没有的小伙伴,讲的很清晰。但功能太简单正如标题,只是个Tutorial。

正文

程序开发就是这样,看百遍,不如动手敲一遍,也许只有在敲代码的过程中,不断的试错,想的更多,思考才会更深。

目标:打怪物的小游戏,具体涉及点如下:

主要开发流程:

关注的代码

刚开始准备贴出所有的核心代码,但发现那样文章会很长,demo通过该链接都能下载,这里就不贴了。自己在学习该demo的时候,对如下代码有点疑惑,因为它涉及到vector向量,后来看了下它,大概明白so记录下。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        // 1 - Choose one of the touches to work with
        guard let touch = touches.first else {
            return
        }
        run(SKAction.playSoundFileNamed("pew-pew-lei.caf", waitForCompletion: false))
        
        let touchLocation = touch.location(in: self)

        // 2 - Set up initial location of projectile
        let projectile = SKSpriteNode(imageNamed: "projectile")
        projectile.position = player.position

        // 3 - Determine offset of location to projectile
        let offset = touchLocation - projectile.position

        // 4 - Bail out if you are shooting down or backwards
        if offset.x < 0 { return }

        // 5 - OK to add now - you've double checked position
        addChild(projectile)
        projectileUsed += 1

        // 6 - Get the direction of where to shoot
        let direction = offset.normalized()

        // 7 - Make it shoot far enough to be guaranteed off screen
        // 只是扩大了scalar 方向没有变
        let shootAmount = direction * 1000

        // 8 - Add the shoot amount to the current position
        // 开始加上结尾,就是整个向量
        let realDest = shootAmount + projectile.position

        // 9 - Create the actions
        let actionMove = SKAction.move(to: realDest, duration: 2.0)
        let actionMoveDone = SKAction.removeFromParent()
        projectile.run(SKAction.sequence([actionMove, actionMoveDone]))
        
        // 添加物理属性
        projectile.physicsBody = SKPhysicsBody(circleOfRadius: projectile.size.width/2)
        projectile.physicsBody?.isDynamic = true
        projectile.physicsBody?.categoryBitMask = PhysiscCategory.projectile
        projectile.physicsBody?.contactTestBitMask = PhysiscCategory.monster
        projectile.physicsBody?.collisionBitMask = PhysiscCategory.none
        projectile.physicsBody?.usesPreciseCollisionDetection = true
    }

End

上一篇下一篇

猜你喜欢

热点阅读