cosmic jet lag Posted October 1, 2016 Share Posted October 1, 2016 Hey, so I'm working on a project in phaser and, no matter what I change, I'm suddenly getting this error: phaser.js:80880 Uncaught TypeError: Cannot read property 'body' of undefined but I don't know where it came from. It looks like a syntax problem with my code or something, because it only appeared as soon as I changed an object to a group, so now I can't test anything, which is a bummer. Can someone help me? I've got this project due soon and I can't, for the life of me, figure out what's wrong. Here's my whole code. Thank you D: var game = new Phaser.Game(1000, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render }); // global variables - these can be accessed by any function since they're declared outside all functions var DEBUG = true; var background; //handies var hand1; var hand2; //keyboard var wKey; var aKey; var sKey; var dKey; var upKey; var downKey; var leftKey; var rightKey; var shiftKey; var enterKey; //objects var objectGroup; var object; var hat; function preload() { // Enlarge the game to fill the screen available game.scale.setShowAll(); // Center the screen HORIZONTALLY and VERTICALLY on the web page game.scale.pageAlignHorizontally = true; game.scale.pageAlignVertically = true; // Refresh the game so it will apply the positioning indicated above (ie, scaled larger and centered) game.scale.refresh(); game.renderer.renderSession.roundPixels = true; //this forces the camera to always be at whole-pixel coordinates, which looks nicer //images game.load.image('player', 'assets/image/player.png'); game.load.image('background', 'assets/image/backgroundTest.png'); game.load.image('hand1', 'assets/image/hand2.png'); game.load.image('hand2', 'assets/image/hand1.png'); game.load.image('test', 'assets/image/placeholder.png'); game.load.image('hat', 'assets/image/hat.png'); } function create() { //set the player's score to zero score = 0; //we use this function to enable input from the cursor keys cursors = game.input.keyboard.createCursorKeys(); //bounds...might not be big enough?? game.world.setBounds(0, 0, 2000, 2000); //background background = game.add.tileSprite(0, 0, 1900, 1900, 'background'); //left hand hand1 = game.add.sprite(game.width*0.5 - 500, game.height*0.5, 'hand1'); hand1.anchor.set(0.5); game.physics.arcade.enable(hand1); hand1.body.height = 100; hand1.events.onInputDown.add(clickyClicky); game.camera.follow(hand1, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1); //right hand hand2 = game.add.sprite(game.width*0.5 + 100, game.height*0.5, 'hand2'); hand2.anchor.set(0.5); game.physics.arcade.enable(hand2); hand2.body.height = 100; hand2.events.onInputDown.add(clickyClicky); game.camera.follow(hand2, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1); //object group objectGroup = game.add.group(); for (i = 0; i < 16; i++){ //createObject(); objectGroup.create(Math.random(), Math.random(), 'hat'); } //do the new keys mapKeyInputs(); } function update() { //hand1 (left) movement if (wKey.isDown){ hand1.body.velocity.y = -150; } else if (sKey.isDown) { hand1.body.velocity.y = 150; } else if (aKey.isDown){ hand1.body.velocity.x = -150; } else if (dKey.isDown){ hand1.body.velocity.x = 150; } else { stopMoving(hand1); } //hand2 (right) movement...for whatever reason it can't go L/R but I'm sure I can work with that if (upKey.isDown){ hand2.body.velocity.y = -150; } else if (downKey.isDown){ hand2.body.velocity.y = 150; } else if (leftKey.isDown){ hand2.body.velocity.x = -150; } else if(rightKey.isDown){ hand2.body.velocity.x = 150; } else { stopMoving(hand2); } //keep the handies near each other if ((hand1.body.x - hand2.body.x) < 200){ hand2.body.x = (hand1.body.x + 500); console.log(hand1.body.x + "hand1"); } else if ((hand2.body.x - hand1.body.x) > 500){ hand2.body.x = (hand1.body.x + 500); } if (checkOverlap(hand1, objectGroup)){ console.log("THERE'S AN OVERLAP! YIPPEE"); objectGroup.tint = 0x0066cc; } else { console.log("this shit isn't working"); } hand1.bringToTop(); hand2.bringToTop(); } // NOTE TO SELF: GET TO THIS SOON! BUT LATER // function touchTheThing(thingThatTouches, touchedThing){ // if (touchTheThing == true){ // touchedThing.tint = (color); // sound.play(); // } // } //what started as a clicky function is now just trying to transfer velocity. function clickyClicky(theSprite, theOtherSprite){ stopMoving(theSprite); game.camera.follow(theOtherSprite, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1); } function stopMoving(someHand){ someHand.body.velocity.x = 0; someHand.body.velocity.y = 0; } function checkOverlap(spriteA, spriteB){ var boundsA = spriteA.getBounds(); var boundsB = spriteB.getBounds(); return Phaser.Rectangle.intersects(boundsA, boundsB); } function mapKeyInputs(){ wKey = mapKey(Phaser.Keyboard.W); aKey = mapKey(Phaser.Keyboard.A); sKey = mapKey(Phaser.Keyboard.S); dKey = mapKey(Phaser.Keyboard.D); upKey = mapKey(Phaser.Keyboard.UP); downKey = mapKey(Phaser.Keyboard.DOWN); leftKey = mapKey(Phaser.Keyboard.LEFT); rightKey = mapKey(Phaser.Keyboard.RIGHT); shiftKey = mapKey(Phaser.Keyboard.SHIFT); enterKey = mapKey(Phaser.Keyboard.ENTER); } function mapKey(keyCode){ return game.input.keyboard.addKey(keyCode); } // function createObject(){ // objectGroup.create(Math.random(), Math.random(), 'hat'); // } function render(){ // this is a useful way to draw placeholder text in Phaser, but don't use it for a finished game! //game.debug.text("Score: " + score , 32, 32); // for debugging purposes, we might want to show the collision boxes for the bodies in the game // this code will only run if the variable DEBUG (set at the top) is true if (DEBUG == true){ //show debug bodies game.debug.body(hand1); game.debug.body(hand2); game.debug.body(hat); } } Link to comment Share on other sites More sharing options...
cosmic jet lag Posted October 1, 2016 Author Share Posted October 1, 2016 Nevermind -- I actually got that problem fixed. It was in debugging the hat. Link to comment Share on other sites More sharing options...
Recommended Posts