Jump to content

Alphamask for multiple sprites?


kirabook
 Share

Recommended Posts

So, I feel like maybe what I'm trying to do is very simple and I'm overthinking it, but I've spent the entire day on this and I think I just need someone to put me on the right track or tell me it's not possible.

Essentially, I want to use 1 image to mask multiple sprites.

I have various panels that look like this (ignore the images used, they're placeholders)

screenshot-localhost-2018_04.28-21-32-07.png.0535749c685018a75bb2b4160435bca8.png

In each panel, I intend to have multiple sprites inside of it. Technically, it works. Here is the first "layer" of the sprite that overlaps the red panels. This works too, as intended. It is currently stacked like:


- Background image
- Red panel

screenshot-localhost-2018_04.28-21-34-55.png.b692501bc9a7ee68cde49206f09e050a.png

SO, the issue comes in when I want to mask more images on top. Again, it technically works... but I guess the issue is that the red panel is still being used to create the mask (which is technically what's supposed to happen), but it's not what I was intending. (I'm providing images because I'm terrible at explaining, sorry). In this image below, it's become stacked like

- Sprite B
- Red Panel
- Sprite A
- Red Panel
- Background image
- Red panel

screenshot-localhost-2018_04.28-21-38-29.png.d6f65c120a266b05934cdbe407a90051.png

 

What I need to happen is for the red panel to act as the mask for multiple sprites. Is this possible? I've been doing some reading and searching and should I be using a polygon mask? Below is the code I made for this. Hopefully it makes some sense. Please excuse the mess

//Call this in every state for now, but maybe we can make it universal
		var panelsJSON = this.cache.getJSON('panels');
		var testpage = this.cache.getJSON('testpage');
		console.log(panelsJSON);
		console.log(testpage);

		for (i = 0; i <  Object.keys(testpage.pages).length; i++) {
			//This loop is for building pages
			//sets the background of the page
			this.add.sprite(0, 0, testpage.pages[i]["bg"]);

			//sets a temporary variable that stores the panels
			var panels = testpage.pages[i]["panels"];

			for (a = 0; a < panels.length; a++) {
				//This loop is to piece together each panel on each page

				//setting variables for each panel
				var type = panels[a]["type"];
				var sprites = panels[a]["sprites"];
				var panelBg = panels[a]["bg"];

				//setting variables for panel types
				var panelWidth = panelsJSON.action[type]["width"];
				var panelHeight = panelsJSON.action[type]["height"];
				var panelPositionX = panelsJSON.action[type]["positionx"];
				var panelPositionY = panelsJSON.action[type]["positiony"];

				//Setting bitmap data for panel based on which type
				panelsJSON.action[type]["bitmapData"] = this.make.bitmapData(panelWidth, panelHeight);
				var bitmapData = panelsJSON.action[type]["bitmapData"];

				//setting panel bg using the bitmap data
				panelsJSON.action[type]["alphaMaskBg"] = bitmapData.alphaMask(panelBg, type);
				var alphaMaskBg = panelsJSON.action[type]["alphaMaskBg"];
				this.add.sprite(panelPositionX, panelPositionY, alphaMaskBg);

				for (s = 0; s < sprites.length; s++) {
					//This loop is for each sprite included in each panel including characters and objects
					//The problem is I'm using the bitmapData variable here again, which masks the sprite based on the red panel, which is the problem. 
					var spriteName = sprites[s]["name"];
					panelsJSON.action[type]["alphaMaskSprite"+s] = bitmapData.alphaMask(spriteName, type);
					var alphaMaskSprite = panelsJSON.action[type]["alphaMaskSprite"+s];
					this.add.sprite(panelPositionX, panelPositionY, alphaMaskSprite);
				}
			}
		}

At some point, I thought maybe if I use the alphaMaskBg variable that I created as the alphamask data would create the effect I wanted, but the red panels still plagued me

				for (s = 0; s < sprites.length; s++) {
					//This loop is for each sprite included in each panel including characters and objects
					//The problem is I'm using the bitmapData variable here again, which masks the sprite based on the red panel, which is the problem. 
					var spriteName = sprites[s]["name"];
					panelsJSON.action[type]["alphaMaskSprite"+s] = alphaMaskBg.alphaMask(spriteName, type);
					var alphaMaskSprite = panelsJSON.action[type]["alphaMaskSprite"+s];
					this.add.sprite(panelPositionX, panelPositionY, alphaMaskSprite);
				}

 

My only other thought was to make the red panel images transparent, but that defeats the purpose. Then my next leap of logic was to use phaser to make the red panels transparent, but that probably doesn't make much sense either (if it's even possible). If someone could set me on the right path, it'd be greatly appreciated

In the end, I need it to work like:

- Sprite B
- Sprite A
- Background image
- Red panel

 

Link to comment
Share on other sites

Well, I ended up working out the solution. It's a little more work for me, but I've created the mask with a polygon instead. Now it properly trims the edges of all the sprites. To make even more simple, each sprite has been added as a child to the background sprite. Hopefully that doesn't cause me a headache later on. This method does seem to have some performance issues, but I'm not sure how else to achieve this. 

** Polygon mask on top **
---- Sprite B

---- Sprite A
- Background image

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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