Jump to content

collide using array


Recommended Posts

(posted in phaser 3 instead of the phaser forum by mistake)

Hi

I'm hoping someone can hep me, I'm trying to create basic map using an array with 2 different images, one is a wall, the other is a floor. I've created twp groups but when i use

this.game.physics.arcade.collide(this.player, this.blocks, null, this.hitBlock, this);

I've used the collide function before on other games with no issues - this is my first time using a 2d array but I can't see where the issue is happening

code below :

cursor controls to move

link example :http://html5gamer.mobi/projects/6/

var GameHipster = GameHipster || {};

GameHipster.GameState = {

  init : function() {
    //keyboard controls
    this.cursors = this.game.input.keyboard.createCursorKeys();
    this.upKey = this.game.input.keyboard.addKey(Phaser.Keyboard.W);
    this.downKey = this.game.input.keyboard.addKey(Phaser.Keyboard.S);
    this.leftKey = this.game.input.keyboard.addKey(Phaser.Keyboard.A);
    this.rightKey = this.game.input.keyboard.addKey(Phaser.Keyboard.D);
    this.actionKey = this.game.input.keyboard.addKey(Phaser.Keyboard.P);

    this.PLAYER_SPEED = 400;
    this.BULLET_SPEED = 500;

    this.score = 0;
  }, // end init

  preload : function() {

  },//end preload

  //executed after everything is loaded
  create: function() {

    blockdata = [
      [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
      [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    ];
    this.blocks = this.add.group();
    this.blocks.enableBody = true;
    this.floors = this.add.group();
    this.floors.enableBody = true;

    var floor, block, i, j, col;
    var ROWS = blockdata.length;
    var COLS = blockdata[0].length;

    //console.log('R : ' + ROWS + ' C : ' + COLS);
    //console.log(blockdata[2][3]);

    for (i =0; i < COLS; i++) {
          for (j =0; j < ROWS; j++){
            //col =1;
            col = (blockdata[j]);

            if (col ==0){
              floor = this.floors.create( 1 + (i*64),1 +(j*64), 'block' + col);
              floor.anchor.setTo(0.5);
            }
            if (col ==1){
              block = this.blocks.create( 1 + (i*64),1 +(j*64), 'block' + col);
              block.anchor.setTo(0.5);
              block.body.immovable =true;
            }

          }

    };

    this.player = this.add.sprite(this.game.world.width / 2, this.game.world.height / 2, 'player');
    this.player.anchor.setTo(0.5);
    this.player.scale.setTo(0.2);
    this.game.physics.arcade.enable(this.player);
    this.player.body.immovable = true;

 

    //ui
    this.uiBlocked = false;

    //text
    var style = {font : '20pt Arial', fill : '#fff'};
    this.scoreLabel = this.add.text(10,10, 'SCORE : ', style);
    this.scoreText = this.add.text(130,10, '0', style);
    this.refreshStats();
  },
  //executed multiple times per second

  update: function() {

    this.player.body.velocity.x = 0;
    this.player.body.velocity.y = 0;

    this.game.physics.arcade.collide(this.player, this.blocks, null, this.hitBlock, this);
    this.keyboardControls();

 

  }, // end update

  keyboardControls : function(){
    //init keyboard controls
    if (this.cursors.left.isDown|| this.leftKey.isDown){
      //this.player.angle += 5;
      this.player.body.velocity.x = -this.PLAYER_SPEED;

    } else
    if (this.cursors.right.isDown || this.rightKey.isDown){
       //this.player.angle -= 5;
       this.player.body.velocity.x = this.PLAYER_SPEED;
    }

    if (this.cursors.up.isDown || this.upKey.isDown){
      //thrust
      this.player.body.velocity.y = -this.PLAYER_SPEED;
    }

    if (this.cursors.down.isDown || this.downKey.isDown){
      //thrust
      this.player.body.velocity.y = this.PLAYER_SPEED;
    }

    if (this.actionKey.isDown){
      this.fireBullet();
    }

  },// end function

  hitBlock : function(player, block){
    console.log('hit');
  },

  endGame : function(player, enemy){
    this.game.state.start('GameState');
  },

  refreshStats : function(){
    this.scoreText.text = this.score;
  }

 

};

 

Any help is appreciated

Eric

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...