Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Hipe
 Share

Recommended Posts

Hi, Please Help m, I am new in Phaser and I love it!!, can you tell me what is wrong in my code, I don't know why the coalition is right in the ground, but not when I want this with (avion and bullet):

my Code:

GameState.prototype.preload = function() {
    this.game.load.image('bullet', '/assets/gfx/bullet.png');
    this.game.load.image('plane', '/assets/gfx/B17.png');
    this.game.load.image('ground', '/assets/gfx/ground.png');
    this.game.load.spritesheet('explosion', '/assets/gfx/explosion.png', 128, 128);
};
 
// Setup the example
GameState.prototype.create = function() {
    
    //======= fast
    var avion = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'plane');
    //this.avion = game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'plane');
    
    
    avion.inputEnabled = true;
    avion.input.enableDrag(true);
    //this.game.physics.arcade.enable([avion]);
    this.game.physics.enable(avion, Phaser.Physics.ARCADE);
    
    avion.body.gravity.y = -35;
 
    
    //=======
    // Set stage background color
    this.game.stage.backgroundColor = 0x4488cc;
 
    // Define constants
    this.SHOT_DELAY = 300; // milliseconds (10 bullets/3 seconds)
    this.BULLET_SPEED = 800; // pixels/second
    this.NUMBER_OF_BULLETS = 20;
    this.GRAVITY = 30; // pixels/second/second
 
    // Create an object representing our gun
    this.gun = this.game.add.sprite(50, this.game.height - 64, 'bullet');
 
    // Set the pivot point to the center of the gun
    this.gun.anchor.setTo(0.5, 0.5);
 
    // Create an object pool of bullets
    this.bulletPool = this.game.add.group();
    for(var i = 0; i < this.NUMBER_OF_BULLETS; i++) {
        // Create each bullet and add it to the group.
        var bullet = this.game.add.sprite(0, 0, 'bullet');
        this.bulletPool.add(bullet);
 
        // Set its pivot point to the center of the bullet
        bullet.anchor.setTo(0.5, 0.5);
 
        // Enable physics on the bullet
        this.game.physics.enable(bullet, Phaser.Physics.ARCADE);
        
 
        // Set its initial state to "dead".
        bullet.kill();
    }
 
    // Turn on gravity
    game.physics.arcade.gravity.y = this.GRAVITY;
 
    // Create some ground
    this.ground = this.game.add.group();
    for(var x = 0; x < this.game.width; x += 32) {
        // Add the ground blocks, enable physics on each, make them immovable
        var groundBlock = this.game.add.sprite(x, this.game.height - 32, 'ground');
        this.game.physics.enable(groundBlock, Phaser.Physics.ARCADE);
        groundBlock.body.immovable = true;
        groundBlock.body.allowGravity = false;
        this.ground.add(groundBlock);
    }
 
    // Create a group for explosions
    this.explosionGroup = this.game.add.group();
 
    // Simulate a pointer click/tap input at the center of the stage
    // when the example begins running.
    this.game.input.activePointer.x = this.game.width/2;
    this.game.input.activePointer.y = this.game.height/2 - 100;
};
 
GameState.prototype.shootBullet = function() {
    // Enforce a short delay between shots by recording
    // the time that each bullet is shot and testing if
    // the amount of time since the last shot is more than
    // the required delay.
    if (this.lastBulletShotAt === undefined) this.lastBulletShotAt = 0;
    if (this.game.time.now - this.lastBulletShotAt < this.SHOT_DELAY) return;
    this.lastBulletShotAt = this.game.time.now;
 
    // Get a dead bullet from the pool
    var bullet = this.bulletPool.getFirstDead();
 
    // If there aren't any bullets available then don't shoot
    if (bullet === null || bullet === undefined) return;
 
    // Revive the bullet
    // This makes the bullet "alive"
    bullet.revive();
 
    // Bullets should kill themselves when they leave the world.
    // Phaser takes care of this for me by setting this flag
    // but you can do it yourself by killing the bullet if
    // its x,y coordinates are outside of the world.
    bullet.checkWorldBounds = true;
    bullet.outOfBoundsKill = true;
 
    // Set the bullet position to the gun position.
    bullet.reset(this.gun.x, this.gun.y);
    bullet.rotation = this.gun.rotation;
 
    // Shoot it in the right direction
    bullet.body.velocity.x = Math.cos(bullet.rotation) * this.BULLET_SPEED;
    bullet.body.velocity.y = Math.sin(bullet.rotation) * this.BULLET_SPEED;
};
 
// The update() method is called every frame
GameState.prototype.update = function() {
    //=====
     this.game.physics.arcade.collide(this.bulletPool, this.ground, function(bullet, ground) {
        // Create an explosion
        this.getExplosion(bullet.x, bullet.y);
 
        // Kill the bullet
        bullet.kill();
    }, null, this);
    //=====
  
    // Check if bullets have collided with the ground    
    this.game.physics.arcade.collide(this.bulletPool, this.ground, function(bullet, ground) {
        // Create an explosion
        this.getExplosion(bullet.x, bullet.y);
 
        // Kill the bullet
        bullet.kill();
    }, null, this);
    
    
    // Rotate all living bullets to match their trajectory
    this.bulletPool.forEachAlive(function(bullet) {
        bullet.rotation = Math.atan2(bullet.body.velocity.y, bullet.body.velocity.x);
    }, this);
 
    // Aim the gun at the pointer.
    // All this function does is calculate the angle using
    // Math.atan2(yPointer-yGun, xPointer-xGun)
    this.gun.rotation = this.game.physics.arcade.angleToPointer(this.gun);
 
    // Shoot a bullet 
        if (this.game.input.activePointer.isDown) {
            this.shootBullet();
        }
    
};
 
// Try to get a used explosion from the explosionGroup.
// If an explosion isn't available, create a new one and add it to the group.
// Setup new explosions so that they animate and kill themselves when the
// animation is complete.
GameState.prototype.getExplosion = function(x, y) {
    // Get the first dead explosion from the explosionGroup
    var explosion = this.explosionGroup.getFirstDead();
 
    // If there aren't any available, create a new one
    if (explosion === null) {
        explosion = this.game.add.sprite(0, 0, 'explosion');
        explosion.anchor.setTo(0.5, 0.5);
 
        // Add an animation for the explosion that kills the sprite when the
        // animation is complete
        var animation = explosion.animations.add('boom', [0,1,2,3], 60, false);
        animation.killOnComplete = true;
 
        // Add the explosion sprite to the group
        this.explosionGroup.add(explosion);
    }
 
    // Revive the explosion (set it's alive property to true)
    // You can also define a onRevived event handler in your explosion objects
    // to do stuff when they are revived.
    explosion.revive();
 
    // Move the explosion to the given coordinates
    explosion.x = x;
    explosion.y = y;
 
    // Set rotation of the explosion at random for a little variety
    explosion.angle = this.game.rnd.integerInRange(0, 360);
 
    // Play the animation
    explosion.animations.play('boom');
 
    // Return the explosion itself in case we want to do anything else with it
    return explosion;
};
 
function onDragStart(sprite, pointer) {
 
    this.shootBullet();
 
}
 
 
 
var game = new Phaser.Game(250, 300, Phaser.AUTO, 'game');
game.state.add('game', GameState, true);
Link to comment
Share on other sites

SAorry this part was not correct :

//=====
     this.game.physics.arcade.collide(this.bulletPool, this.avion, function(bullet, avion) {
        // Create an explosion
        this.getExplosion(bullet.x, bullet.y);
 
        // Kill the bullet
        bullet.kill();
    }, null, this);
    //=====
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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