scope2229 Posted June 19, 2018 Share Posted June 19, 2018 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 More sharing options...
brentstrandy Posted June 19, 2018 Share Posted June 19, 2018 You set your player's position to 0,0 - this means the bottom right corner of your player is set to 0,0. Try adding .setOrigin(0) like this: player = this.physics.add.sprite(32, config.height - 150, "ship").setOrigin(0); This will make the top left corner of your player set to 0, 0 scope2229 1 Link to comment Share on other sites More sharing options...
Recommended Posts