0
votes

I am currently making a small platformer game, where the player jumps up infinitely, much like the android game "Abduction".
I have managed to create platforms in random locations on a timed interval and adding it to an array.
However i can only get the player sprite to collide with 1 of the platform arrays, which is this.walls array that i have made so i have a set starting place for the player.
Does anyone know what the issue might be. Ugly code alert by the way, i am quite new at this.

I have checked update: function to see if i had forgot to add some stuff there. ive spellchecked the code and all sorts of things but i am at a completel loss.

var playState = {
preload: function () {

},

create: function () {
    game.add.image (0, 0,'bg');
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.renderer.renderSession.roundPixels = true;

    this.cursor = game.input.keyboard.createCursorKeys();

    game.input.keyboard.addKeyCapture(
        [Phaser.Keyboard.UP, Phaser.Keyboard.DOWN, Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT]);

    this.wasd = {
        up: game.input.keyboard.addKey(Phaser.Keyboard.W), left: game.input.keyboard.addKey(Phaser.Keyboard.A), right: game.input.keyboard.addKey(Phaser.Keyboard.D)
    };


    this.player = game.add.sprite(game.width/2, game.height/1.2, 'player'); 
    this.player.anchor.setTo(0.5, 0.5);
    game.physics.arcade.enable(this.player);
    this.player.body.gravity.y = 800;

   this.scoreLabel = game.add.text(30, 30, 'score: 0', 
        { font: '18px Arial', fill: '#ffffff' });
    game.global.score = 0;

    game.time.events.loop(1000, this.updateScore);
    game.time.events.loop(4000, this.addPlatform);

    this.createWorld();



},


 update: function () {
    game.physics.arcade.collide(this.player, this.walls);
    game.physics.arcade.collide(this.player, this.platforms);



    if (!this.player.inWorld) {
        this.playerDie();
    }

     this.movePlayer();


},
    updateScore: function(){
        this.score +=1;
        game.global.score +=1;
        //fix this at some point 

    },


createWorld: function() {
    this.walls = [];

    this.walls.push(game.add.sprite(170, 750, 'platform', 0));
    this.walls.push(game.add.sprite(70, 650, 'platform', 0));
    this.walls.push(game.add.sprite(320, 550, 'platform', 0));
    this.walls.push(game.add.sprite(200, 450, 'platform', 0));
    this.walls.push(game.add.sprite(10, 550, 'platform', 0));
    this.walls.push(game.add.sprite(70, 350, 'platform', 0));
    this.walls.push(game.add.sprite(310, 300, 'platform', 0));
    this.walls.push(game.add.sprite(230, 200, 'platform', 0));
    this.walls.push(game.add.sprite(100, 100, 'platform', 0));
    this.walls.push(game.add.sprite(270, 0, 'platform', 0));




    for (var x=0; x< this.walls.length; x++) {
        game.physics.arcade.enable(this.walls[x]);
        this.walls[x].enableBody = true;
        this.walls[x].body.immovable = true;
        this.walls[x].body.velocity.y = 20;

        } 

},
addPlatform: function() {
    this.platforms = [];

    this.platforms.push(game.add.sprite(game.rnd.integerInRange(0, 350),-20,'platform',0));


    for (var x=0; x< this.platforms.length; x++) {
        game.physics.arcade.enable(this.platforms[x]);
        this.platforms[x].enableBody = true;
        this.platforms[x].body.immovable = true;
        this.platforms[x].body.velocity.y = 20;

    }
},




movePlayer: function() {
    if (this.cursor.left.isDown || this.wasd.left.isDown) {
        this.player.body.velocity.x = -250;
    }
    else if (this.cursor.right.isDown || this.wasd.right.isDown) {
        this.player.body.velocity.x = 250;
    }
    else { 
        this.player.body.velocity.x = 0;
    }

    if ((this.cursor.up.isDown || this.wasd.up.isDown)
        && this.player.body.touching.down){
        this.player.body.velocity.y = -450;
    }
},
playerDie: function() {
    this.player.kill();

    //this.deadSound.play();
    //this.emitter.x = this.player.x;
    //this.emitter.y = this.player.y;
    //this.emitter.start(true, 800, null, 15);
    game.time.events.add(1000, this.startMenu, this);
    game.camera.shake(0.02, 300);
},

startMenu: function() {
    game.state.start('menu');
},



};

this.walls is so i have a starting ground of set platforms and this.platforms is the randomly generated platforms.

I want to be able to actually jump on those platforms and not just fall through, which i do at the moment

1

1 Answers

0
votes

Your platform is an array of sprites, so you have to parse through each sprite inside your array.

Or, you can make a "Group" object, instead of plain array. You can make a Group of each plaform, then do collision with this Group and player.