NumberOneRobot Posted March 24, 2015 Share Posted March 24, 2015 I am trying to test some body data for a game I'm making by modifying the Body Debug example at http://phaser.io/examples/v2/p2-physics/body-debug. I'm getting a TypeError: Undefined is not a function message when I try to run the script, however, and it is happening at the call to game.load.physics. I am using the exact code from the example besides changing the filenames to match my filesystem, and the game.load.image calls work fine, just no body data can be loaded. I just updated my phaser version from GitHub, so it shouldn't be out of date, but I'm not sure if there's some other issue causing this. I doubt it'd be a bug in Phaser. Do you think there is something wrong with the download, or is there something else that'd be causing the problem? The full code for the script and error message stack trace are below (I removed the HTML components, but they were unchanged from the downloadable code for the example: Uncaught TypeError: undefined is not a function (index):23preload (index):23d.StateManager.start phaser.min.js:4d.StateManager.add phaser.min.js:4d.StateManager.boot phaser.min.js:4 d.Game.boot phaser.min.js:5d.Game._onBoot phaser.min.js:5 window.onload = function() { var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('contra2', 'contra2.png'); game.load.image('bunny', 'bunny.png'); game.load.image('block', 'block.png'); game.load.image('wizball', 'wizball.png'); console.log(game.load.physics); game.load.physics('physicsData', 'sprites.json'); } var contra; var bunny; var block; var wizball; var result = 'Click a body'; function create() { // Enable p2 physics game.physics.startSystem(Phaser.Physics.P2JS); contra = game.add.sprite(100, 200, 'contra2'); bunny = game.add.sprite(550, 200, 'bunny'); block = game.add.sprite(300, 400, 'block'); wizball = game.add.sprite(500, 500, 'wizball'); game.physics.p2.enable([ contra, bunny ], true); game.physics.p2.enable([ block, wizball ], true); // Convex polys contra.body.clearShapes(); contra.body.loadPolygon('physicsData', 'contra2'); bunny.body.clearShapes(); bunny.body.loadPolygon('physicsData', 'bunny'); // Circle wizball.body.setCircle(45); game.input.onDown.add(click, this); console.log(contra.body.debug); console.log(block.body.debug); } function click(pointer) { // You can hitTest against an array of Sprites, an array of Phaser.Physics.P2.Body objects, or don't give anything // in which case it will check every Body in the whole world. var bodies = game.physics.p2.hitTest(pointer.position, [ contra, bunny, block, wizball ]); if (bodies.length === 0) { result = "You didn't click a Body"; } else { result = "You clicked: "; for (var i = 0; i < bodies.length; i++) { // The bodies that come back are p2.Body objects. // The parent property is a Phaser.Physics.P2.Body which has a property called 'sprite' // This relates to the sprites we created earlier. // The 'key' property is just the texture name, which works well for this demo but you probably need something more robust for an actual game. result = result + bodies[i].parent.sprite.key; if (i < bodies.length - 1) { result = result + ', '; } } } } function update() { bunny.body.rotateLeft(2); } function render() { game.debug.text(result, 32, 32); } }; Link to comment Share on other sites More sharing options...
Westinghouse Posted March 24, 2015 Share Posted March 24, 2015 Your webserver does not appear to be configured to handle JSON files correctly. Link to comment Share on other sites More sharing options...
NumberOneRobot Posted March 24, 2015 Author Share Posted March 24, 2015 The web server I'm running is the Python SimpleHTTPServer, how do I configure it to handle JSON files? On a different note, I don't really understand why that is the issue here. From what I can see, it seems that the physics property/function of the game.load object does not exist. It is undefined when I log it in the console. Link to comment Share on other sites More sharing options...
NumberOneRobot Posted March 24, 2015 Author Share Posted March 24, 2015 Changing from using phaser.min.js to using phaser.js seems to have fixed the undefined issue. Link to comment Share on other sites More sharing options...
Recommended Posts