Animation 请有人分享一个当前的SpriteKit纹理图集样本来分离吗

Animation 请有人分享一个当前的SpriteKit纹理图集样本来分离吗,animation,sprite-kit,textures,Animation,Sprite Kit,Textures,我的背景是Arduino,我发现Swift有点不透明。也有很多过时的教程。我正在努力让一个动画精灵出现在屏幕上。我将使用一个小的动画.png序列,将其保存到ball.atlas文件夹中,并将其复制到Assets.xcsets 我找到了我认为是奈特弗德拉贡发布的东西。运行它会在控制台中显示“工作”,但我不明白如何将它分配给一个玩家,从而在模拟器上显示。如果有人愿意分享这样做所需的额外代码,我可以将其分离 代码如下: let textureAtlas = SKTextureAtlas(nam

我的背景是Arduino,我发现Swift有点不透明。也有很多过时的教程。我正在努力让一个动画精灵出现在屏幕上。我将使用一个小的动画.png序列,将其保存到ball.atlas文件夹中,并将其复制到Assets.xcsets

我找到了我认为是奈特弗德拉贡发布的东西。运行它会在控制台中显示“工作”,但我不明白如何将它分配给一个玩家,从而在模拟器上显示。如果有人愿意分享这样做所需的额外代码,我可以将其分离

代码如下:

    let textureAtlas = SKTextureAtlas(named: "example")
    var textureArray = [SKTexture]()
    var frames:[SKTexture] = []
    for index in 1 ... 59 {
        let textureName = "example_\(index)"
        let texture = textureAtlas.textureNamed(textureName)
        frames.append(texture)
        textureArray = frames
        print("worked")

我无法与您共享纹理图集,但我可以帮助您解释一下,请参见下文

let textureAtlas = SKTextureAtlas(named: "example")
var textureArray = [SKTexture]()
var frames:[SKTexture] = []
for index in 1 ... 59 {
    let textureName = "example_\(index)" 
    let texture = textureAtlas.textureNamed(textureName)
    frames.append(texture)
    textureArray = frames
    print("worked")
给出“let textureName=”example_u(index)”的行是KoD为您提供地图集中所有精灵的命名约定的行。此代码位的地图集本身称为“example”。地图集中保存的所有精灵都指定了以下名称“example_X”(X是一个整数).example_1、example_2、example_3等…等等,直到到达example_59。如果不想制作59个纹理,则需要编辑for循环,并给出新值

嗯,好的,看看这里

import SpriteKit

class GameScene: SKScene{

//MARK: PROPERTIES
var lastUpdateTime: TimeInterval = 0
var dt: TimeInterval = 0
var myAnimation = SKAction()
let myAnimaTimeInterval: TimeInterval = 0.1
let mySprite = SKSpriteNode(texture: SKTextureAtlas(named: "example").textureNamed("example_1"))

//MARK: INITIALISATION
override func sceneDidLoad() {

}

override func didMove(to view: SKView) {
    addChild(mySprite)
    mySprite.position = CGPoint.zero
    setAnimation()
    runAnimation(node: mySprite)
}

//MARK: GAME LOOP
override func update(_ currentTime: TimeInterval) {
    updateGameLoopTimer(currentTime: currentTime)

}

override func didEvaluateActions() {

}

override func didFinishUpdate() {

}

//MARK: TOUCHES
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

//MARK: HELPER FUNCTIONS
private func setAnimation() {
    let textureAtlas = SKTextureAtlas(named: "example")
    var textureArray: [SKTexture] = []
    for index in 2...59 {
        let textureName = "example_\(index)"
        let texture = textureAtlas.textureNamed(textureName)
        textureArray.append(texture)
    }
    for index in (1...58).reversed() {
        let textureName = "example_\(index)"
        let texture = textureAtlas.textureNamed(textureName)
        textureArray.append(texture)
    }

    let animation = SKAction.animate(with: textureArray, timePerFrame: myAnimaTimeInterval)
    myAnimation = SKAction.repeatForever(animation)
}

private func runAnimation(node: SKSpriteNode) {
    node.run(myAnimation, withKey: "myAnimation")
}

private func stopAnimation(node: SKSpriteNode) {
    node.removeAction(forKey: "myAnimation")
}

public func updateGameLoopTimer(currentTime: TimeInterval) {
    if lastUpdateTime > 0 {
        dt = currentTime - lastUpdateTime
    } else {
        dt = 0
    }
    lastUpdateTime = currentTime
}


//MARK: CLEAN-UP
deinit {

}
}
导入SpriteKit
类游戏场景:SKScene{
//马克:财产
var lastUpdateTime:TimeInterval=0
var dt:TimeInterval=0
var myAnimation=SKAction()
设myAnimaTimeInterval:TimeInterval=0.1
让mySprite=SKSpriteNode(纹理:SKTextureAtlas(命名为:“示例”).TextureName(“示例1”))
//标记:初始化
重写func sceneDidLoad(){
}
覆盖func didMove(到视图:SKView){
addChild(mySprite)
mySprite.position=CGPoint.zero
setAnimation()
运行动画(节点:mySprite)
}
//马克:游戏循环
覆盖函数更新(uCurrentTime:TimeInterval){
updateGameLoopTimer(currentTime:currentTime)
}
重写func didealuateActions(){
}
重写func didFinishUpdate(){
}
//马克:触摸
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
}
//标记:辅助函数
私有函数setAnimation(){
设textureAtlas=SKTextureAtlas(命名为:“示例”)
var textureArray:[SKTexture]=[]
对于2…59中的索引{
让textureName=“示例\(索引)”
让纹理=textureAtlas.textureName(textureName)
textureArray.append(纹理)
}
对于(1…58)中的索引。反向(){
让textureName=“示例\(索引)”
让纹理=textureAtlas.textureName(textureName)
textureArray.append(纹理)
}
让animation=SKAction.animate(使用:textureArray,timePerFrame:myAnimaTimeInterval)
myAnimation=SKAction.repeatForever(动画)
}
私有函数运行动画(节点:SKSpriteNode){
运行(myAnimation,带键:“myAnimation”)
}
私有函数停止动画(节点:SKSpriteNode){
node.removeAction(forKey:“myAnimation”)
}
public func updateGameLoopTimer(当前时间:时间间隔){
如果lastUpdateTime>0{
dt=当前时间-上次更新时间
}否则{
dt=0
}
lastUpdateTime=currentTime
}
//马克:清理
脱硝{
}
}
我对角色的第一个精灵做了一些改动,这让我稍微改变了循环

您可以使用runAnimation方法将动画附加到任何精灵,并使用stopAnimation方法停止动画

试着用上面的替换你的游戏场景文件。好吧,现在用游戏循环计时器…也许有更好的方法来计时游戏…但对你和我来说都没问题


请再试一次

我无法与您共享纹理图集,但我会帮助您解释一下,请参见下文

let textureAtlas = SKTextureAtlas(named: "example")
var textureArray = [SKTexture]()
var frames:[SKTexture] = []
for index in 1 ... 59 {
    let textureName = "example_\(index)" 
    let texture = textureAtlas.textureNamed(textureName)
    frames.append(texture)
    textureArray = frames
    print("worked")
给出“let textureName=“example_(index)”的那一行是KoD给出Atlas中所有精灵的命名约定的地方。这段代码的atlas本身称为“示例”。保存在Atlas中的所有精灵都指定了以下名称“example_X”(X是一个整数)。示例1、示例2、示例3等。。。等等,直到你找到例子。如果不希望生成59个纹理,则需要编辑for循环,并给出新值

嗯,好的,看看这里

import SpriteKit

class GameScene: SKScene{

//MARK: PROPERTIES
var lastUpdateTime: TimeInterval = 0
var dt: TimeInterval = 0
var myAnimation = SKAction()
let myAnimaTimeInterval: TimeInterval = 0.1
let mySprite = SKSpriteNode(texture: SKTextureAtlas(named: "example").textureNamed("example_1"))

//MARK: INITIALISATION
override func sceneDidLoad() {

}

override func didMove(to view: SKView) {
    addChild(mySprite)
    mySprite.position = CGPoint.zero
    setAnimation()
    runAnimation(node: mySprite)
}

//MARK: GAME LOOP
override func update(_ currentTime: TimeInterval) {
    updateGameLoopTimer(currentTime: currentTime)

}

override func didEvaluateActions() {

}

override func didFinishUpdate() {

}

//MARK: TOUCHES
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

//MARK: HELPER FUNCTIONS
private func setAnimation() {
    let textureAtlas = SKTextureAtlas(named: "example")
    var textureArray: [SKTexture] = []
    for index in 2...59 {
        let textureName = "example_\(index)"
        let texture = textureAtlas.textureNamed(textureName)
        textureArray.append(texture)
    }
    for index in (1...58).reversed() {
        let textureName = "example_\(index)"
        let texture = textureAtlas.textureNamed(textureName)
        textureArray.append(texture)
    }

    let animation = SKAction.animate(with: textureArray, timePerFrame: myAnimaTimeInterval)
    myAnimation = SKAction.repeatForever(animation)
}

private func runAnimation(node: SKSpriteNode) {
    node.run(myAnimation, withKey: "myAnimation")
}

private func stopAnimation(node: SKSpriteNode) {
    node.removeAction(forKey: "myAnimation")
}

public func updateGameLoopTimer(currentTime: TimeInterval) {
    if lastUpdateTime > 0 {
        dt = currentTime - lastUpdateTime
    } else {
        dt = 0
    }
    lastUpdateTime = currentTime
}


//MARK: CLEAN-UP
deinit {

}
}
导入SpriteKit
类游戏场景:SKScene{
//马克:财产
var lastUpdateTime:TimeInterval=0
var dt:TimeInterval=0
var myAnimation=SKAction()
设myAnimaTimeInterval:TimeInterval=0.1
让mySprite=SKSpriteNode(纹理:SKTextureAtlas(命名为:“示例”).TextureName(“示例1”))
//标记:初始化
重写func sceneDidLoad(){
}
覆盖func didMove(到视图:SKView){
addChild(mySprite)
mySprite.position=CGPoint.zero
setAnimation()
运行动画(节点:mySprite)
}
//马克:游戏循环
覆盖函数更新(uCurrentTime:TimeInterval){
updateGameLoopTimer(currentTime:currentTime)
}
重写func didealuateActions(){
}
重写func didFinishUpdate(){
}
//马克:触摸
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
}
//标记:辅助函数
私有函数setAnimation(){
设textureAtlas=SKTextureAtlas(命名为:“示例”)
var textureArray:[SKTexture]=[]
对于2…59中的索引{
让textureName=“示例\(索引)”
让纹理=textureAtlas.textureName(textureName)
textureArray.append(纹理)
}
对于(1…58)中的索引。反向(){
让textureName=“示例\(索引)”
让纹理=textureAtlas.textureName(textureName)
textureArray.append(纹理)
}
让animation=SKAction.animate(使用:textureArray,timePerFrame:myAnimaTimeInterval)
myAnimation=SKAction.repeatForever(动画)
}