TimTation Posted October 5, 2014 Share Posted October 5, 2014 Hello all!I have been messing around with Phaser and have made a small start to a game by playing with some source-code from the space hipster tutorial. I have a top down character animated with a sprite-sheet and am now working on using code from the "shoot the pointer" Phaser example to add a rotating child sprite that shoots, onto my animation of a walking body. My code works fine if I treat the rotating shooting sprite and walking animated body separately, but as soon as I add the shooting sprite onto the walking body as a child the code breaks down. I am rather new to Phaser and game dev so I apologize in advance if I am missing something obvious. here is the code for my main game state(as I said the child adding seems to mess things up but I wouldn't be surprised if it was something else I am no aware of...)var SpaceHipster = SpaceHipster || {};//title screenSpaceHipster.Game = function(){};//variablesvar player;var torso;var bullets;//var fireRate controls the firerate of players gunvar fireRate = 100;var nextFire = 0;SpaceHipster.Game.prototype = { create: function() { //make background of repeating graa images this.background = this.game.add.tileSprite(0, 0, this.game.width, this.game.height, 'grass'); //bullet group and creation bullets = this.game.add.group(); bullets.enableBody = true; bullets.physicsBodyType = Phaser.Physics.ARCADE; bullets.createMultiple(50, 'bullet'); bullets.setAll('checkWorldBounds', true); bullets.setAll('outOfBoundsKill', true); //player init player = this.game.add.sprite(0, 0, 'player'); player.anchor.setTo(0.5); //player torso init torso = this.game.add.sprite(0, 0, 'playerTorso'); torso.anchor.setTo(0.5); player.addChild(torso); //add the torso to the player sprite //enable physics on both the legs and torso pf player this.game.physics.enable(player, Phaser.Physics.ARCADE); this.game.physics.enable(torso, Phaser.Physics.ARCADE); //player animation loading player.animations.add('down', [0, 1, 2, 3, 4, 5, 6, 7, 8], 10, true); player.animations.add('up', [9, 10, 11, 12, 13, 14, 15, 16], 10, true); player.animations.add('left', [17, 18, 19, 20, 21, 22, 23, 24], 10, true); player.animations.add('right', [25, 26, 27, 28, 29, 30, 31, 32], 10, true); //player will not go off the screen player.body.collideWorldBounds = true; torso.body.collideWorldBounds = true; //code for rotating torso to pointer torso.body.allowRotation = false; player.body.allowRotation = false; }, update: function() { //allow player torso to rotate torso.rotation = this.game.physics.arcade.angleToPointer(torso); //if the mouse is clicked execute the fire function if (this.game.input.activePointer.isDown) { this.fire(); } //player movement handler if (this.game.input.keyboard.isDown(Phaser.Keyboard.A)) { player.x -= 4; player.animations.play('left'); } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.D)) { player.x += 4; player.animations.play('right'); } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.W)) { player.y -= 4; player.animations.play('up'); } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.S)) { player.y += 4; player.animations.play('down'); } else { // Stand still player.animations.stop(); player.frame = 1; } },fire: function() { //if the fire cooldown is done executer the rest of the bullet firing logic if (this.game.time.now > nextFire && bullets.countDead() > 0) { nextFire = this.game.time.now + fireRate; var bullet = bullets.getFirstDead(); //controls where on the player image the bullet originates from bullet.reset(torso.x - 8, torso.y - 8); //'fires' bullet image to pointer coords, the number controls the bullets velocity this.game.physics.arcade.moveToPointer(bullet, 1000); }},render: function() { this.game.debug.spriteInfo(torso, 32, 450);}};/*TODO*/Thanks for any help you can offer! - TimTation Link to comment Share on other sites More sharing options...
powerfear Posted October 5, 2014 Share Posted October 5, 2014 Change this line notice the make instead of add. torso = this.game.make.sprite(0, 0, 'playerTorso'); Link to comment Share on other sites More sharing options...
TimTation Posted October 5, 2014 Author Share Posted October 5, 2014 Well I changed the line but my problem persists... when I attempt to shoot, the bullets origin is 0,0 not the coords of the torso image attached to the walking sprite. I'll mess with the code a bit but any other insights you have would be great! Link to comment Share on other sites More sharing options...
Recommended Posts