Jump to content

Sprite collision


FedeCuci
 Share

Recommended Posts

I am currently making a game that you can check out at http://spalaxys.online and I would like to add little asteroids in a fixed random location in which the player cannot go through. I have looked at the phaser examples as well as watched some videos, but I cannot get it working. What is the best way to go about this? I have linked my files so you can check them out. I would really appreciate any help.

This is the code that I am using: (create.js)

function create() {
    game.physics.startSystem(Phaser.Physics.ARCADE);



    game.renderer.clearBeforeRender = false;
    game.renderer.roundPixels = true;

    game.add.tileSprite(0, 0, 30000, 30000, 'space');
    //game.add.tileSprite(0, 0, 1920, 1920, 'background');

    game.world.setBounds(0, 0, 30000, 30000);


    //Possible weapons
    Weapons = {
        sniperWeapon: new Weapon("Sniper weapon", 0, 1000, 0, null, null, false, 5000, 90, game.add.weapon(5, 'long-bullet')),
        uziWeapon: new Weapon("Uzi", 20, 50, 0, null, null, false, 1500, 90, game.add.weapon(500, 'bullet')),
        AK47LikeWeapon: new Weapon("AK47-like", 5, 100, 0, null, null, false, 1000, 90, game.add.weapon(100, 'round-bullet')),
        
    };

    ThrustEmitters = {
        basicEmitter: new ThrustEmitter(game.add.emitter(game.world.centerX, game.world.centerY, 400), ['fire1', 'fire2', 'fire3'], 0.3),
        ourEmitter: new ThrustEmitter(game.add.emitter(game.world.centerX, game.world.centerY, 1000), ['fire01', 'fire02', 'fire03'], 0.5),
    };

    // Creating a new object of class Player
    player = new Player("keyboardMouse", game.world.centerX, game.world.centerY, 200, 200, "12341",
        game.add.sprite(game.world.randomX, game.world.randomY, 'ship'), //Sprite handler object
        Weapons.sniperWeapon.weapon, // Weapon handler object
        ThrustEmitters.ourEmitter.emitter // Thrust emmiter handler object
    );

    game.physics.enable(player.sprite, Phaser.Physics.ARCADE);
    player.spawn();
    game.camera.follow(player.sprite);
    player.sprite.maxVelocity = player.maxVelocity;

    //  Game input
    cursors = game.input.keyboard.createCursorKeys();
    var style = {
        font: "bold 32px Arial",
        fill: "#fff",
        boundsAlignH: "center",
        boundsAlignV: "middle"
    };
    var text = game.add.text(game.world.centerX - 500, game.world.centerY - 300, 
        "Move with W,S and mouse. \n Switch to touch/click movement with TAB. \nToggle debug with SPACE. \nShoot with LMB. \
        \nSwitch weapons with 1, 2, 3.", style);
    text.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);

    //Make resources group
    //var resources = game.add.group();

    //Run the show_resources function located in main.js
    for (var i = 0; i < 250; i++){

    var asteroid_brown = game.add.sprite(game.world.randomX, game.world.randomY, 'asteroid');
    var rock = game.add.sprite(game.world.randomX, game.world.randomY, 'rock');

    }

    game.physics.enable(asteroid_brown, Phaser.Physics.ARCADE);
    game.physics.enable(rock, Phaser.Physics.ARCADE);

    //  We'll set the bounds to be from x0, y100 and be 800px wide by 100px high
    text.setTextBounds(0, 100, 800, 100);

    // this.spaceButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);
    // this.WButton = this.input.keyboard.addKey(Phaser.KeyCode.W);
    // this.SButton = this.input.keyboard.addKey(Phaser.KeyCode.S);
    // this.AButton = this.input.keyboard.addKey(Phaser.KeyCode.A);
    // this.DButton = this.input.keyboard.addKey(Phaser.KeyCode.D);
    // this.oneButton = this.input.keyboard.addKey(Phaser.KeyCode.ONE);
    // this.twoButton = this.input.keyboard.addKey(Phaser.KeyCode.TWO);

    Keys = {
        space: this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR),
        w: this.input.keyboard.addKey(Phaser.KeyCode.W),
        s: this.input.keyboard.addKey(Phaser.KeyCode.S),
        one: this.input.keyboard.addKey(Phaser.KeyCode.ONE),
        two: this.input.keyboard.addKey(Phaser.KeyCode.TWO),
        three: this.input.keyboard.addKey(Phaser.KeyCode.THREE),
        four: this.input.keyboard.addKey(Phaser.KeyCode.FOUR),
        five: this.input.keyboard.addKey(Phaser.KeyCode.FIVE),
        six: this.input.keyboard.addKey(Phaser.KeyCode.SIX),
        seven: this.input.keyboard.addKey(Phaser.KeyCode.SEVEN),
        tab: this.input.keyboard.addKey(Phaser.KeyCode.TAB),
    }

    // WEAPON CHANGING

    Keys.one.onDown.add(function(){player.setWeapon(Weapons.sniperWeapon.weapon)}, this);
    Keys.two.onDown.add(function(){player.setWeapon(Weapons.uziWeapon.weapon)}, this);
    Keys.three.onDown.add(function(){player.setWeapon(Weapons.AK47LikeWeapon.weapon)}, this);


    // OTHER KEYS
    Keys.space.onDown.add(function () {
        showDebug = true;
    }, this);
    Keys.tab.onDown.add(function () {
        if (player.movementType == "keyboardMouse") {
            console.log("touchMouse")
            player.movementType = "touchMouse"
        } else {
            console.log("keyboardMouse")
            player.movementType = "keyboardMouse"
        }
    }, this);

    game.input.keyboard.addKeyCapture([
        Phaser.Keyboard.W,
        Phaser.Keyboard.A,
        Phaser.Keyboard.S,
        Phaser.Keyboard.D,
        Phaser.Keyboard.SPACEBAR,
        Phaser.Keyboard.ONE,
        Phaser.Keyboard.TWO,
        Phaser.Keyboard.THREE,
        Phaser.Keyboard.FOUR,
        Phaser.Keyboard.FIVE,
        Phaser.Keyboard.TAB,
    ]);
}

