Jump to content

Problem with overlapping sprite and group


quiphop
 Share

Recommended Posts

My overlap doesn't work and i don't know why. Here's the code for group

var Bonus = function(game){    // this.sprites = sprite;    this.bonuses = null;    this.isTimeToCreate = false;    this.tickLoop = function(){        this.isTimeToCreate = true;    };    this.createEnemy = function(){            var icecream = this.bonuses.create(1000, game.rnd.integerInRange(100, 300), 'icecream');            // policeman.scale.setTo(1.7,1.7);            game.physics.enable(icecream);            icecream.body.velocity.x = -80;            };}Bonus.prototype = {    create: function(game){        this.bonuses = game.add.group();        this.bonuses.enableBody =true;        this.bonuses.physicsBodyType = Phaser.Physics.ARCADE;        this.createEnemy();        var TIMER = game.time.events.loop(5000, this.tickLoop, this);    },    update:function(){        var random = Math.floor((Math.random() * 100) + 1)        if(this.isTimeToCreate ){            // console.log(random);            if(random < 50){            this.createEnemy();            this.isTimeToCreate = false;          }          else {            this.isTimeToCreate = false;          }        }    }}

and for sprite:

var Player = function(game,currentSprite){    this.currentSprite = currentSprite;    this.cursors = null;    // this.sprites = sprite;    this.sprite = null}Player.prototype = {    create: function(game){        switch(this.currentSprite){            case 1 :this.sprite = game.add.sprite(170, 0, 'lvl_1_player');break;            case 2 :this.sprite = game.add.sprite(170, 0, 'lvl_2_player');break;            case 3 :this.sprite = game.add.sprite(170, 0, 'lvl_3_player');break;        }        this.sprite.animations.add('run');        this.sprite.animations.play('run', 8, true);        game.physics.enable(this.sprite);           this.sprite.body.collideWorldBounds = true;        this.sprite.body.enableBodyDebug = true;        this.sprite.body.bounce.y = 0.2;        this.sprite.body.gravity.y = 1100;        // this.sprite.flipped = true;        this.sprite.body.height = 190;        // console.log(this.sprite);        this.cursors = game.input.keyboard.createCursorKeys();    },    update:function(){        if(this.cursors.up.isDown && this.sprite.body.touching.down){            // console.log("KEY");            this.sprite.body.velocity.y = -600;        }    }}

and overlap(main update function):

function update() {    game.physics.arcade.collide(player.sprite, level.platforms);    //game.physics.arcade.collide(level.stars, level.platforms);    game.physics.arcade.overlap( enemy.enemyes,player.sprite,collectStar,null,this );//    game.physics.arcade.overlap(player.sprite, level.stars, collectStar, null, this);    player.update();    level.update();    enemy.update(); }

Why it doesn't work?

Link to comment
Share on other sites

So I'v figured out the problem.
Now Bonus class looks like like:
 

var Bonus = function(game){    // this.sprites = sprite;    this.bonuses = null;    this.bonuses = game.add.group();    this.bonuses.enableBody =true;    this.isTimeToCreate = false;    this.tickLoop = function(){        this.isTimeToCreate = true;    };    this.createEnemy = function(){            var icecream = this.bonuses.create(1000, game.rnd.integerInRange(100, 300), 'icecream');            icecream.frame = game.rnd.integerInRange(1,6);            icecream.body.velocity.x = -80;            };}Bonus.prototype = {    create: function(game){           this.createEnemy();    },

And it creates at main create function:
 

icecream = new Bonus(game);        icecream.create(game);        iceCreamTimer = game.time.events.loop(5000, function(){            icecream.create(game);        }, this); 
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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