[SOLVED] Check if 3 Textures Align in a row

I want to check if 3 textures align in the same row, kind of like in Candy Crush. My problem is that I don’t know how I would go about on this. What I assume would be needed to be done is something in each texture that checks other textures to see if any are similar to itself. If 3 textures have found that they use the same texture, then it’ll alert in the console. As I said before though, I really don’t have much of an idea on how I would go about this on PlayCanvas.

@XKRS-GT already made the code that swaps each texture if they are close to each other.

If you want to check three elements are using the same sprite, check my answer here:

Ok, I’ll look into it soon.

1 Like

Hey @Leonidas,

We tried this today. In the below code, this.one, this.two, this.three, and this.four are images.

if ((this.one.element.texture === this.two.element.texture) && (this.two.element.texture === this.three.element.texture) &&(this.one.element.texture === this.four.element.texture)) {
        if (this.checkall4 === false) {
            let top = (this.getTop(this.one.parent.name));
            this.one.texture = this.app.root.findByName(top).children[0].texture;
            //alert("top");
            this.sound.sound.play("3");
            
            this.checkall4 = true;
        }
    }
    
    else if ((this.one.element.texture === this.two.element.texture) && (this.two.element.texture === this.three.element.texture)) {
        if (this.checkf3 === false) {
            let top = (this.getTop(this.one.parent.name));
            this.one.texture = this.app.root.findByName(top).children[0].texture;
            
            this.sound.sound.play("3");
            this.checkf3 = true;
        }
    }
    
    else if ((this.two.element.texture === this.three.element.texture) && (this.two.element.texture === this.four.element.texture)) {
        if (this.checkl3 === false) {
            let top = (this.getTop(this.one.parent.name));
            this.one.texture = this.app.root.findByName(top).children[0].texture;
            
            this.sound.sound.play("3");
            this.checkl3 = true;
        }
    }

However, this doesn’t switch the textures. What could be the problem? The function getTop returns the name of the Image above, and the function works correctly. The sound plays but the textures don’t switch :thinking:.

I think this is wrong, since texture is not a direct property of an entity.

Maybe you mean this:

this.app.root.findByName(top).children[0].element.texture;

Oh god, yes, that was an obvious one. But I’m confused as to why there were no errors? Usually the engine throws up errors if I overlook something simple like this.

The reason it didn’t throw an error is because from the engine point of view the code is correct:

// this evaluates to undefined
this.app.root.findByName(top).children[0].texture; 

// then you assign it to property `texture`, which 
// might be what you actually wanted to do, the engine doesn't know
this.one.texture = undefined;
2 Likes

Ah. OK, thank you @LeXXik.

1 Like