Jump to content

overlap


Muthar
 Share

Recommended Posts

hey guys

 
 

hey i am new this , i am making a game and in this game i wanted when the player overlap with the coin, the coin disappear but its not over for some reason and i don't why. i have tired a lot of ways even i tired asking the coin into a tile when i made the map but even than it still doesn't work

 

this is my code

 var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { 
 preload: preload, create: create, update: update, render: render });

 function preload() {
game.load.tilemap('level12', 'res/level12.json', null, 
Phaser.Tilemap.TILED_JSON);
game.load.image('sprite12', 'res/sprite12.png');
game.load.spritesheet('dude', 'res/player.png', 64, 64);
game.load.spritesheet('droid', 'res/droid.png', 32, 32);
game.load.image('starSmall', 'res/star.png');
game.load.spritesheet('coin', 'res/coin.png',32,32 );
game.load.image('background', 'res/sprite3.png');

}

var map;
var tileset;
var layer;
var player;
var facing = 'left';
var jumpTimer = 0;
var cursors;
var jumpButton;
var bg;
var coin;


function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#000000';


bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;

map = game.add.tilemap('level12');
map.addTilesetImage('sprite12');
map.addTilesetImage('coin');
map.setCollisionBetween(1, 12);
map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);

layer = map.createLayer('Tile Layer 1');
layer.resizeWorld();

coin = game.add.group();
coin.enableBody = true;
coin.physicsBodyType = Phaser.Physics.ARCADE;
createcoin();


game.physics.arcade.gravity.y = 200;

player = game.add.sprite(150, 900, 'dude');


game.physics.enable(player, Phaser.Physics.ARCADE);

player.body.bounce.y = 0.2;


player.body.collideWorldBounds = true;



player.animations.add('left', [3,2,1,0], 10, true);
player.animations.add('right', [4,5,6,7], 10, true);


game.camera.follow(player);

cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);


game.physics.arcade.overlap(coin, player, killcoin(), null, this);

}
function killcoin(coin,player) {

coin.kill();
}



function  createcoin() {
coin = game.add.sprite(50,700, 'coin');
}

function update() {

game.physics.arcade.collide(player, layer);

player.body.velocity.x = 0;

if (cursors.left.isDown) {
    player.body.velocity.x = -150;

    if (facing != 'left') {
        player.animations.play('left');
        facing = 'left';
    }
}
else if (cursors.right.isDown) {
    player.body.velocity.x = 150;

    if (facing != 'right') {
        player.animations.play('right');
        facing = 'right';
    }
}
else {
    if (facing != 'idle') {
        player.animations.stop();

        if (facing == 'left') {
            player.frame = 0;
        }
        else {
            player.frame = 5;
        }

        facing = 'idle';
    }
}

if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer) {
    player.body.velocity.y = -250;
    jumpTimer = game.time.now + 750;
}
}

function render () {



}

 

 

 

if anyone can help would be great 

Regards

 

Link to comment
Share on other sites

What happens when the player overlaps the coin? Any errors?

This might be the problem:

game.physics.arcade.overlap(coin, player, killcoin(), null, this);

should be...

game.physics.arcade.overlap(coin, player, killcoin, null, this);

Try that :)

Link to comment
Share on other sites

ncil is right here, if the function expects a callback function, you have to use a reference to the callback function as argument. Here you're using an expression resulting from a function call.

But you might also want to read this (from Phaser 2.6.2 documentation) :

Quote

An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them, unless you are checking Group vs. Sprite, in which case Sprite will always be the first parameter.

 

Also your code, you're using "coin" as a group name, which I find misleading, you better use a plural for naming groups (or arrays). Have you tried to put more than coin in the level? What happens when the player overlaps with one of them?

P.S: Anyone else see the preformated code in barely legible colors (dark grey on darker grey)?

Link to comment
Share on other sites

The killcoin function is expecting two arguments but you're giving it none...

function killcoin(coin,player) {
coin.kill();
}

This code line

game.physics.arcade.overlap(coin, player, killcoin(), null, this);

Should be like this

game.physics.arcade.overlap(coin, player, killcoin(coin, player), null, this);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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