Several obstacles need several collisions - Problem with masks


(Wilko) #1

Hi and in a first time sorry if my english is bad.

I’m working on a school project and I need help about masks. My game is a runner and I have various pattern (image with a pixelMask) which constantly spawn. But my problem is that I want different type of obstacle on each pattern, so can I differantiate the differents types if I use color or alpha ?

For example :

I have a pattern which is a single PNG file with a blue block and a red block.

In this pattern I want that when my player collide the blue block, he’s just blocked and when he collide the red block, he die. Can I use any function to do that or I have to write the position of my red block in the code ?

Thanks for your responses.


(azrafe7) #2

Not really sure I understand what your problem is, so correct me if I get it wrong.

What I think you could do is use two separate entities for the blocks, so that you can assign a different collision type for each one of them, and have the player react accordingly when colliding.

I’m figuring a setup somewhat like this:

// BlueBlockEntity.as constructor
public function BlueBlockEntity(...) {
    ...
    // assign graphic, position, collision mask, etc.
    ...
    type = "blue_block";
}

// RedBlockEntity.as constructor
public function RedBlockEntity(...) {
    ...
    // assign graphic, position, collision mask, etc.
    ...
    type = "red_block";
}

// PlayerEntity.as update()
override public function update() {
    ...
    // update player logic
    ...

    // die on collision with red blocks
    var collidingRedBlock:RedBlockEntity = collideTypes("red_block", x, y) as RedBlock;
    if (collidingRedBlock != null) {
        die();
    } else {
        // move player but stop it if collides with blue blocks
        moveBy(xSpeed, ySpeed, "blue_block");
    }
}

(Wilko) #3

Yes, thank you, it’s a solution. But, it’s not me who is in charge of the level design so my aim was to know if we can access to native data of the image (like alpha or color) to simplify the work and allow my level designer to test their pattern even if I’m not here. But if it’s not possible I gonna use this method azrafe7.


(Per_K) #4

I might be completely of on the wrong track here, but; is what you want to do to get information about the color at a certain point?

There is a getPixel(x:int, y:int) function in Canvas that might be good for that.

I will right away admit that I’m fumbling in the dark here, but if this is a workable idea maybe someone with better knowledge could elaborate on this (or possibly explain why this is a dumb idea) .