Jump to content

this.createWorld(); inside a state is undefined.


DWboutin
 Share

Recommended Posts

Hi,

 

I do the awesome book of Tomas Palef and i got an error. On my main state, after creating all my walls, i have the function "this.createWorld()" who is undefined.

 

Why is it undefined? What does it do exactly? Is it mandatory?

 

Thank you

 

var mainState = {  preload: function(){    // charger une image    game.load.image('player', 'assets/player.png');    game.load.image('wallV', 'assets/wallVertical.png');    game.load.image('wallH', 'assets/wallHorizontal.png');    game.load.image('coin', 'assets/coin.png');  },  create: function(){    // Mettre le background d'une couleur    game.stage.backgroundColor = '#3498db';    // Initialiser le moteur de physique, on peut avoir P2 (Plus complexe), Ninja et Arcade    game.physics.startSystem( Phaser.Physics.ARCADE );    // Création    // Créer le personnage    this.player = game.add.sprite( game.world.centerX, game.world.centerY, 'player' );    // Setter le "anchor point" du personnage au centre de celui-ci. Par défaut en haut à gauche    this.player.anchor.setTo( 0.5, 0.5 );    // Gravité    // dire au moteur de physique de prendre le player dans sa physique    game.physics.arcade.enable( this.player );    // ajouter la gravité en Y au player    this.player.body.gravity.y = 500;    // Controller le player    this.cursor = game.input.keyboard.createCursorKeys();    // Groupe    // Création d'un groupe qui englobe les murs    this.walls = game.add.group();    // Mettre la physics    this.walls.enableBody = true;    // Créer les 2 murs dans le groupe    game.add.sprite(0, 0, 'wallV', 0, this.walls);    game.add.sprite(480, 0, 'wallV', 0, this.walls);    game.add.sprite(0, 0, 'wallH', 0, this.walls); // Top left    game.add.sprite(300, 0, 'wallH', 0, this.walls); // Top right    game.add.sprite(0, 320, 'wallH', 0, this.walls); // Bottom left    game.add.sprite(300, 320, 'wallH', 0, this.walls); // Bottom right    game.add.sprite(-100, 160, 'wallH', 0, this.walls); // Middle left    game.add.sprite(400, 160, 'wallH', 0, this.walls); // Middle right    var middleTop = game.add.sprite(100, 80, 'wallH', 0, this.walls);    middleTop.scale.setTo(1.5, 1);    var middleBottom = game.add.sprite(100, 240, 'wallH', 0, this.walls);    middleBottom.scale.setTo(1.5, 1);    // Rendre tout les murs imbougable    this.walls.setAll('body.immovable', true);    this.createWorld();    // la cenne    this.coin = game.add.sprite(60, 40, 'coin');    game.physics.arcade.enable(this.coin);    this.coin.anchor.setTo(0.5, 0.5);    // score    this.scoreLabel = game.add.text(30, 30, 'score: 0', { font: '18px Arial', fill: '#ffffff'});    this.score = 0;      },  update: function(){    // Détecter la collision, toujours mettre en premier    game.physics.arcade.collide(this.player, this.walls);    game.physics.arcade.overlap(this.player, this.coin, this.takeCoin, null, this);    this.movePlayer();    if (!this.player.inWorld) {      this.playerDie();    }  },  movePlayer: function(){    if(this.cursor.left.isDown){      this.player.body.velocity.x = -200;    }else if(this.cursor.right.isDown){      this.player.body.velocity.x = 200;    }else{      this.player.body.velocity.x = 0;    }    if (this.cursor.up.isDown && this.player.body.touching.down) {      this.player.body.velocity.y = -320;    }  },  playerDie: function(){    game.state.start('main');  },  takeCoin: function(player, coin){    // Détruire le coin    this.coin.kill();    this.score += 5;    this.scoreLabel.text = 'score: ' + this.score;  }};var game = new Phaser.Game(500, 340, Phaser.AUTO, 'gameDiv');game.state.add('main', mainState);game.state.start('main');

 

Link to comment
Share on other sites

It means what it says. It's undefined because you have not defined it. Either define a function in the mainState called createWorld, or remove that line of code. It does nothing because it has not been defined. It is not mandatory, it is not a Phaser function.

My guess is the book had a function in there called createWorld but they removed it and forgot to remove the call to it on that line of code.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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