Jump to content

Collision problem


winspeednl
 Share

Recommended Posts

Hi,

 

I want a sprite to collide with a group, i tried:

var gameState = {    create:function(){        map = game.add.tilemap("level1");        map.addTilesetImage("rock");        map.addTilesetImage("crate");        map.addTilesetImage("lava");        map.addTilesetImage("key");        map.setCollisionBetween(1,4);        levelLayer = map.createLayer("level");        //lavaGroup = new Phaser.Group(game);        lavaGroup = game.add.group();        //game.physics.enable(lavaGroup, Phaser.Physics.ARCADE);                for (var x = 0; x < window.innerWidth; x+=32) {            for (var y = 0; y < window.innerWidth; y+=32) {                if (map.getTileWorldXY(x, y, tileSize, tileSize, levelLayer) != null) {                    if (map.getTileWorldXY(x, y, tileSize, tileSize, levelLayer).index == 3) {                        lavaGroup.create(x, y, "lava");                        map.removeTileWorldXY(x, y, tileSize, tileSize, levelLayer);                    }                }            }        }        console.log(lavaGroup);    },    update:function(){        game.physics.arcade.collide(player, lavaGroup, die);        game.physics.arcade.overlap(player, lavaGroup, die); //the event doesnt get called and the player just falls thru. :/    }};function die(){    player.x = 64;    player.y = 250;}

Output of console.log(lavaGroup):

Phaser.Group {game: Phaser.Game, name: "group", z: 2, position: Phaser.Point, scale: Phaser.Point…}_bounds: Phaser.Rectanglebottom: (...)bottomRight: (...)centerX: (...)centerY: (...)empty: (...)halfHeight: (...)halfWidth: (...)height: 1left: (...)perimeter: (...)randomX: (...)randomY: (...)right: (...)top: (...)topLeft: (...)topRight: (...)type: 22volume: (...)width: 1x: 0y: 0__proto__: Phaser.Rectangle_cacheAsBitmap: false_cacheIsDirty: false_cr: 1_currentBounds: null_hash: Array[4]0: Phaser.Sprite_bounds: Phaser.Rectangle_cacheAsBitmap: false_cacheIsDirty: false_cr: 1_currentBounds: null_exists: true_frame: Phaser.Frame_height: 0_mask: null_sr: 0_width: 0alive: truealpha: 1anchor: Phaser.Pointangle: (...)animations: Phaser.AnimationManagerblendMode: 0body: nullbottom: (...)cacheAsBitmap: (...)cachedTint: 16777215cameraOffset: Phaser.Pointchildren: Array[0]deltaX: (...)deltaY: (...)deltaZ: (...)events: Phaser.Eventsexists: (...)filterArea: nullfilters: (...)fixedToCamera: (...)frame: (...)frameName: (...)fresh: falsegame: Phaser.Gameheight: (...)hitArea: nullinCamera: (...)inWorld: (...)inputEnabled: (...)key: "lava"left: (...)mask: (...)offsetX: (...)offsetY: (...)parent: Phaser.Group_bounds: Phaser.Rectangle_cacheAsBitmap: false_cacheIsDirty: false_cr: 1_currentBounds: null_hash: Array[4]_mask: null_sortProperty: "z"_sr: 0alive: truealpha: 1angle: (...)cacheAsBitmap: (...)cameraOffset: Phaser.Pointchildren: Array[4]classType: function (game, x, y, key, frame) {cursor: Phaser.SpritecursorIndex: 0enableBody: falseenableBodyDebug: falseexists: truefilterArea: nullfilters: (...)fixedToCamera: falsegame: Phaser.Gameheight: (...)hitArea: nullignoreDestroy: falselength: (...)mask: (...)name: "group"onDestroy: Phaser.Signalparent: Phaser.WorldphysicsBodyType: 0physicsType: 7pivot: Phaser.Pointposition: Phaser.Pointrenderable: falserotation: 0scale: Phaser.Pointstage: Phaser.Stagetotal: (...)transformCallback: nulltransformCallbackContext: nulltype: 7visible: truewidth: (...)worldAlpha: 1worldTransform: Phaser.MatrixworldVisible: (...)x: (...)y: (...)z: 2__proto__: Phaser.GroupphysicsType: 0pivot: Phaser.Pointposition: Phaser.PointpreviousPosition: Phaser.PointpreviousRotation: 0renderOrderID: 4renderable: trueright: (...)rotation: 0scale: Phaser.Pointshader: nullsmoothed: (...)stage: Phaser.Stagetexture: PIXI.Texturetint: 16777215top: (...)transformCallback: nulltransformCallbackContext: nulltype: 0visible: truewidth: (...)world: Phaser.PointworldAlpha: 1worldTransform: Phaser.MatrixworldVisible: (...)x: (...)y: (...)z: 1__proto__: Phaser.Sprite1: Phaser.Sprite2: Phaser.Sprite3: Phaser.Spritelength: 4__proto__: Array[0]_mask: null_sortProperty: "z"_sr: 0alive: truealpha: 1angle: (...)cacheAsBitmap: (...)cameraOffset: Phaser.Pointchildren: Array[4]classType: function (game, x, y, key, frame) {cursor: Phaser.SpritecursorIndex: 0enableBody: falseenableBodyDebug: falseexists: truefilterArea: nullfilters: (...)fixedToCamera: falsegame: Phaser.Gameheight: (...)hitArea: nullignoreDestroy: falselength: (...)mask: (...)name: "group"onDestroy: Phaser.Signalparent: Phaser.WorldphysicsBodyType: 0physicsType: 7pivot: Phaser.Pointtype: 25x: 0y: 0__proto__: Phaser.Pointposition: Phaser.Pointtype: 25x: 0y: 0__proto__: Phaser.Pointrenderable: falserotation: 0scale: Phaser.Pointtype: 25x: 1y: 1__proto__: Phaser.Pointstage: Phaser.Stage_backgroundColor: 3355443_bounds: Phaser.Rectangle_cacheAsBitmap: false_cacheIsDirty: false_cr: 1_currentBounds: null_hiddenVar: "webkitvisibilitychange"_mask: null_onChange: function (event) {_sr: 0alpha: 1backgroundColor: (...)backgroundColorSplit: Array[3]backgroundColorString: "#333333"cacheAsBitmap: (...)children: Array[1]currentRenderOrderID: 6disableVisibilityChange: falseexists: truefilterArea: nullfilters: (...)game: Phaser.Gameheight: (...)hitArea: nullmask: (...)name: "_stage_root"parent: nullpivot: Phaser.Pointposition: Phaser.Pointrenderable: falserotation: 0scale: Phaser.Pointsmoothed: (...)stage: Phaser.StagetransformCallback: nulltransformCallbackContext: nullvisible: truewidth: (...)worldAlpha: 1worldTransform: Phaser.MatrixworldVisible: (...)x: (...)y: (...)__proto__: Phaser.Stagetotal: (...)transformCallback: nulltransformCallbackContext: nulltype: 7visible: truewidth: (...)worldAlpha: 1worldTransform: Phaser.Matrixa: 1b: 0c: 0d: 1tx: 0ty: 0type: 24__proto__: Phaser.MatrixworldVisible: (...)x: (...)y: (...)z: 2__proto__: Phaser.Group_hash : Array[4]    0: Phaser.Sprite    1: Phaser.sprite    ...

I get no errors but the collision/overlap doesn't work

 

Thanks

Link to comment
Share on other sites

I can make anything of that "lavaGroup" console ouput, but have you looked at the documentation on arcade.collide here? The 2 colliding objects will get passed to your callback function "die". So maybe try something like this code below, to see if the callback function gets called at all, and if so then what goes wrong.



function die(pl, lav) {
    //debugger;
    console.log('die was called - pl.name'+pl.name+' lav.x='+lav.x);
    pl.x = 64;
    pl.y = 250;
}


If you're working in Chrome, you can de-comment the debugger; line to stop there and go through the code step-by-step.

Link to comment
Share on other sites

Thanks for your replay BdR

Still no errors but the player(sprite) just falls thru the group(lavaGroup) how can i prevent that

I cant get it to work

update: function(){    //game.physics.arcade.collide(player, lavaGroup, die, null, this); // :/    var bool = game.physics.arcade.overlap(player, lavaGroup, die); // no collision no output    console.log(bool); // only says false even when player overlaps/* why is the player falling thru */}

btw i use Intel XDK

Link to comment
Share on other sites

You've added lavaGroup the game which is good, but did you also add the player sprite to the game? So something like this:

lavaGroup = game.add.group(); // was already okplayer = game.add.sprite(80, 80, 'guybitmap'); // <-

Also, if you do debugger just before the arcade.overlap line you can inspect the variables. Is the player variable set correctly at that point, or is it undefined or null?

Link to comment
Share on other sites

How i create the player:

player = game.add.sprite(64, 100, 'player'); //player is a spritesheet of 24x24 per frameplayer.anchor.setTo(0.5);game.physics.enable(player, Phaser.Physics.ARCADE);player.body.gravity.y = 250; game.input.onDown.add(addBlock, this);        player.body.bounce.y = 0.2;player.body.collideWorldBounds = true;        player.animations.add('left', [0, 1, 2, 3], 10, true);player.animations.add('right', [4, 5, 6, 7], 10, true);player.animations.play('right');

Thanks MichaelD i forgot that, Thanks!

it is working, but one more problem the lava tile falls down when the player touches it,

lavaGroup.enableBody = true;lavaGroup.physicsBodyType = Phaser.Physics.ARCADE;lavaGroup.setAll('body.immovable', true); // This doesnt work?
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...