Jump to content

Problem: "object.body is undefined"


Recommended Posts

Hi, first-time user here,


In my game, I have a vehicle that is made up of multiple objects.  Since we're going to have to rotate or move every piece together, I thought I could make a group for all the objects, call it "Player", then use code like Player.body.velocity.x = 1, etc. in the update function to control it.  However, it keeps telling me Player.body is undefined.


Here is my code:


window.onload = function() {
     var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'prototype', { preload: preload, create: create, update: update });
     function preload () {
           game.load.image('Rover_proto', 'Assets/Art/Rover/Rover_proto.png');
var Player;
     function create () {
        //  We're going to be using physics, so enable the Arcade Physics system
        //The actual rover will be made of separate objects,
        //we put them all in a group so they all act together.
        Player = game.add.group();
        // We will enable physics for any object that is created in this group
      Player.body.enable = true; // ERROR:  Player.body is undefined
        //create the prototype rover in the Player group
        //Rover_proto = Player.create(game.world.centerX, game.world.centerY, 'Rover_proto');
        var Rover_proto = game.add.sprite(game.world.centerX, game.world.centerY, 'Rover_proto');
        Rover_proto.body.enable = true;
     function update(){
     //Moving the rover
     //reset the player's velocity
     Player.body.velocity.x = 0; // ERROR:  Player.body is undefined
So yeah, it keeps telling me Player.body is undefined, even though I say "Player.body.enable = true."  If I use the Rover_proto.body and comment out everything to do with Player, it works fine and tells me that Rover_proto.body is defined.
I want to know if I'm using groups right and how to make this error go away, and still be able to put everything for our Rover under one group.  I've looked at various guides and examples online but they are all contradictory, using totally different commands (like object.body.enable versus object.enableBody, I don't know if there is even a difference).  I checked the documentation but it doesn't have any real examples.
Thanks for the help,
Link to comment
Share on other sites

Yes, as can be seen in the code above, I already tried that.  The problem seems to be that for some reason the game isn't creating a body for the Player group.


I just tried getting rid of the Player group and just having the Rover object by itself, but when I try to do Rover_proto.body.velocity.x it gives me the error  


ReferenceError: Rover_proto is not defined


As compared to the error when I try to use Player.body where it says:


TypeError: Player.body is undefined


So with the Player group, it gives me a TypeError on the line where I say "Player.body.enable = true;" saying that Player.body is undefined.  And when I use Rover_proto object, for some reason it is fine with the line "Rover_proto.body.enable = true;" but gives me a ReferenceError when I try "Rover_proto.body.velocity.x" saying Rover_proto itself is undefined.


I have no idea what's going on but it looks like there are multiple problems.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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