Jump to content

changing image on which multiple sprites are based


svatem1
 Share

Recommended Posts

Hi, I have image in cache on which multiple sprites are based like this:

a = game.add.sprite(0, 0, "image");
b = game.add.sprite(0, 0, "image");

Now: I want to change the underlying image's data like e.g. this (notice the "image" as argument to generateTexture):

game.cache.getBitmapData("somedata").generateTexture("image")

This fails at runtime. According to what I've found out so far, I have to do it like this:

game.cache.getBitmapData("somedata").generateTexture("image", function(texture) {
    a.loadTexture("image");
    b.loadTexture("image");
})

The question is: is this achievable via more elegant way? The main problem I have with this is I have to keep track of every sprite that's based on "image",  because this:

game.cache.getBitmapData("somedata").generateTexture("image", function(texture) {
    a.loadTexture("image");
    // b.loadTexture("image");
});

also fails at runtime. Isn't this somehow covered by what Phaser already offers? Thx in advance.

Link to comment
Share on other sites

As far as i understand that u want to change all texture's on one place. If this is the case, you can create an array and push all elements that u want to track, when u want to change those simply use forEach function and load textures on them.
 

var trackImages = [];
a = game.add.sprite(0, 0, 'image');
trackImages.push(a);
b = game.add.sprite(0, 0, 'image');
trackImages.push(b);


//when u want to change them..
trackImages.forEach(function(val) {
  val.loadTexture('newImage');
});

One other choice is that you can check here : https://phaser.io/examples/v2/particles/particle-class
I know its not about sprite's but you can simply extend Phaser.Sprite class to your custom class and update them all at once. I didn't try this out but it should work.

Link to comment
Share on other sites

Thanks, yeah, i know how to use basic JS to express what I want (and your example won't stand, generateTexture is asynchronous, which will complicate things if you want e.g. to extend Phaser.Sprite, imagine both a & b be both of that class and both call reRender() method that does what I've expressed). I'm interested merely in Phaser's internals. You linked a very good example. The one line in your example I'd like to find out either exists or not for my case is: 

bmd.dirty = true;

Notice this one line will automagically make all sprites having bmd as texture to update themselves. The reason I'm not using bitmapData as underlying data structure for my sprites is:

renderer.setTexturePriority(keys)

keys -> keys inside imageCache. If I could elevate multitexturing capability with bitmapData I wouldn't bother converting it and I'd be updating my sprites with that one dirty flag. So this is also good question: can I somehow elevate multitexturing capabilities with bitmapData? Thx.

Link to comment
Share on other sites

Thx again. Do you have anything specific in mind regarding the article you linked or it's just a result of generic "phaser multitexturing" google search? Because I'm (I mean - everybody is) fully capable of that (in fact I did some time ago and the result can be summarized as "setTexturePriority(keys) is a function that allows you to elevate multitexturing capability, keys are keys of Image objects in Phaser cache not bitmapData objects") and moreover the correct phrase to google was "phaser multitexturing bitmapdata".

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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