epicTCK Posted April 12, 2016 Share Posted April 12, 2016 I have the following code: var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create }); function preload(){ game.load.image('avocad', 'assets/avocad.png'); game.load.audio('drink','assets/drink.mp3'); } var drinkSound, avocad; function create(){ drinkSound = game.add.audio('drink'); game.stage.backgroundColor = "#FEFACB"; avocad = game.add.sprite(game.world.centerX, game.world.centerY, 'avocad').anchor.set(0.5); avocad.inputEnabled = true; avocad.events.onInputDown.add(pour, this); } function pour(){ drinkSound.play(); } Which doesn't work. It all checks out, but it it throws the following error: TypeError: avocad.events is undefined Stack trace: create@http://localhost:8000/avocadGame/game.js:14:2 Phaser.StateManager.prototype.loadComplete@http://localhost:8000/avocadGame/phaser.js:29240:13 Phaser.Loader.prototype.finishedLoading@http://localhost:8000/avocadGame/phaser.js:71446:9 Phaser.Loader.prototype.processLoadQueue@http://localhost:8000/avocadGame/phaser.js:71403:13 Phaser.Loader.prototype.asyncComplete@http://localhost:8000/avocadGame/phaser.js:71476:9 Phaser.Loader.prototype.fileComplete@http://localhost:8000/avocadGame/phaser.js:72323:13 Phaser.Loader.prototype.xhrLoad/xhr.onload@http://localhost:8000/avocadGame/phaser.js:71875:28 I've followed this example loosely. Could I get some help? Link to comment Share on other sites More sharing options...
rich Posted April 12, 2016 Share Posted April 12, 2016 The problem is the way you're chaining things together. Anchor.set returns a Point object, not a Sprite. So effectively your code is saying 'input enable this Point object', which ain't gonna fly. Link to comment Share on other sites More sharing options...
Arcanorum Posted April 12, 2016 Share Posted April 12, 2016 Dam beat me to it :L To elaborate. You may be thinking that a sprite object is being returned from the first function game.add.sprite(...) just fine, then you are just doing .anchor.set(...) on that object, but this is not the case. Do console.log(avocad); just after the avocad = game.add.sprite... line to see what I mean. Link to comment Share on other sites More sharing options...
Recommended Posts