Andrew972000 Posted March 4, 2018 Share Posted March 4, 2018 Hello I have a problem with my code. I get the error Uncaught ReferenceError: game is not defined at lvl1.preload (game2.js:16). How can I fix this? Thanks for all the responses! var blobbie; var platforms; function init(){ var game = new Phaser.Game(1900, 900, Phaser.CANVAS, '', null,false,false); game.state.add("lvl1",lvl1); game.state.start("lvl1"); } var lvl1 = function(){ } lvl1.prototype = { init: function(){ }, preload: function() { game.load.image('Lvl1', 'assets/Lvl1.png'); game.load.image('blobbie', 'assets/Blobbie02.png'); game.load.image('l1_ebene1', 'assets/lvl1_teile/Ebene1.png'); game.load.image('l1_ebene2', 'assets/lvl1_teile/Ebene2.png'); game.load.image('l1_ebene3', 'assets/lvl1_teile/Ebene3.png'); game.load.image('l1_ebene4', 'assets/lvl1_teile/Ebene4.png'); game.load.image('l1_plattform1', 'assets/lvl1_teile/Plattform1.png'); game.load.image('l1_plattform2', 'assets/lvl1_teile/Plattform2.png'); game.load.image('l1_plattform3', 'assets/lvl1_teile/Plattform3.png'); game.load.image('l1_stumpf1', 'assets/lvl1_teile/Stumpf1.png'); game.load.image('l1_stumpf2', 'assets/lvl1_teile/Stumpf2.png'); }, create: function() { game.physics.startSystem(Phaser.Physics.ARCADE); //Add Objekt game.add.sprite(0, 0, 'Lvl1'); blobbie=game.add.sprite(300, 870, 'blobbie'); blobbie.anchor.setTo (1, 1); //Boden platforms = game.add.group(); platforms.enableBody = true; var ground = platforms.create(0, game.world.height+20, 'l1_ebene1'); ground.body.immovable = true; ground = platforms.create(1025, game.world.height+20, 'l1_ebene2'); ground.body.immovable = true; ground = platforms.create(3060, game.world.height+20, 'l1_ebene3'); ground.body.immovable = true; ground = platforms.create(3421, game.world.height+20, 'l1_ebene4'); ground.body.immovable = true; //Plattformen var platt = platforms.create(440, 697, 'l1_plattform1'); platt.body.immovable = true; platt = platforms.create(1705, 590, 'l1_plattform2'); platt.body.immovable = true; platt = platforms.create(2781, 685, 'l1_plattform3'); platt.body.immovable = true; platt = platforms.create(1230, game.world.height-194, 'l1_stumpf1'); platt.body.immovable = true; platt = platforms.create(2265, game.world.height-194, 'l1_stumpf2'); platt.body.immovable = true; //Kamera game.camera.follow(blobbie); //Cursor cursors = game.input.keyboard.createCursorKeys(); //Game Physik game.physics.arcade.enable(blobbie); game.world.setBounds(0, 0, 3840, 1080); blobbie.body.bounce.y = 0.2; blobbie.body.gravity.y = 400; blobbie.body.collideWorldBounds = true; }, update: function() { game.physics.arcade.collide(blobbie, platforms); if (cursors.left.isDown) { blobbie.x -= 8; } else if (cursors.right.isDown) { blobbie.x += 8; } if (cursors.up.isDown) { blobbie.y -= 6; } } } Link to comment Share on other sites More sharing options...
head Posted March 5, 2018 Share Posted March 5, 2018 place "var game" outside the init function, remove "var" from init function. Link to comment Share on other sites More sharing options...
Recommended Posts