Jump to content

obj1.kill()  kills both  obj1  and  obj2 ?


vmars316
 Share

Recommended Posts

Hello & Thanks ,
In the code below :
obj1.kill() works , but  btw: (with no //) obj2.kill()  does not .
And  obj1.kill()  kills both  obj1  and  obj2 .
And  (with no //) lies02sprite.body.visible = true;   does not work either .
I want  truth02sprite/obj2()  to remain visible . 
Why ?

Quote

    lies02sprite = game.add.sprite(0, 198, 'lies02');
    game.physics.enable(lies02sprite, Phaser.Physics.ARCADE);
    lies02sprite.body.velocity.x = 130;
    lies02sprite.body.immovable = true;
    lies02sprite.body.setSize(64, 64, 0, 0);
    lies02sprite.anchor.setTo(0.5, 0.5);    

Quote

function collisionTruth02 (obj1, obj2) {
    bullet.body.velocity.y = -500;
    obj1.kill();
//    obj2.kill();
    truth02sprite.body.visible = true;
    scoreTruths();
}

Example here : http://liesandcowpies.com/testMe/vm-phaser/vm-vertical-collision-Web.html 

Thanks 

Link to comment
Share on other sites

I don't see truth02sprite created anywhere in your code and try this for the visibility (remove body)

truth02sprite.visible = true;

This assumes that truth02sprite.visible was originally set to false.

Link to comment
Share on other sites

Thanks Batzi ,
truth02sprite.visible = true;   made it visible , but stopped  truth02sprite  dead .
so I tried  truth02sprite.body.velocity.x = 100;  , same difference .
so I tried  truth02sprite.velocity.x = 100;
That made the whole game stop dead .
Interesting though .
Thanks

Link to comment
Share on other sites

1 hour ago, vmars316 said:

Thanks Batzi ,
truth02sprite.visible = true;   made it visible , but stopped  truth02sprite  dead .
so I tried  truth02sprite.body.velocity.x = 100;  , same difference .
so I tried  truth02sprite.velocity.x = 100;
That made the whole game stop dead .
Interesting though .
Thanks

Did it fix your problem though or you having another issue?

Link to comment
Share on other sites

truth02sprite.visible = true;   made it visible , but stopped  truth02sprite  dead .

Yes , it made the sprite visible .

But it created a new prob , at  function collisionTruth02  

truth02sprite now stops dead . 

I forgot to answer one of your questions :

yes , truth02sprite is created: 

Quote

  truth02sprite = game.add.sprite(0, 198, 'truth02');
    game.physics.enable(truth02sprite, Phaser.Physics.ARCADE);
    truth02sprite.body.velocity.x = 100;
    truth02sprite.body.immovable = true;
    truth02sprite.body.setSize(64, 64, 0, 0);

Thanks

Link to comment
Share on other sites

Thanks Batzi ,

I want the  lies02sprite  to disappear when it gets hit , which is does , no prob with that .    lies**sprite  are the bad guys .

truth02sprite is a good guy . I want them to stay visible ;

But I want to penalize shooter  -1  point for hitting them .

I want  truth**sprite s  to keep moving .  No stopping !

 http://liesandcowpies.com/testMe/vm-phaser/vm-vertical-collision-Web.html 


Thanks

Link to comment
Share on other sites

2 minutes ago, vmars316 said:

Thanks Batzi ,

I want the  lies02sprite  to disappear when it gets hit , which is does , no prob with that .    lies**sprite  are the bad guys .

truth02sprite is a good guy . I want them to stay visible ;

But I want to penalize shooter  -1  point for hitting them .

I want  truth**sprite s  to keep moving .  No stopping !

 http://liesandcowpies.com/testMe/vm-phaser/vm-vertical-collision-Web.html 


Thanks

Can you please show me the whole code?

Link to comment
Share on other sites

Sure :

// C:\Phaser\vm-WIP\vm-vertical-collision.js
// C:\Phaser\Phaser.js
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'vm-vertical-collision', { preload: preload, create: create, update: update, render: render });

function preload() {

    game.load.image('dummyRect', 'dummyRect.png');
    game.load.image('truth01', 'truth01.png');
    game.load.image('truth02', 'truth02.png');
    game.load.image('lies01', 'lies01.png');
    game.load.image('lies02', 'lies02.png');
    game.load.image('cowpie', 'cowpie.png');
    game.load.image('bullet', 'cowpie.png');
    game.load.image('thrower', 'thrower.png');
//    game.load.image('bullet', 'C:/Phaser/phaser-2.4.4/DownloadExamples/phaser-examples-master/examples/assets/games/asteroids/bullets.png');

}

var truth01sprite;
var truth02sprite;
var cowpieSprite;
var throwerSprite;
var cursors;
var bullet;
var bullets;
var okGuys;
var fireRate = 500;
var nextFire = 0;
var bulletTime = 0;
var addThis;
var worldBoundsWidth = 800;
var scoreLies = 0;
var scoreTruth = 0;
var totalHits = 0;
var dummyRect;
var pacerRect;

function create() {
    game.world.setBounds(0, 0, 800, 600);
    game.physics.startSystem(Phaser.Physics.ARCADE);

    game.stage.backgroundColor = '#992d2d';
    var style = { font: "bold 18px Arial", fill: "#cdcdcd"};
    scoreLiesText = game.add.text(0, 0,"Score: 0", style);  
    scoreTruthText = game.add.text(160, 0,"oops!: 0", style);
    totalHitsText = game.add.text(310, 0, "Total Hits: 0", style);
    
//    dummyRect = new Phaser.Rectangle(0, 0, 16, 16);
    pacerRect = game.add.sprite(0, 200, 'dummyRect');
    game.physics.enable(pacerRect, Phaser.Physics.ARCADE);
    pacerRect.body.immovable = true;
    pacerRect.body.velocity.x = 500;
    
    truth01sprite = game.add.sprite(50, 28, 'truth01');
    game.physics.enable(truth01sprite, Phaser.Physics.ARCADE);
    truth01sprite.body.immovable = true;
    truth01sprite.body.velocity.x = 100;
    truth01sprite.body.setSize(64, 64, 0, 0);
    truth01sprite.anchor.setTo(0.5, 0.5);
    
    
    lies01sprite = game.add.sprite(0, 48, 'lies01');
    game.physics.enable(lies01sprite, Phaser.Physics.ARCADE);
    lies01sprite.body.immovable = true;
    lies01sprite.body.velocity.x = 130;    
    lies01sprite.body.setSize(64, 64, 0, 0);
    lies01sprite.anchor.setTo(0.5, 0.5);
    
    truth02sprite = game.add.sprite(0, 198, 'truth02');
    game.physics.enable(truth02sprite, Phaser.Physics.ARCADE);
    truth02sprite.body.velocity.x = 100;
    truth02sprite.body.immovable = true;
    truth02sprite.body.setSize(64, 64, 0, 0);
//    truth02sprite.set('anchor.x', 0.5);
//    truth02sprite.set('anchor.y', 0.5);
    
    lies02sprite = game.add.sprite(0, 198, 'lies02');
    game.physics.enable(lies02sprite, Phaser.Physics.ARCADE);
    lies02sprite.body.velocity.x = 130;
    lies02sprite.body.immovable = true;
    lies02sprite.body.setSize(64, 64, 0, 0);
    lies02sprite.anchor.setTo(0.5, 0.5);    
 //  See the offset bounding box for another example.
//    truth02sprite.body.setSize(64, 64, 0, 0);

    cowpieSprite = game.add.sprite(300, game.height -195 , 'cowpie');
    cowpieSprite.name = 'cowpie';
    game.physics.enable(cowpieSprite, Phaser.Physics.ARCADE);
    cowpieSprite.body.velocity.y = -300;
    cowpieSprite.body.acceleration.y = -300;
    
    //  Our cowpie bullets
    bullets = game.add.group();
    bullets.enableBody = true;
    game.physics.enable(bullets, Phaser.Physics.ARCADE);


    //  All 40 of them
    bullets.createMultiple(10, 'bullet');   // bullet becomes bullets 
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);
//    bullets.body.immovable = true;
//    bullets.body.velocity.y = -300;
//    bullets.body.acceleration.y = -300;
//  All 4 of them
//    okGuys.createMultiple(2, ('truth01sprite','truth02sprite'));   // truth01sprite becomes okGuys 
//    okGuys.setAll('anchor.x', 0.5);
//    okGuys.setAll('anchor.y', 0.5);    
    
//    throwerSprite = game.add.sprite(game.width / 2, game.height -125, 'thrower');
    throwerSprite = game.add.sprite(300, game.height -100, 'thrower');
    throwerSprite.name = 'thrower';
    game.physics.enable(throwerSprite, Phaser.Physics.ARCADE);
    throwerSprite.anchor.setTo(0.5, 0.5);    
//    throwerSprite.body.velocity.y = -300;
//    throwerSprite.body.acceleration.y = -300;

    cursors = game.input.keyboard.createCursorKeys();

}

