Using pixel colors as reference for level layouts

0 favourites
  • 6 posts
From the Asset Store
Color Flow
$2 USD
60% off
Color all tiles with the same color in 22 moves or less!
  • 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 :)

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • hmmmm, very interesting... I've always liked your style ROJO lol

  • also it worked like a charm :)

  • 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?

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)