Jump to content

Cant get player to display


scope2229
 Share

Recommended Posts

I've been using this post Player.js

To try and separate my code but i'm failing to understand really how it works. 

 


var Player = new Phaser.Class({
  Extends: Phaser.GameObjects.Image,
  initialize:
  function Player(scene, x, y){
    Phaser.GameObjects.Image.call(this, scene);
    this.speed = 160;
  },
  create: function(){
    player = this.physics.add.sprite(32, config.height - 150, "ship");
    player.setCollideWorldBounds(true);
    cursors = this.input.keyboard.createCursorKeys();
  },
  update: function(){
    player.setVelocity(0,0);
    if (cursors.left.isDown){
      player.setVelocityX(-this.speed);
    }
    else if (cursors.right.isDown){
      player.setVelocityX(160);
    }
    else{
      player.setVelocityX(0);
    }
    if (cursors.up.isDown){
      player.setVelocityY(-150);
    }
    else if (cursors.down.isDown){
      player.setVelocityY(+150);
    }
  }
});

then i call the player in lvl1.js

var lvl1State = new Phaser.Class({
  Extends: Phaser.Scene,
  initialize:
  function lvl1(){
    Phaser.Scene.call(this, {key:"lvl1"});
  },
  create: function(){
    this.starfield = this.add.tileSprite(400,300,800,4000, 'starfield');
    this.juststars = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula1 = this.add.tileSprite(400,300,800,4000, 'nebula1');
    this.stars2 = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula2 = this.add.tileSprite(400,300,2000,4000, 'nebula2');

    this.createStarfield();
    //add player
    this.player = new Player(this.game, 0, 0);
    this.game.add.existing(this.player);

  },//end of create
  update: function(){
    this.starfieldEffects();


  },//end of update

  //-------GLOBAL FUNCTIONS-----//

  createStarfield: function(){
    this.starfield = this.add.tileSprite(400,300,800,4000, 'starfield');
    this.juststars = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula1 = this.add.tileSprite(400,300,800,4000, 'nebula1');
    this.stars2 = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula2 = this.add.tileSprite(400,300,2000,4000, 'nebula2');
  },
  starfieldEffects: function(){
    this.starfield.y += 0.5;
    this.juststars.y += 0.2;
    this.stars2.y += 3.8;
    this.nebula1.y += 0.8;
    this.nebula2.y += 0.3;
    this.nebula2.x -= 0.1;
  }
});//end of var

//ADD GAME TO SCENES
Space_Game.scenes.push(lvl1State);

but nothing on the page shows up. I managed to get rid of all the errors at first but then still nothing happened. Im at a loss as to how i use separate classes.

i have 10 lvls so far and the player with the bullets is repeated over and over. id rather remove and learn how to separate than to just continue on how i have been with rewriting or copy paste all the repeated code then add new code for the level.

Link to comment
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...
 Share

  • Recently Browsing   0 members

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