通常Pixi给你三种方式从已经加载的纹理贴图集中创建精灵:

  1. 如果你是使用的 loader来加载纹理贴图集, 使用loader的 resources:
    1. let sprite = new Sprite(
    2. resources["images/treasureHunter.json"].textures["frameId.png"]
    3. );
  2. 要创建一个精灵需要输入太多东西了!
    所以我建议你给纹理贴图集的textures对象创建一个叫做id的别名,象是这样:
    1. let id = PIXI.loader.resources["images/treasureHunter.json"].textures;
    现在你就可以像这样实例化一个精灵了:
    1. let sprite = new Sprite(id["frameId.png"]);
    真不错啊~!

这里在setup函数中用三种不同的创建方法创建和显示了dungeon, explorer, 和 treasure精灵。

这里是代码运行的结果:

  1. explorer.y = app.stage.height / 2 - explorer.height / 2;

学会使用纹理贴图集来创建一个精灵是一个基本的操作。所以在我们继续之前,你来试着写一些这样的精灵吧:blob们和exit的门,让他们看起来象是这样:

All the texture atlas sprites

下面就是所有的代码啦。我也把HTML放了进来,现在你可以看见所有的上下文。(你可以在examples/spriteFromTextureAtlas.html找到可以用于演示的代码。)注意,blob精灵是用一个循环加进舞台的,并且他有一个随机的位置。

  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Make a sprite from a texture atlas</title>
  4. <body>
  5. <script src="../pixi/pixi.min.js"></script>
  6. <script>
  7. //Aliases
  8. let Application = PIXI.Application,
  9. Container = PIXI.Container,
  10. loader = PIXI.loader,
  11. resources = PIXI.loader.resources,
  12. TextureCache = PIXI.utils.TextureCache,
  13. Sprite = PIXI.Sprite,
  14. Rectangle = PIXI.Rectangle;
  15. //Create a Pixi Application
  16. let app = new Application({
  17. width: 512,
  18. height: 512,
  19. antialias: true,
  20. transparent: false,
  21. resolution: 1
  22. }
  23. );
  24. //Add the canvas that Pixi automatically created for you to the HTML document
  25. document.body.appendChild(app.view);
  26. loader
  27. .add("images/treasureHunter.json")
  28. .load(setup);
  29. //Define variables that might be used in more
  30. //than one function
  31. let dungeon, explorer, treasure, door, id;
  32. function setup() {
  33. //There are 3 ways to make sprites from textures atlas frames
  34. //1. Access the `TextureCache` directly
  35. let dungeonTexture = TextureCache["dungeon.png"];
  36. dungeon = new Sprite(dungeonTexture);
  37. app.stage.addChild(dungeon);
  38. //2. Access the texture using throuhg the loader's `resources`:
  39. explorer = new Sprite(
  40. resources["images/treasureHunter.json"].textures["explorer.png"]
  41. );
  42. explorer.x = 68;
  43. //Center the explorer vertically
  44. explorer.y = app.stage.height / 2 - explorer.height / 2;
  45. app.stage.addChild(explorer);
  46. //3. Create an optional alias called `id` for all the texture atlas
  47. //frame id textures.
  48. id = PIXI.loader.resources["images/treasureHunter.json"].textures;
  49. //Make the treasure box using the alias
  50. treasure = new Sprite(id["treasure.png"]);
  51. app.stage.addChild(treasure);
  52. //Position the treasure next to the right edge of the canvas
  53. treasure.x = app.stage.width - treasure.width - 48;
  54. treasure.y = app.stage.height / 2 - treasure.height / 2;
  55. app.stage.addChild(treasure);
  56. //Make the exit door
  57. door = new Sprite(id["door.png"]);
  58. door.position.set(32, 0);
  59. app.stage.addChild(door);
  60. let numberOfBlobs = 6,
  61. spacing = 48,
  62. xOffset = 150;
  63. //Make as many blobs as there are `numberOfBlobs`
  64. //Make a blob
  65. let blob = new Sprite(id["blob.png"]);
  66. //Space each blob horizontally according to the `spacing` value.
  67. //`xOffset` determines the point from the left of the screen
  68. //at which the first blob should be added.
  69. let x = spacing * i + xOffset;
  70. //Give the blob a random y position
  71. //(`randomInt` is a custom function - see below)
  72. let y = randomInt(0, app.stage.height - blob.height);
  73. //Set the blob's position
  74. blob.x = x;
  75. blob.y = y;
  76. //Add the blob sprite to the stage
  77. app.stage.addChild(blob);
  78. }
  79. }
  80. //The `randomInt` helper function
  81. function randomInt(min, max) {
  82. return Math.floor(Math.random() * (max - min + 1)) + min;
  83. }
  84. </script>
  85. </body>

你可以看见所有的泡泡怪都用一个for循环被创建了,每一个泡泡怪都有一个独一无二的x坐标,像是下面这样:

  1. let x = spacing * i + xOffset;
  2. blob.x = x;

泡泡怪的y坐标将会从0到512之间随机取值,它的变量名是stage.height。它的值是利用randomInt函数来得到的。randomInt返回一个由你定义范围的随机数。

  1. randomInt(lowestNumber, highestNumber)

这意味着如果你想要一个1到10之间的随机数,你可以这样得到它:

  1. let randomNumber = randomInt(1, 10);

这是randomInt方法的定义:

  1. function randomInt(min, max) {
  2. return Math.floor(Math.random() * (max - min + 1)) + min;

randomInt是一个很好的用来做游戏的工具函数,我经常用他。