从已经加载的纹理贴图集中创建精灵

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

  1. 使用 TextureCache:
    1. let texture = TextureCache["frameId.png"],
    2. sprite = new Sprite(texture);
  2. 如果你是使用的 loader来加载纹理贴图集, 使用loader的 resources:
    1. let sprite = new Sprite(
    2. resources["images/treasureHunter.json"].textures["frameId.png"]
    3. );
  3. 要创建一个精灵需要输入太多东西了!
    所以我建议你给纹理贴图集的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. //Define variables that might be used in more
  2. //than one function
  3. let dungeon, explorer, treasure, id;
  4. function setup() {
  5. //There are 3 ways to make sprites from textures atlas frames
  6. //1. Access the `TextureCache` directly
  7. let dungeonTexture = TextureCache["dungeon.png"];
  8. dungeon = new Sprite(dungeonTexture);
  9. app.stage.addChild(dungeon);
  10. //2. Access the texture using throuhg the loader's `resources`:
  11. explorer = new Sprite(
  12. resources["images/treasureHunter.json"].textures["explorer.png"]
  13. );
  14. explorer.x = 68;
  15. //Center the explorer vertically
  16. explorer.y = app.stage.height / 2 - explorer.height / 2;
  17. app.stage.addChild(explorer);
  18. //3. Create an optional alias called `id` for all the texture atlas
  19. //frame id textures.
  20. id = PIXI.loader.resources["images/treasureHunter.json"].textures;
  21. //Make the treasure box using the alias
  22. treasure = new Sprite(id["treasure.png"]);
  23. app.stage.addChild(treasure);
  24. //Position the treasure next to the right edge of the canvas
  25. treasure.x = app.stage.width - treasure.width - 48;
  26. treasure.y = app.stage.height / 2 - treasure.height / 2;
  27. app.stage.addChild(treasure);
  28. }

这里是代码运行的结果:

Explorer, dungeon and treasure

舞台定义为512像素见方的大小,你可以看到代码中app.stage.heightapp.stage.width属性使得精灵们排成了一排。下面的代码使得explorery属性垂直居中了。

  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. //load a JSON file and run the `setup` function when it's done
  27. loader
  28. .add("images/treasureHunter.json")
  29. .load(setup);
  30. //Define variables that might be used in more
  31. //than one function
  32. let dungeon, explorer, treasure, door, id;
  33. function setup() {
  34. //There are 3 ways to make sprites from textures atlas frames
  35. //1. Access the `TextureCache` directly
  36. let dungeonTexture = TextureCache["dungeon.png"];
  37. dungeon = new Sprite(dungeonTexture);
  38. app.stage.addChild(dungeon);
  39. //2. Access the texture using throuhg the loader's `resources`:
  40. explorer = new Sprite(
  41. resources["images/treasureHunter.json"].textures["explorer.png"]
  42. );
  43. explorer.x = 68;
  44. //Center the explorer vertically
  45. explorer.y = app.stage.height / 2 - explorer.height / 2;
  46. app.stage.addChild(explorer);
  47. //3. Create an optional alias called `id` for all the texture atlas
  48. //frame id textures.
  49. id = PIXI.loader.resources["images/treasureHunter.json"].textures;
  50. //Make the treasure box using the alias
  51. treasure = new Sprite(id["treasure.png"]);
  52. app.stage.addChild(treasure);
  53. //Position the treasure next to the right edge of the canvas
  54. treasure.x = app.stage.width - treasure.width - 48;
  55. treasure.y = app.stage.height / 2 - treasure.height / 2;
  56. app.stage.addChild(treasure);
  57. //Make the exit door
  58. door = new Sprite(id["door.png"]);
  59. door.position.set(32, 0);
  60. app.stage.addChild(door);
  61. //Make the blobs
  62. let numberOfBlobs = 6,
  63. spacing = 48,
  64. xOffset = 150;
  65. //Make as many blobs as there are `numberOfBlobs`
  66. for (let i = 0; i < numberOfBlobs; i++) {
  67. //Make a blob
  68. let blob = new Sprite(id["blob.png"]);
  69. //Space each blob horizontally according to the `spacing` value.
  70. //`xOffset` determines the point from the left of the screen
  71. //at which the first blob should be added.
  72. let x = spacing * i + xOffset;
  73. //Give the blob a random y position
  74. //(`randomInt` is a custom function - see below)
  75. let y = randomInt(0, app.stage.height - blob.height);
  76. //Set the blob's position
  77. blob.x = x;
  78. blob.y = y;
  79. //Add the blob sprite to the stage
  80. app.stage.addChild(blob);
  81. }
  82. }
  83. //The `randomInt` helper function
  84. function randomInt(min, max) {
  85. return Math.floor(Math.random() * (max - min + 1)) + min;
  86. }
  87. </script>
  88. </body>

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

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

spacing变量的值是48,xOffset的值是150。这意味着第一个blob怪的位置的x坐标将会是150。这个偏移使得泡泡怪离舞台左边的距离有150个像素。每一个泡泡怪都有个48像素的空余,也就是说每一个泡泡怪都会比在循环之中前一个创建的泡泡怪的位置的x坐标多出48像素以上的增量。它使得泡泡怪们相互间隔,从地牢地板的左边排向右边。
每一个blob也被赋予了一个随机的y坐标,这里是处理这件事的代码:

  1. let y = randomInt(0, stage.height - blob.height);
  2. blob.y = y;

泡泡怪的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;
  3. }

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