update.js:

function update() {

    

    player.sprite.rotation = game.physics.arcade.angleToPointer(player.sprite);
    
    
    if (player.movementType == "keyboardMouse") {



        if (Keys.w.isDown) {
            game.physics.arcade.accelerationFromRotation(player.sprite.rotation, player.positiveAcceleration, player.sprite.body.acceleration);
            // this.isEmmiter=true;
            this.txtW2 = "Is W still down? YES";
            if (!this.isThrustEmitter) {
                player.emitter.start(false, 3000, 5);
                this.isThrustEmitter = true;
            }

        } else if (Keys.s.isDown) {
            game.physics.arcade.accelerationFromRotation(player.sprite.rotation, (-1) * player.negativeAcceleration, player.sprite.body.acceleration);
            this.txtS2 = "S still down? YES"
            this.isThrustEmitter = false;
        } else {
            player.sprite.body.acceleration.set(0);
            this.txtW2 = "Is W still down? NO";
            this.txtS2 = "S still down? YES"
            this.isThrustEmitter = false;
        
        

        }
        
    }



    // if (this.WButton.downDuration(250))
    // {
    // 	this.txtW = "W was pressed 250 ms ago? YES";
    // } 
    // else
    // {
    // 	this.txtW = "W was pressed 250 ms ago? NO";
    // }
    // if (this.SButton.downDuration(250))
    // {
    // 	this.txtS = "S was pressed 250 ms ago? YES";
    // } 
    // else
    // {
    // 	this.txtS = "S was pressed 250 ms ago? NO";
    // }

    if (game.input.activePointer.isDown) {
        player.weapon.fire();
        if (player.movementType == "touchMouse") {
            game.physics.arcade.accelerationFromRotation(player.sprite.rotation, player.positiveAcceleration, 
                player.sprite.body.acceleration);
            if (!this.isThrustEmitter) {
                player.emitter.start(false, 3000, 5);
                this.isThrustEmitter = true;
            }
            
        }
    }
    else {
        if (player.movementType == "touchMouse") {
            this.isThrustEmitter = false;  
            player.sprite.body.acceleration.set(0);
        }
    }

    //THRUST EMITTER
    if(this.isThrustEmitter) {
        //player.emitter.on = true;
    }
    else {
        player.emitter.on = false;
    }
    // We have to calculate the vector for thrust emitter
    var px = Math.cos(player.sprite.rotation) * 1000;
    var py = Math.sin(player.sprite.rotation) * 1000;

    px *= -1; //So it goes from the 'back' of the ship
    py *= -1; //So it goes from the 'back' of the ship

    player.emitter.minParticleSpeed.set(px / 2, py / 2);
    player.emitter.maxParticleSpeed.set(px, py);

    player.emitter.emitX = player.sprite.x;
    player.emitter.emitY = player.sprite.y;

    

    // bullets.forEachExists(screenWrap, this);

}

I would like the player ('ship' sprite) to not go through the asteroids ('asteroid_brown' and 'rock' sprites) but whenever I try to add physics to them or try to change them the game stops working.

Thanks in advance, Fede

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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