Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

TexturePackerSprite

The TexturePackerSprite element defines a series of sprite frames within an image for a TexturePackerSpriteSequence. It has an increased performance thanks to its support for sprite sheets created with TexturePacker. More...

Import Statement: import Felgo 3.0

Properties

Detailed Description

The TexturePackerSprite component is needed for the TexturePackerSpriteSequence like the Sprite is needed for the SpriteSequence. It supports sprite sheet created with TexturePacker. It also has an improved performance compared to the Sprite component.

Read the guide How to use TexturePacker with Felgo to know why sprite sheets reduce the memory consumption and improve the performance of your game.

This component is derived from {TexturePackerAnimatedSprite}. It adds two new properties, name and to. Additionally it changes some default values.

The name property can be seen as the identifier of a state in a stochastic state machine and the to property describes this state's transitions to other states.

Both visible and running are also false by default because the TexturePackerSpriteSequence handles them. The same applies to loops, which is set to TexturePackerSprite.Infinite by default.

Here is an example of how to use two TexturePackerSprite in a TexturePackerSpriteSequence:

 import Felgo 3.0
 import QtQuick 2.0

 GameWindow {
     Scene {
         TexturePackerSpriteSequence {

             TexturePackerSprite {
                 name: "jump"
                 source: "../assets/img/squaby.json"
                 frameNames: ["squ1-jump-1.png", "squ1-jump-2.png", "squ1-jump-3.png", "squ1-jump-4.png"]
                 frameRate: 3

                 to: {"jump": 1, "walk": 3}
             }
             TexturePackerSprite {
                 name: "walk"
                 source: "../assets/img/squaby.json"
                 frameNames: ["squ1-walk-1.png", "squ1-walk-2.png", "squ1-walk-3.png", "squ1-walk-4.png"]
                 frameRate: 3

                 to: {"jump": 1, "walk": 1}
             }
         }
     }
 }

To change the active animation manually, use the jumpTo() function like in this example:

 import Felgo 3.0
 import QtQuick 2.0

 GameWindow {
   Scene {
     TexturePackerSpriteSequence {
       id: squabySprite
       anchors.centerIn: parent

       TexturePackerSprite {
         name: "walk"
         source: "../assets/img/squaby.json"
         frameNames: ["squ1-walk-1.png", "squ1-walk-2.png", "squ1-walk-3.png", "squ1-walk-4.png"]
         frameRate: 3
       }
       TexturePackerSprite {
         name: "jump"
         source: "../assets/img/squaby.json"
         frameNames: ["squ1-jump-1.png", "squ1-jump-2.png", "squ1-jump-3.png", "squ1-jump-4.png"]
         to: {"jump": 1, "walk": 3}
         frameRate: 3
       }
     }// SpriteSequenceFromFile

     Row {
       anchors.bottom: parent.bottom // put on the bottom
       GameButton {
         text: "Jump"
         onClicked: {
           squabySprite.jumpTo("jump")
         }
       }// GameButton
       GameButton {
         text: "Walk"
         onClicked: {
           squabySprite.jumpTo("walk")
         }
       }// GameButton
     }// Row
   }
 }

This component supports content scaling to pick the best image quality for your screen density.

Property Documentation

name : string

A string containing a unique identifier within the TexturePackerSpriteSequence for this sprite. The name is used in the to property or the jumpTo() method. The TexturePackerSpriteSequence uses this identifier to access its TexturePackerSprite children.

The default value is an empty string, so you need to set it if you define multiple sprites to avoid conflicts.


to : var

This property is a QVariantMap, which is an object with number properties in JavaScript. The TexturePackerSpriteSequence uses this map to determine which sprite it will animate next when the current sprite has run a complete cycle. The property names in this object are strings, which contain the name of a TexturePackerSprite in the same TexturePackerSpriteSequence. You can also use the name of this TexturePackerSprite if you want a probability to stay in this sprite. The values of these properties say how probable it is to choose this sprite next.

Basically, this property defines the transitions of a stochastic state machine. And the TexturePackerSprite children of the TexturePackerSpriteSequence are the states.

Default is null, so you need to set it or this sprite will run forever.

Here is an example of how to use it:

 import Felgo 3.0
 import QtQuick 2.0

 GameWindow {
     Scene {
         TexturePackerSpriteSequence {

             TexturePackerSprite {
                 name: "jump"
                 source: "../assets/img/squaby.json"
                 frameNames: ["squ1-jump-1.png", "squ1-jump-2.png", "squ1-jump-3.png", "squ1-jump-4.png"]
                 frameRate: 3

                 to: {"jump": 1, "walk": 3}
             }
             TexturePackerSprite {
                 name: "walk"
                 source: "../assets/img/squaby.json"
                 frameNames: ["squ1-walk-1.png", "squ1-walk-2.png", "squ1-walk-3.png", "squ1-walk-4.png"]
                 frameRate: 3

                 to: {"jump": 1, "walk": 1}
             }
         }
     }
 }

The TexturePackerSpriteSequence will start with the first sprite called "jump". When this sprite has finished "walk" will be the next sprite with a probability of 3/4. The probability to stay in the "jump" sprite for one more cycle is the remaining 1/4. When the "walk" sprite finishes to probabilities for "jump" and "walk" are 50:50.

Note: "{"jump": 1, "walk": 3}" is equal to "{"jump": 0.25, "walk": 0.75}". You don't need to calculate the exact probabilities. Just use relative values.


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded