PIXI的學習------(四)從已經加載的紋理貼圖集中創建精靈的三種方法

  1. 使用 TextureCache: let dungeonTexture = TextureCache["dungeon.png"];
            dungeon = new Sprite(dungeonTexture);
            app.stage.addChild(dungeon);
  2. 如果你是使用的 loader來加載紋理貼圖集, 使用loader的 resources:
  • explorer = new Sprite(
        resources["images/treasureHunter.json"].textures["explorer.png"]
    );
    explorer.x = 68;
    
    //Center the explorer vertically
    explorer.y = app.stage.height / 2 - explorer.height / 2;
    app.stage.addChild(explorer);
    3.要創建一個精靈需要輸入太多東西了! 所以我建議你給紋理貼圖集的textures對象創建一個叫做id的別名
  • id = PIXI.loader.resources["images/treasureHunter.json"].textures;
    
    //Make the treasure box using the alias
    treasure = new Sprite(id["treasure.png"]);
    app.stage.addChild(treasure);
    <!doctype html>
    <meta charset="utf-8">
    <title>Make a sprite from a texture atlas</title>
    <body>
    <script src="../pixi/pixi.min.js"></script>
    <script>
    
    //Aliases
    let Application = PIXI.Application,
        Container = PIXI.Container,
        loader = PIXI.loader,
        resources = PIXI.loader.resources,
        TextureCache = PIXI.utils.TextureCache,
        Sprite = PIXI.Sprite,
        Rectangle = PIXI.Rectangle;
    
    //Create a Pixi Application
    let app = new Application({
        width: 512,
        height: 512,                       
        antialias: true,
        transparent: false,
        resolution: 1
      }
    );
    
    //Add the canvas that Pixi automatically created for you to the HTML document
    document.body.appendChild(app.view);
    
    //load a JSON file and run the `setup` function when it's done
    loader
      .add("images/treasureHunter.json")
      .load(setup);
    
    //Define variables that might be used in more
    //than one function
    let dungeon, explorer, treasure, door, id;
    
    function setup() {
    
      //There are 3 ways to make sprites from textures atlas frames
    
      //1. Access the `TextureCache` directly
      let dungeonTexture = TextureCache["dungeon.png"];
      dungeon = new Sprite(dungeonTexture);
      app.stage.addChild(dungeon);
    
      //2. Access the texture using throuhg the loader's `resources`:
      explorer = new Sprite(
        resources["images/treasureHunter.json"].textures["explorer.png"]
      );
      explorer.x = 68;
    
      //Center the explorer vertically
      explorer.y = app.stage.height / 2 - explorer.height / 2;
      app.stage.addChild(explorer);
    
      //3. Create an optional alias called `id` for all the texture atlas
      //frame id textures.
      id = PIXI.loader.resources["images/treasureHunter.json"].textures;
    
      //Make the treasure box using the alias
      treasure = new Sprite(id["treasure.png"]);
      app.stage.addChild(treasure);
    
      //Position the treasure next to the right edge of the canvas
      treasure.x = app.stage.width - treasure.width - 48;
      treasure.y = app.stage.height / 2 - treasure.height / 2;
      app.stage.addChild(treasure);
    
      //Make the exit door
      door = new Sprite(id["door.png"]);
      door.position.set(32, 0);
      app.stage.addChild(door);
    
      //Make the blobs
      let numberOfBlobs = 6,
          spacing = 48,
          xOffset = 150;
    
      //Make as many blobs as there are `numberOfBlobs`
      for (let i = 0; i < numberOfBlobs; i++) {
    
        //Make a blob
        let blob = new Sprite(id["blob.png"]);
    
        //Space each blob horizontally according to the `spacing` value.
        //`xOffset` determines the point from the left of the screen
        //at which the first blob should be added.
        let x = spacing * i + xOffset;
    
        //Give the blob a random y position
        //(`randomInt` is a custom function - see below)
        let y = randomInt(0, app.stage.height - blob.height);
    
        //Set the blob's position
        blob.x = x;
        blob.y = y;
    
        //Add the blob sprite to the stage
        app.stage.addChild(blob);
      }
    }
    
    //The `randomInt` helper function
    function randomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    </script>
    </body>
    上 面是一個簡單的例子。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章