Using pixel colors as reference for level layouts

Get help using Construct 2

Post » Mon Sep 17, 2012 11:30 pm

hey guys, quick question,

I may have missed this elsewhere, but I'm wondering if there's a way to reference the colors of specific pixels of a sprite. This is commonly used for level layouts and whatnot, and I'm just curious if there's a way to do it within Construct 2. Thanks :)
B
64
S
8
G
9
Posts: 290
Reputation: 8,590

Post » Tue Sep 18, 2012 7:27 am

You can use the canvas plugin to reference pixels.
If you're sampling only a few pixels you can use the Canvas.rgbaAt(x,y) expression to get color values like this "rgba(0,0,0,0)", "rgba(255,255,0,1)".

I also added another expression Canvas.AsJSON which is the entire image in a format that can be loaded into an array with a load action. It is a very quick WIP, so it's not that intuitive. The array size ends up being (1,1,Canvas.Width*Canvas.Height*4).

To access a pixel use this expression:
Array.At(0, 0, (y*Canvas.Width + x)*4 + component)
Where x,y is the pixel position and component is 0 for red, 1 for green, 2 for blue and 3 for alpha. Each component will be a number in the range of 0 to 255.R0J0hound2013-12-17 01:52:58
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Tue Sep 18, 2012 7:30 am

hmmmm, very interesting... I've always liked your style ROJO lol
B
64
S
8
G
9
Posts: 290
Reputation: 8,590

Post » Tue Sep 18, 2012 10:45 am

also it worked like a charm :)
B
64
S
8
G
9
Posts: 290
Reputation: 8,590

Post » Tue Jan 28, 2014 5:15 am

@brent_hamel

How'd you got it to work? I have no luck.

I've put an image into one canvas (FromCanvas) and want to copy it, pixel by pixel, onto anther canvas (ToCanvas). If I use FromCanvas.rgbaAt() to read pixels from the first canvas then it works but everything is excruciatingly slow. However, if I create an array and do "Load from JSON string FromCanvas.AsJSON" it looks like I always get all zeros when reading from this array, although it does appear that this option would be much faster if I could populate my array correctly. Debugger won't let me see what's really inside the array, it just says that array data is "[object Object]".

I never used JSON with Construct 2 before and can't figure out what I'm doing wrong. Did you encounter any issues or it just work straight away?TheM2014-01-28 22:34:26
B
3
Posts: 1
Reputation: 227

Post » Fri Jan 31, 2014 8:58 pm

Well all I was doing was referencing the colors of the pixels on my canvas, to load an array with values based on the specific colors colors. I did it all using the rgbAt expression you mentioned. It worked fast and efficiently for me, so I'm not sure where you're running into trouble. I didn't use any JSON at all.
B
64
S
8
G
9
Posts: 290
Reputation: 8,590


Return to How do I....?

Who is online

Users browsing this forum: BipolarMike, totoe and 17 guests