Jump to content

extending PixiJS class to create multiple sprites with different textures


rip
 Share

Recommended Posts

I'd like to revise this PixiJS example codepen to use multiple textures.

https://codepen.io/ripmurdock/pen/wvQGjoW?editors=0011

The AwesomeSprite class extends PIXI.Sprite.  I expected that each sprite created with this class would use the texture that is passed at the time the class is called.

When the class is called multiple times to create multiple instances with different texture parameters, every instance uses the first texture passed.

I've tried creating multiple classes with unique class names.  Even if the classes are duplicated, PIXI.Sprite creates a new sprite with the first texture that is passed.

How can I revise this code, so that PIXI.Sprite creates a new sprite with the current texture parameter each time the AwesomeSprite class is called?

Thanks!

gsap.config({trialWarn: false})
console.clear();

asyncCall();

async function asyncCall() {
let minX = 0;
let minY = 0;
let maxX = window.innerWidth;
let maxY = window.innerHeight;

let total   = 0;
let batch   = 3;
let limit   = 5000;

let ease  = Power0.easeNone;  

const app = new PIXI.Application({autoResize: true, backgroundAlpha: 0});

app.renderer.resize(maxX, maxY);

PIXI.Assets.addBundle('texturesBundle', {
    bunny: 'https://pixijs.io/examples/examples/assets/bunny.png',
    eggHead: 'https://pixijs.io/examples/examples/assets/eggHead.png',
    flowerTop: 'https://pixijs.io/examples/examples/assets/flowerTop.png',
  });

const assets = await PIXI.Assets.loadBundle('texturesBundle');

let sprites = [];

document.body.appendChild(app.view);  

let container = new PIXI.ParticleContainer(limit, {
  scale: true,
  position: true,
  rotation: true,
  uvs: false,
  alpha: true
});


app.stage.addChild(container);

//
// AwesomeSprite
// ========================================================================
class AwesomeSprite extends PIXI.Sprite {
  constructor(texture) {
    super(texture);       
    let repeat = -1;
    let yoyo = true;
    let time1  = Math.random()*(3-0.5)+0.5
    let time2  = Math.random()*(4.5-2)+2
    let time3  = Math.random()*(4.5-2)+2
    let prog1  = Math.random()
    let prog2  = Math.random()
    let prog3  = Math.random()
    let scale  = Math.random()*0.5
    let alpha  = 1
    let speedX = (Math.random()*(10+10)-10) * (-1 + Math.round(Math.random()) * 2)
    let speedY = (Math.random()*(10-2)+2) * (-1 + Math.round(Math.random()) * 2)   
    let startX = .5*(maxX)
    let startY = .5*(maxY)
    let angle  = (-1 + Math.round(Math.random()) * 2) * Math.PI * 2

    this.anchor.set(0.5);
    this.pivot.set(0.5);
    this.scale.set(0.1);
    
    this.x = startX;
    this.y = startY;
    this.alpha = alpha;               
    this.rotation = 0;    
    
    this.speed = new PIXI.Point(speedX, speedY);
        
    gsap.to(this,{ repeat, ease, rotation: angle, duration: time1, }).progress(prog1);    
    gsap.to(this.scale,{ repeat, ease, yoyo, x: 1, y: 1, duration: time3, }).progress(prog3);   
  }
 
  update() {
    
    this.x += this.speed.x;
    this.y += this.speed.y;

    if (this.x > maxX) {
      
      this.x = maxX;
      this.speed.x *= -1;
      
    } else if (this.x < minX) {
      
      this.x = minX;
      this.speed.x *= -1;
    }
    
    if (this.y > maxY) {
      
      this.y = maxY;
      this.speed.y *= -1;
      
    } else if (this.y < minY) {
      
      this.y = minY;
      //this.speed.y = 0;
      //this.speed.y = 0;
      this.speed.y *= -1;
    }
  }
}

    gsap.ticker.add(render);
    createSprites(batch);
    let sprite = PIXI.Sprite.from(assets.bunny);
    sprite.anchor.set(0.5);
    sprite.x = app.screen.width * 2/9;
    sprite.y = app.screen.height / 2;
    app.stage.addChild(sprite);
 
    sprite = PIXI.Sprite.from(assets.eggHead);
    sprite.anchor.set(0.5);
    sprite.x = app.screen.width * 4/9;
    sprite.y = app.screen.height / 2;
    app.stage.addChild(sprite);
 
    sprite = PIXI.Sprite.from(assets.flowerTop);
    sprite.anchor.set(0.5);
    sprite.x = app.screen.width * 6/9;
    sprite.y = app.screen.height / 2;
    app.stage.addChild(sprite);
 
//
// CREATE sprites
// ========================================================================
function createSprites(count) {
  //console.log(count)
  for (let i = 0; i < count; i++) {    
   createSprite(i)
  }
 
  function createSprite(i) {
    let sprite0
    switch(i%3) {
      case 0:
        sprite0 = new AwesomeSprite(assets.eggHead);
        break;
      case 1:
        sprite0 = new AwesomeSprite(assets.bunny);
        break;
      default:
        sprite0 = new AwesomeSprite(assets.flowerTop);
    }
        
    sprites.push(sprite0);
    container.addChild(sprite0);
    total++
  }
}

//
// RENDER
// ========================================================================
function render() {

  for (let i = 0; i < total; i++) {
    sprites[i].update();
  }
 
  app.renderer.render(app.stage);
}
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...