NistorCristian

Members
  • Content Count

    15
  • Joined

  • Last visited

About NistorCristian

  • Rank
    Member

Recent Profile Visitors

869 profile views
  1. Hello, I'm trying to make a grey effect on some graphic elements but they look pixelated. var game = new Phaser.Game(800, 600, Phaser.WEBGL, 'phaser-example', { preload: preload, create: create }); function preload() { game.load.image('phaser', 'assets/sprites/phaser2.png'); game.load.script('gray', 'https://cdn.rawgit.com/photonstorm/phaser/master/filters/Gray.js'); } function create() { var logo = game.add.sprite(game.world.centerX, game.world.centerY, 'phaser'); logo.anchor.setTo(0.5, 0.5); var graphics = game.add.graphics(100, 100); // set a fill and line style graphics.beginFill(0xFF3300); graphics.lineStyle(10, 0xffd900, 1); // draw a shape graphics.moveTo(50,50); graphics.lineTo(250, 50); graphics.lineTo(100, 100); graphics.lineTo(250, 220); graphics.lineTo(50, 220); graphics.lineTo(50, 50); graphics.endFill(); // set a fill and line style again graphics.lineStyle(10, 0xFF0000, 0.8); graphics.beginFill(0xFF700B, 1); // draw a second shape graphics.moveTo(210,300); graphics.lineTo(450,320); graphics.lineTo(570,350); graphics.quadraticCurveTo(600, 0, 480,100); graphics.lineTo(330,120); graphics.lineTo(410,200); graphics.lineTo(210,300); graphics.endFill(); // draw a rectangle graphics.lineStyle(2, 0x0000FF, 1); graphics.drawRect(50, 250, 100, 100); // draw a circle graphics.lineStyle(0); graphics.beginFill(0xFFFF0B, 0.5); graphics.drawCircle(470, 200, 200); graphics.endFill(); graphics.lineStyle(20, 0x33FF00); graphics.moveTo(30,30); graphics.lineTo(600, 300); window.graphics = graphics; grayfilter = game.add.filter('Gray'); grayfilter.gray = 1; this.game.world.filters = [grayfilter]; } You can copy paste all the code in here to see the result. http://phaser.io/examples/v2/display/arc Is there a way to fix this ?
  2. Hi, I'm doing a Gun that shots bullets. I put those bullets into a group and when a bullet touches something I kill the bullet. The problem is if I kill the bullet the sprite dissapears but it's body is still there. I can see it if I have game.debug.box2dWorld(); enabled. How can I remove it ? Is destroy() a better option? I'm reusing the bullet. Thank you
  3. Hi, I'm trying to make a missile that follows my mouse. It is ok when it launches but it doesn't revive in the proper position. It revives where the cursor is. Here is the code: // Try to get a missile from the missileGroup // If a missile isn't available, create a new one and add it to the group. Game.prototype.launchMissile = function(x, y) { // // Get the first dead missile from the missileGroup var missile = this.missileGroup.getFirstDead(); // If there aren't any available, create a new one if (missile === null) { missile = new Missile(this.game); this.missileGroup.add(missile); } // Revive the missile (set it's alive property to true) // You can also define a onRevived event handler in your explosion objects // to do stuff when they are revived. missile.revive(); // Move the missile to the given coordinates missile.x = x; missile.y = y; return missile; }; // Try to get a used explosion from the explosionGroup. // If an explosion isn't available, create a new one and add it to the group. // Setup new explosions so that they animate and kill themselves when the // animation is complete. Game.prototype.getExplosion = function(x, y) { // Get the first dead explosion from the explosionGroup var explosion = this.explosionGroup.getFirstDead(); // If there aren't any available, create a new one if (explosion === null) { explosion = this.game.add.sprite(0, 0, 'explosion'); explosion.anchor.setTo(0.5, 0.5); // Add an animation for the explosion that kills the sprite when the // animation is complete var animation = explosion.animations.add('boom', [0,1,2,3], 60, false); animation.killOnComplete = true; // Add the explosion sprite to the group this.explosionGroup.add(explosion); } // Revive the explosion (set it's alive property to true) // You can also define a onRevived event handler in your explosion objects // to do stuff when they are revived. explosion.revive(); // Move the explosion to the given coordinates explosion.x = x; explosion.y = y; // Set rotation of the explosion at random for a little variety explosion.angle = this.game.rnd.integerInRange(0, 360); // Play the animation explosion.animations.play('boom'); // Return the explosion itself in case we want to do anything else with it return explosion; }; // Missile constructor var Missile = function(game, x, y) { Phaser.Sprite.call(this, game, x, y, 'rocket'); // Set the pivot point for this sprite to the center this.anchor.setTo(0.5, 0.5); // Enable physics on the missile this.game.physics.box2d.enable(this); // Define constants that affect motion this.SPEED = 250; // missile speed pixels/second this.TURN_RATE = 5; // turn rate in degrees/frame this.WOBBLE_LIMIT = 15; // degrees this.WOBBLE_SPEED = 250; // milliseconds this.SMOKE_LIFETIME = 3000; // milliseconds // Create a variable called wobble that tweens back and forth between // -this.WOBBLE_LIMIT and +this.WOBBLE_LIMIT forever this.wobble = this.WOBBLE_LIMIT; this.game.add.tween(this) .to( { wobble: -this.WOBBLE_LIMIT }, this.WOBBLE_SPEED, Phaser.Easing.Sinusoidal.InOut, true, 0, Number.POSITIVE_INFINITY, true ); // Add a smoke emitter with 100 particles positioned relative to the bottom // center of this missile this.smokeEmitter = this.game.add.emitter(0, 0, 100); // Set motion parameters for the emitted particles this.smokeEmitter.gravity = 0; this.smokeEmitter.setXSpeed(0, 0); this.smokeEmitter.setYSpeed(-80, -50); // make smoke drift upwards // Make particles fade out after 1000ms this.smokeEmitter.setAlpha(1, 0, this.SMOKE_LIFETIME, Phaser.Easing.Linear.InOut); // Create the actual particles this.smokeEmitter.makeParticles('smoke'); // Start emitting smoke particles one at a time (explode=false) with a // lifespan of this.SMOKE_LIFETIME at 50ms intervals this.smokeEmitter.start(false, this.SMOKE_LIFETIME, 50); }; // Missiles are a type of Phaser.Sprite Missile.prototype = Object.create(Phaser.Sprite.prototype); Missile.prototype.constructor = Missile; Missile.prototype.update = function() { // If this missile is dead, don't do any of these calculations // Also, turn off the smoke emitter if (!this.alive) { this.smokeEmitter.on = false; return; } else { this.smokeEmitter.on = true; } // Position the smoke emitter at the center of the missile this.smokeEmitter.x = this.x; this.smokeEmitter.y = this.y; // Calculate the angle from the missile to the mouse cursor game.input.x // and game.input.y are the mouse position; substitute with whatever // target coordinates you need. var targetAngle = this.game.math.angleBetween( this.x, this.y, this.game.input.activePointer.x, this.game.input.activePointer.y ); // Add our "wobble" factor to the targetAngle to make the missile wobble // Remember that this.wobble is tweening (above) targetAngle += this.game.math.degToRad(this.wobble); // Gradually (this.TURN_RATE) aim the missile towards the target angle if (this.rotation !== targetAngle) { // Calculate difference between the current angle and targetAngle var delta = targetAngle - this.rotation; // Keep it in range from -180 to 180 to make the most efficient turns. if (delta > Math.PI) delta -= Math.PI * 2; if (delta < -Math.PI) delta += Math.PI * 2; if (delta > 0) { // Turn clockwise this.body.rotateRight(180); } else { // Turn counter-clockwise this.body.rotateRight(-180); } // Just set angle to target angle if they are close if (Math.abs(delta) < this.game.math.degToRad(this.TURN_RATE)) { this.rotation = targetAngle; } } // Calculate velocity vector based on this.rotation and this.SPEED this.body.velocity.x = Math.cos(this.rotation) * this.SPEED; this.body.velocity.y = Math.sin(this.rotation) * this.SPEED; }; and in Game update if (this.missileGroup.countLiving() === 0) { this.launchMissile(this.game.width/2, this.game.height - 16); } // If any missile is within a certain distance of the mouse pointer, blow it up this.missileGroup.forEachAlive(function(m) { var distance = this.game.math.distance(m.x, m.y, this.game.input.activePointer.x, this.game.input.activePointer.y); if (distance < 50) { m.kill(); // this.getExplosion(m.x, m.y); } }, this); Thank you.
  4. Hello, I'm trying to implement a missile based on this example: http://gamemechanicexplorer.com/#homingmissiles-1 If I change the physics from arcade to box2d the missile doesn't follow the pointer. I tried to see what the problem is and I think is this.rotation. Box2d sees rotation different from arcade. How can I fix this ? if (this.rotation !== targetAngle) { // Calculate difference between the current angle and targetAngle var delta = targetAngle - this.rotation; // Keep it in range from -180 to 180 to make the most efficient turns. if (delta > Math.PI) delta -= Math.PI * 2; if (delta < -Math.PI) delta += Math.PI * 2; if (delta > 0) { // Turn clockwise this.angle += this.TURN_RATE; } else { // Turn counter-clockwise this.angle -= this.TURN_RATE; } // Just set angle to target angle if they are close if (Math.abs(delta) < this.game.math.degToRad(this.TURN_RATE)) { this.rotation = targetAngle; } } Thank you
  5. Or how can I remove the rect that Phaser creates when there is no image?
  6. Or is there a way to call Phaser.Sprite.call(this, game, x, y); ?? Without the sprite?
  7. Hello, How can I do something like this: Phaser.Sprite.call(this, game, x, y , graphics); Where graphics is: graphics = game.add.graphics(0, 0); graphics.beginFill(0xFF33ff); graphics.drawPolygon(polygonStructure); graphics.endFill(); game.add.sprite(x, y); This doesn't help me because I have to make something like this: MonsterBunny = function (game, x, y, rotateSpeed) { Phaser.Sprite.call(this, game, x, y, 'bunny'); this.rotateSpeed = rotateSpeed; }; MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype); MonsterBunny.prototype.constructor = MonsterBunny; /** * Automatically called by World.update */ MonsterBunny.prototype.update = function() { this.angle += this.rotateSpeed; }; To be able to do: var wabbit = new MonsterBunny(game, 200, 300, 1); Thank you
  8. Hi, I want to make a Box2d Sof body. I made something like that but I don't know if I'm using the right method. As you can see in the attached image there is a circle and some smaller circles to define the Soft body area. To make it purple I'm using graphics. graphics.clear(); graphics.beginFill(0xFF33ff); graphics.drawPolygon(polygonStructure); graphics.endFill(); That is what I put in update function. polygonStructure is an array made with position x and y of every little circle. What is better, to update the points of a polygon and put a graphic sprite over it or draw it like I'm doing it now? And ... How can I do the edges more soft? Using some arc or something? Thank you
  9. I'm trying to make a soft body width Box2D after this tutorial: http://www.emanueleferonato.com/2012/09/21/step-by-step-creation-of-a-box2d-soft-body-blob/ This is my code and I attached a picture. // Enable Box2D physics game.physics.startSystem(Phaser.Physics.BOX2D); game.physics.box2d.debugDraw.joints = true; game.physics.box2d.gravity.y = 500; game.physics.box2d.setBoundsToWorld(); game.physics.box2d.restitution = 0.8; game.physics.box2d.setBoundsToWorld(); // Set up handlers for mouse events game.input.onDown.add(mouseDragStart, this); game.input.addMoveCallback(mouseDragMove, this); game.input.onUp.add(mouseDragEnd, this); var particleNumber = 15; var particleDistance = 50; var blobX = 320; var blobY = 240; // You can also create a Circle body directly, without binding it to a sprite: var circle = new Phaser.Physics.Box2D.Body(game, null, blobX, blobY); circle.setCircle(16); circle.bullet = true; for (var i = 0; i < particleNumber; i++) { var angle = (2*Math.PI) / particleNumber * i; var posX = blobX + particleDistance * Math.cos(angle); var posY = blobY + particleDistance * Math.sin(angle); var smallBall = new Phaser.Physics.Box2D.Body(game, null, posX, posY); smallBall.setCircle(2); smallBall.bullet = true; dynamicBodies.push( smallBall ); //bodyA, bodyB, length, ax, ay, bx, by, frequency, damping game.physics.box2d.distanceJoint(circle, smallBall); }; for (var i = 0; i < particleNumber; i++) { if (i < dynamicBodies.length - 1) { game.physics.box2d.distanceJoint(dynamicBodies[i], dynamicBodies[i+1]); }else{ game.physics.box2d.distanceJoint(dynamicBodies[i], dynamicBodies[0]); }; } I have few problems: 1) I don't know how to make it soft when it touches the ground. 2) If I drag the object fast and smash it into the wall it will be like the second picture. Thank you
  10. Hi, I want to make and emitter but to place the particles behind the element. How can I do that? Unfortunately I don't know how. //create an emitteremitter = game.add.emitter(0, 0, 1000);emitter.makeParticles('fireblob'); http://codepen.io/anon/pen/RWdYQB Thank you
  11. It looks like the problem is here: Phaser.Sprite.call(this, game, x, y, 'player'); Beacause if I console.log(this) it is not an isoSprite. So, how can I use something like Phaser.Sprite.call(this, game, x, y, 'player') but in the isometric world?
  12. The position is my first clue that I don't add the player properly. Before making the Player function I had here player = game.add.isoSprite(390, 450, 0, 'player', 2); The 1390 and 1450 are placing the player in almost exact location as before with 390 and 450
  13. Hi, I'm trying to make a player using the Isometric Plugin. My problem is that the player doesn't move. I'm sure it is not a problem in the movement code but it is somewhere else. Here is what I did: var Start = function (game) { };Start.prototype = { preload: function () { game.load.spritesheet('player', 'http://nistorcristian.com/game/img/player.png', 8, 25); // game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; game.time.advancedTiming = true; // Add and enable the plug-in. game.plugins.add(new Phaser.Plugin.Isometric(game)); // Start the IsoArcade physics system. game.physics.startSystem(Phaser.Plugin.Isometric.ISOARCADE); game.world.setBounds(0, 0, 3000, 3000); game.iso.anchor.setTo(0.5, 0.2); }, create: function () { // Create a group for our tiles, so we can use Group.sort isoGroup = game.add.group(); isoMarginGroup = game.add.group(); // Set the global gravity for IsoArcade. game.physics.isoArcade.gravity.setTo(0, 0, -500); player = new Player(this.game, 1390, 1450); game.add.existing(player); game.camera.follow(player); }, update: function () { } }; var Player = function (game, x, y) { // player = game.add.isoSprite(390, 450, 0, 'player', 2); Phaser.Sprite.call(this, game, x, y, 'player'); this.tint = 0x86bfda; this.anchor.set(0.5); game.physics.isoArcade.enable(this); this.body.collideWorldBounds = true; this.animations.add('up', [3], 12, true); this.animations.add('down', [0], 12, true); this.animations.add('left', [2], 12, true); this.animations.add('right', [1], 12, true); }; Player.prototype = Object.create(Phaser.Sprite.prototype); Player.prototype.constructor = Player; Player.prototype.update = function() { this.cursors = game.input.keyboard.createCursorKeys(); this.game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN, Phaser.Keyboard.SPACEBAR ]); // this.angle += this.rotateSpeed; var player = this; if (this.cursors.up.isDown) { if (facing != 'up'){ player.animations.play('up'); facing = 'up'; } player.body.velocity.y = -speed; } else if (this.cursors.down.isDown) { if (facing != 'down'){ player.animations.play('down'); facing = 'down'; } player.body.velocity.y = speed; } else { player.body.velocity.y = 0; } if (this.cursors.left.isDown) { if (facing != 'left'){ player.animations.play('left'); facing = 'left'; } player.body.velocity.x = -speed; } else if (this.cursors.right.isDown) { if (facing != 'right'){ player.animations.play('right'); facing = 'right'; } player.body.velocity.x = speed; } else { player.body.velocity.x = 0; } };The animation is changing but the player doesn't move. I don't think I add the player correctly. Thank you
  14. I'm trying to make a Missile that will follow the player. My problem is that I don't know how to get the player position inside Missile object. I will paste my code below and write a comment where the problem is: window.onload = function() { var GameState = function(game) { }; // Here is a custom game object Player = function (game, x, y) { Phaser.Sprite.call(this, game, x, y, 'player'); }; Player.prototype = Object.create(Phaser.Sprite.prototype); Player.prototype.constructor = Player; /** * Automatically called by World.update */ Player.prototype.update = function() { // this.angle += this.rotateSpeed; }; // Load images and sounds GameState.prototype.preload = function() { this.game.load.image('ground', '/phaser/img/background2.png'); this.game.load.spritesheet('player', '/phaser/img/dude.png', 32, 48); this.game.load.image('bullet', '/phaser/img/bullet.png'); this.game.load.image('gun', '/phaser/img/gun.png'); this.game.load.image('rocket', '/assets/gfx/rocket.png'); this.game.load.image('platform', 'assets/platform.png'); }; // Setup the example GameState.prototype.create = function() { var player = new Player(game, 200, 300); game.add.existing(player); // Set stage background to something sky colored this.game.stage.backgroundColor = 0x4488cc; // Define movement constants this.MAX_SPEED = 350; // pixels/second this.ACCELERATION = 2600; // pixels/second/second this.DRAG = 200; // pixels/second this.GRAVITY = 1500; // pixels/second/second this.JUMP_SPEED = -400; // pixels/second (negative y is up) this.SHOT_DELAY = 100; // milliseconds (10 bullets/second) this.BULLET_SPEED = 500; // pixels/second this.NUMBER_OF_BULLETS = 10; this.platforms = this.add.physicsGroup(); for (var i = 0; i <= 10; i++) { this.platforms.create(this.game.rnd.integerInRange(450, 600), this.game.rnd.integerInRange(300, 300), 'platform'); }; this.platforms.setAll('body.allowGravity', false); this.platforms.setAll('body.immovable', true); // Create a player sprite this.player = player; // Enable physics on the player this.game.physics.enable(this.player, Phaser.Physics.ARCADE); // Make player collide with world boundaries so he doesn't leave the stage this.player.body.collideWorldBounds = true; // Set player minimum and maximum movement speed this.player.body.maxVelocity.setTo(this.MAX_SPEED, this.MAX_SPEED * 10); // x, y // Add drag to the player that slows them down when they are not accelerating this.player.body.drag.setTo(this.DRAG, 0); // x, y this.player.body.setSize(20, 32, 5, 16); this.player.animations.add('left', [0, 1, 2, 3], 10, true); this.player.animations.add('turn', [4], 20, true); this.player.animations.add('right', [5, 6, 7, 8], 10, true); this.player.animations.add('jumpright', [6], 10, true); this.player.animations.add('jumpleft', [1], 10, true); // Create a missile and add it to the game in the bottom center of the stage this.game.add.existing( new Missile(this.game, this.game.width/2, this.game.height - 16) ); // Since we're jumping we need gravity game.physics.arcade.gravity.y = this.GRAVITY; // Flag to track if the jump button is pressed this.jumping = false; // Create some ground for the player to walk on this.ground = this.game.add.group(); for(var x = 0; x < this.game.width; x += 32) { // Add the ground blocks, enable physics on each, make them immovable var groundBlock = this.game.add.sprite(x, this.game.height - 32, 'ground'); this.game.physics.enable(groundBlock, Phaser.Physics.ARCADE); groundBlock.body.immovable = true; groundBlock.body.allowGravity = false; this.ground.add(groundBlock); } // Create an object representing our gun this.gun = this.game.add.sprite(this.player.x, this.player.y, 'gun'); // Set the pivot point to the center of the gun this.gun.anchor.setTo(0.5, 0.5); // Create an object pool of bullets this.bulletPool = this.game.add.group(); for(var i = 0; i < this.NUMBER_OF_BULLETS; i++) { // Create each bullet and add it to the group. var bullet = this.game.add.sprite(0, 0, 'bullet'); this.bulletPool.add(bullet); // Set its pivot point to the center of the bullet bullet.anchor.setTo(0.5, 0.5); // Enable physics on the bullet this.game.physics.enable(bullet, Phaser.Physics.ARCADE); // Set its initial state to "dead". bullet.kill(); } // Simulate a pointer click/tap input at the center of the stage // when the example begins running. this.game.input.activePointer.x = this.game.width/2; this.game.input.activePointer.y = this.game.height/2; // Capture certain keys to prevent their default actions in the browser. // This is only necessary because this is an HTML5 game. Games on other // platforms may not need code like this. this.game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN ]); }; // Missile constructor var Missile = function(game, x, y) { Phaser.Sprite.call(this, game, x, y, 'rocket'); // Set the pivot point for this sprite to the center this.anchor.setTo(0.5, 0.5); // Enable physics on the missile game.physics.enable(this, Phaser.Physics.ARCADE); // Define constants that affect motion this.SPEED = 250; // missile speed pixels/second this.TURN_RATE = 5; // turn rate in degrees/frame }; // Missiles are a type of Phaser.Sprite Missile.prototype = Object.create(Phaser.Sprite.prototype); Missile.prototype.constructor = Missile; Missile.prototype.update = function() { // Calculate the angle from the missile to the mouse cursor game.input.x // and game.input.y are the mouse position; substitute with whatever // target coordinates you need. var targetAngle = this.game.math.angleBetween( this.x, this.y, this.player.position.x, this.player.position.y // ----- >>>> HOW TO CALL PLAYER ??? ); // Gradually (this.TURN_RATE) aim the missile towards the target angle if (this.rotation !== targetAngle) { // Calculate difference between the current angle and targetAngle var delta = targetAngle - this.rotation; // Keep it in range from -180 to 180 to make the most efficient turns. if (delta > Math.PI) delta -= Math.PI * 2; if (delta < -Math.PI) delta += Math.PI * 2; if (delta > 0) { // Turn clockwise this.angle += this.TURN_RATE; } else { // Turn counter-clockwise this.angle -= this.TURN_RATE; } // Just set angle to target angle if they are close if (Math.abs(delta) < this.game.math.degToRad(this.TURN_RATE)) { this.rotation = targetAngle; } } // Calculate velocity vector based on this.rotation and this.SPEED this.body.velocity.x = Math.cos(this.rotation) * this.SPEED; this.body.velocity.y = Math.sin(this.rotation) * this.SPEED; }; var facing = 'right'; // The update() method is called every frame GameState.prototype.update = function() { this.physics.arcade.collide(this.player, this.platforms, this.setFriction, null, this); this.physics.arcade.collide(this.bulletPool, this.platforms, this.setFrictionBullet, null, this); // Collide the player with the ground this.game.physics.arcade.collide(this.player, this.ground); if (this.leftInputIsActive()) { if (facing != 'left'){ this.player.animations.play('left'); facing = 'left'; } if (this.upInputIsActive()) { this.player.animations.play('jumpleft'); }; // If the LEFT key is down, set the player velocity to move left this.player.body.acceleration.x = -this.ACCELERATION; } else if (this.rightInputIsActive()) { if (facing != 'right'){ this.player.animations.play('right'); facing = 'right'; } if (this.upInputIsActive()) { this.player.animations.play('jumpright'); }; // If the RIGHT key is down, set the player velocity to move right this.player.body.acceleration.x = this.ACCELERATION; } else if(this.upInputIsActive()){ if (facing == 'idle-left'){ this.player.animations.play('jumpleft'); }else if(facing == 'idle-right'){ this.player.animations.play('jumpright'); } }else { if (facing != 'idle'){ this.player.animations.stop(); if (facing == 'left'){ this.player.frame = 0; facing = 'idle-left'; }else if(facing == 'right'){ this.player.frame = 5; if (facing != 'idle-left') { facing = 'idle-right'; }; }else if(facing == 'idle-left'){ this.player.frame = 0; }else if(facing == 'idle-right'){ this.player.frame = 5; } } this.player.body.acceleration.x = 0; } console.log(this.player.body.velocity.x); // Set a variable that is true when the player is touching the ground var onTheGround = this.player.body.touching.down; // If the player is touching the ground, let him have 2 jumps if (onTheGround) { this.jumps = 2; this.player.body.drag.x = 500; this.jumping = false; } // Jump! Keep y velocity constant while the jump button is held for up to 150 ms if (this.jumps > 0 && this.upInputIsActive(150)) { this.player.body.drag.x = 200; this.player.body.velocity.y = this.JUMP_SPEED; this.jumping = true; } // Reduce the number of available jumps if the jump input is released if (this.jumping && this.upInputReleased()) { this.jumps--; this.jumping = false; } // Aim the gun at the pointer. // All this function does is calculate the angle using // Math.atan2(yPointer-yGun, xPointer-xGun) this.gun.rotation = this.game.physics.arcade.angleToPointer(this.gun); this.gun.position.x = this.player.position.x + 20; this.gun.position.y = this.player.position.y + 40; // Shoot a bullet if (this.game.input.activePointer.isDown) { if (this.gun.angle > -90 && this.gun.angle < 90 && this.game.input.activePointer.x > this.player.body.x && (facing == 'right' || facing == 'idle-right')) { this.shootBullet(); }else if((this.gun.angle < -90 || this.gun.angle > 90) && this.game.input.activePointer.x < this.player.body.x && (facing == 'left' || facing == 'idle-left')){ this.shootBullet(); }; } }; GameState.prototype.shootBullet = function() { // Enforce a short delay between shots by recording // the time that each bullet is shot and testing if // the amount of time since the last shot is more than // the required delay. if (this.lastBulletShotAt === undefined) this.lastBulletShotAt = 0; if (this.game.time.now - this.lastBulletShotAt < this.SHOT_DELAY) return; this.lastBulletShotAt = this.game.time.now; // Get a dead bullet from the pool var bullet = this.bulletPool.getFirstDead(); // If there aren't any bullets available then don't shoot if (bullet === null || bullet === undefined) return; // Revive the bullet // This makes the bullet "alive" bullet.revive(); // Bullets should kill themselves when they leave the world. // Phaser takes care of this for me by setting this flag // but you can do it yourself by killing the bullet if // its x,y coordinates are outside of the world. bullet.checkWorldBounds = true; bullet.outOfBoundsKill = true; // Set the bullet position to the gun position. bullet.reset(this.gun.x, this.gun.y); bullet.rotation = this.gun.rotation; // Shoot it in the right direction bullet.body.velocity.x = Math.cos(bullet.rotation) * this.BULLET_SPEED; bullet.body.velocity.y = Math.sin(bullet.rotation) * this.BULLET_SPEED; bullet.body.allowGravity = false; }; // This function should return true when the player activates the "go left" control // In this case, either holding the right arrow or tapping or clicking on the left // side of the screen. GameState.prototype.leftInputIsActive = function() { var isActive = false; isActive = this.input.keyboard.isDown(Phaser.Keyboard.LEFT); return isActive; }; // This function should return true when the player activates the "go right" control // In this case, either holding the right arrow or tapping or clicking on the right // side of the screen. GameState.prototype.rightInputIsActive = function() { var isActive = false; isActive = this.input.keyboard.isDown(Phaser.Keyboard.RIGHT); return isActive; }; // This function should return true when the player activates the "jump" control // In this case, either holding the up arrow or tapping or clicking on the center // part of the screen. GameState.prototype.upInputIsActive = function(duration) { var isActive = false; isActive = this.input.keyboard.downDuration(Phaser.Keyboard.UP, duration); return isActive; }; // This function returns true when the player releases the "jump" control GameState.prototype.upInputReleased = function() { var released = false; released = this.input.keyboard.upDuration(Phaser.Keyboard.UP); return released; }; GameState.prototype.setFriction = function(player, platform) { player.body.x -= platform.body.x - platform.body.prev.x; }; GameState.prototype.setFrictionBullet = function(bullet, platform) { bullet.kill(); }; var game = new Phaser.Game(848, 450, Phaser.CANVAS, 'game'); game.state.add('game', GameState, true);};