ios - 我的 Sprites 在 Sprite Kit 中使用 Swift 被压缩

标签 ios swift sprite-kit sprite image-size

我正在使用 Sprite Kit 创建我的第一款游戏。这是一款 Flappy Bird 类型的游戏。左边的图片是今天早些时候的比赛。我最终创建了一个全新的项目。我复制了上一个项目的代码并将其粘贴到当前项目中。我也将与上一个项目完全相同的图像放入当前项目中。没有编译错误,除了游戏现在看起来像右图之外,一切都一样。如您所见,图像宽度较小。任何建议将不胜感激,如果您希望我提供此问题中的所有代码,请告诉我,谢谢。

enter image description here

这是射箭场景的所有代码:

//
//  ArcheryScene.swift
//  FlappyBird (swift)
//
//  Created by Brandon Ballard on 1/6/15.
//  Copyright (c) 2015 Brandon Ballard. All rights reserved.
//

import UIKit
import SpriteKit

class ArcheryScene: SKScene, SKPhysicsContactDelegate {

var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var pipesMoveAndRemove = SKAction()
var score = 0

let pipeGap = 150.0

enum ColliderType:UInt32 {
    case BIRD = 1
    case PIPE = 2
}


override func didMoveToView(view: SKView) {
    /* Setup your scene here */


    backgroundColor = SKColor.cyanColor()

    //physics
    self.physicsWorld.gravity = CGVectorMake(0.0, -15.0);
    self.physicsWorld.contactDelegate = self



    //Bird
    var birdTexture = SKTexture(imageNamed:"Bird")
    birdTexture.filteringMode = SKTextureFilteringMode.Nearest

    bird = SKSpriteNode(texture: birdTexture)
    bird.setScale(0.6)
    bird.position = CGPoint(x: self.frame.width * 0.35 + 20, y: self.frame.size.height * 0.95)

    bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
    bird.physicsBody?.dynamic = true
    bird.physicsBody?.allowsRotation = true
    bird.physicsBody?.affectedByGravity = true
    bird.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
    bird.physicsBody!.contactTestBitMask = ColliderType.PIPE.rawValue
    self.addChild(bird)

    //Ground
    var groundTexture = SKTexture(imageNamed: "Ground")

    var sprite = SKSpriteNode(texture: groundTexture)
    sprite.setScale(2.0)
    sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2.0)


    self.addChild(sprite)

    var ground = SKNode()

    ground.position = CGPointMake(0, groundTexture.size().height + 0)
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
    ground.physicsBody?.dynamic = false
    self.addChild(ground)



    //Pipes

    //Create the Pipes

    pipeUpTexture = SKTexture(imageNamed: "PipeUp")
    pipeDownTexture = SKTexture(imageNamed: "PipeDown")

    //Movement of Pipes

    let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
    let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
    let removePipes = SKAction.removeFromParent()

    pipesMoveAndRemove = SKAction.sequence([movePipes,removePipes])

    //Spawn Pipes

    let spawn = SKAction.runBlock({() in self.spawnPipes()})
    let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
    let spawnThenDelay = SKAction.sequence([spawn,delay])
    let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)

    self.runAction(spawnThenDelayForever)
}

func spawnPipes() {

    let pipePair = SKNode()
    pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
    pipePair.zPosition = -10

    let height = UInt32(self.frame.size.height / 4)
    let y = arc4random() % height + height

    var pipeDown = SKSpriteNode(texture: pipeDownTexture)
    pipeDown.setScale(2.0)////////
    pipeDown.position = CGPointMake(3.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap) )

    pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
    pipeDown.physicsBody?.dynamic = false
    pipeDown.physicsBody!.affectedByGravity = false
    pipeDown.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
    pipePair.addChild(pipeDown)

    var pipeUp = SKSpriteNode(texture: pipeUpTexture)
    pipeUp.setScale(2.0)
    pipeUp.position = CGPointMake(0.0, CGFloat(y))

    pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size )
    pipeUp.physicsBody?.dynamic = false
    pipeUp.physicsBody!.affectedByGravity = false
    pipeUp.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
    pipePair.addChild(pipeUp)

    pipePair.runAction(pipesMoveAndRemove)
    self.addChild(pipePair)

}



func didBeginContact(contact: SKPhysicsContactDelegate) {
    scene?.view?.paused = true
    //gameOver()

}

func createScoreNode() -> SKLabelNode {
    let scoreNode = SKLabelNode(fontNamed: "Brandon Ballard")
    scoreNode.name = "scoreNode"

    let newScore = "\(score)"

    scoreNode.text = newScore
    scoreNode.fontSize = 125
    scoreNode.fontColor = SKColor.cyanColor()
    scoreNode.position = CGPointMake(CGRectGetMidX(self.frame), 58)
    self.addChild(scoreNode)
    return scoreNode
}

func gameOver() {
    let scoreNode = self.createScoreNode()
    self.addChild(scoreNode)
    let fadeOut = SKAction.sequence([SKAction.waitForDuration(3.0), SKAction.fadeOutWithDuration(3.0)])

    let welcomeReturn = SKAction.runBlock({
        let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
        let welcomeScene = GameScene(fileNamed: "GameScene")
        self.scene!.view?.presentScene(welcomeScene, transition: transition)
    })

    let sequence = SKAction.sequence([fadeOut, welcomeReturn])
    self.runAction(sequence)
}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */


    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        bird.physicsBody?.velocity = CGVectorMake( 0, 0 )
        bird.physicsBody?.applyImpulse(CGVectorMake(0,25))

    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
}

最佳答案

要解决我的问题,我需要做的就是在我的 GameScene 文件中添加一行简单的代码。这是我在解决出现 ArcheryScene 的问题之前在 GameScene 文件中的代码:

welcomeNode?.runAction(fadeAway, completion: {
                let doors = SKTransition.pushWithDirection(SKTransitionDirection.Down, duration: 1.0)
                let archeryScene = ArcheryScene(fileNamed: "ArcheryScene")
                self.view?.presentScene(archeryScene, transition: doors)
            })

我所要做的就是在创建“archeryScene”之后添加这行代码:

archeryScene.scaleMode = .AspectFill

这使得图像“适合屏幕大小”。 Apple 使用的“.AspectFill”的确切描述是:

“计算每个维度的缩放因子,并选择两者中较大的一个。场景的每个轴都按相同的缩放因子进行缩放。这保证了 View 的整个区域被填充但可能会导致部分要裁剪的场景。”

关于ios - 我的 Sprites 在 Sprite Kit 中使用 Swift 被压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27808885/

相关文章:

iphone - 一个应用程序中的多个 plist 文件

ios - 是否显示 UIView?

ios - 设置accessibilityElementsHidden是否递归工作?

ios - 在 Swift 3 中将结构写入 outputStream

swift - 无法识别 if 语句条件中使用的变量

ios - 在 Swift 中使用更新函数的更清洁(更好)替代方法

ios - 上面的 UIView 呈现 SKScene - 为什么触摸总是通过场景?

iphone - UITableViewController单元格设计问题

ios - 在 UITableview 重叠单元格中制作节标题

swift - 以一定的速度在屏幕上移动一个对象。(Sprite Kit)