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>
    上 面是一个简单的例子。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值