function update() {
  //  game.physics.arcade.collide(cowpieSprite, truth02sprite, collisionHandler, null, this);
//    game.physics.arcade.collide(bullets, truth01sprite, collisionHandler, null, this);
//    game.physics.arcade.collide(bullets, lies01sprite, collisionHandler, null, this);
    game.physics.arcade.collide(bullets, truth02sprite, collisionTruth02, null, this);
    game.physics.arcade.collide(bullets, lies02sprite, collisionLies02, null, this);

    if (cursors.up.isDown)
    {
        fireBullet();
    }    
/*    else
    {
        sprite.body.acceleration.set(0);
    }
*/
    if (cursors.left.isDown)
    {
        throwerSprite.body.velocity.x= -100;
        throwerSprite.body.x  -=  4;
        throwerSprite.body.velocity.x = 0;
//        sprite.body.angularVelocity = -300;
    }

    else if (cursors.right.isDown)
    {
//        throwerSprite.body.velocity.x= 100;
        throwerSprite.body.x  +=  4;
        throwerSprite.body.velocity.x = 0;
    }
    else
    {
        throwerSprite.body.velocity.x = 0;
//        sprite.body.angularVelocity = 0;
    }
        
//        truth02sprite.body.velocity.x = 100; 
//        truth02sprite.scale.setTo(1, 1);
//        truth02sprite.play('moving');

        if (truth02sprite.x > worldBoundsWidth) 
    {
        truth02sprite.scale.setTo(-1, 1); 
        truth02sprite.body.velocity.x = -100;
    }
        if (truth02sprite.x < 0) 
    {
        truth02sprite.scale.setTo(1, 1); 
        truth02sprite.body.velocity.x = +100;
    }
        if (lies02sprite.x > worldBoundsWidth) 
    {
        lies02sprite.scale.setTo(-1, 1); 
        lies02sprite.body.velocity.x = -100;
    }
        if (lies02sprite.x < 0) 
    {
        lies02sprite.scale.setTo(1, 1); 
        lies02sprite.body.velocity.x = +100;
    }    
}    
function collisionTruth02 (obj1, obj2) {
    bullet.body.velocity.y = -500;
    obj1.kill();
//    obj2.kill();
    truth02sprite.visible = true;  //causes it to stop
    truth02sprite.body.velocity.x = 100;
    
//    truth02sprite.body.visible = false;
    scoreTruths();
}
    function collisionLies02 (obj1, obj2) {
    bullet.body.velocity.y = -500;
    lies02sprite.body.visible = false;
    obj1.kill();
    obj2.kill();
    scoreLiess();
}
function scoreLiess(){
        scoreLies = scoreLies + 3;
        scoreLiesText.text = 'Score : ' + scoreLies;
        totalHits = totalHits +1;
        totalHitsText.text = 'Total Hits : ' + totalHits;
}
function scoreTruths(){
        scoreLies = scoreLies - 1;
        scoreTruth ++;
        scoreTruthText.text = 'oops! : ' + scoreTruth;
        totalHits = totalHits +1;
        totalHitsText.text = 'Total Hits : ' + totalHits;
}

