Вопрос

I have a tilemap and am adding another layer containing an image of a tetris block on top of the tilemap layer. I want the player on the original tilemap to recognize the image added on as a solid entity. I am not sure how to go about this problem? Is there a physics package I can use or a property of a sprite object that I can modify to make the image a solid entity?

I tried the following code:

layer = map.createLayer('World1');
spriteLayer = game.add.group();
spriteLayer.z = 1;

// Add sprites to spriteLayer.
tetris = game.add.sprite(200, 120, 'tetris');
tetris.anchor.setTo(0.1, 0.5);
spriteLayer.add(tetris);             

function update() {
game.physics.arcade.collide(p, spriteLayer); 
game.physics.arcade.collide(p, layer);
...
}

but that does not seem to work. Sprite layer is the layer containing the image of the tetris block and p is my player. I can't figure out how to fix this problem and would appreciate any help or any other ideas on how to approach it. Thanks!

Это было полезно?

Решение

You can collide a Sprite against as many things as you like, but each check has to be its own collide call. So basically what you're doing above is fine (assuming 'p' is your player), but you need to make sure that your Tetris sprite and Player sprite have both been enabled for physics collision.

If everything in spriteLayer is going to collide you can do this easily with:

spriteLayer = game.add.physicsGroup();

Then anything added to it will have an Arcade Physics body enabled by default.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top