function fireBullet () {

    if (game.time.now > nextFire)
    {
        nextFire = game.time.now + fireRate;
        bullet = bullets.getFirstExists(false);
        if (bullet)
        {
            bullet.reset(throwerSprite.body.x + 16, throwerSprite.body.y + 16);
            bullet.lifespan = 2000;
            bullet.body.velocity.y = -300;
            bullet.body.acceleration.y = -300;
//            bulletTime = game.time.now + 50;
        }
    }
}

function render() {

//    game.debug.body(truth02sprite);
 //   game.debug.body(cowpieSprite);
    game.debug.body(bullets);

}

 

Link to comment
Share on other sites

why are you doing this?

truth02sprite.visible = true;

and btw do this for the velocities if you want to add to the previous one

truth02sprite.body.velocity.x += 100;

do not do this

truth02sprite.body.velocity.x = +100;

 

Link to comment
Share on other sites

I was doing this :

truth02sprite.visible = true;

because this :

    obj1.kill(); 

Kills obj2 instead of obj1 . 

 

Any ways , I changed :

obj1.kill();    to    bullet.kill(); 

And all is working the way it should .

 

function collisionTruth02 (obj1, obj2) {
    bullet.body.velocity.y = -500;
    bullet.kill();
    scoreTruths();
}

Btw: this was a typo .

truth02sprite.body.velocity.x = +100;

Should have been   .x = 100;   or   .x = -100 .

All gone now .

 

Thanks

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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