fusilian Posted April 19, 2016 Share Posted April 19, 2016 hello, i need help with my game. I am trying to mkae the asteroids bounce back when they reach my blockedLayer (in tilemap) but when they reach the border they just stick to it(they still move but along the blockedlayer). here is the code. is it because they have a behavior with angular velocity? var TopDownGame = TopDownGame || {}; var bulletProperties = { speed: 400, interval: 250, lifeSpan: 2000, maxCount: 30, }; var playerProperties = { velocity: 300, timeToReset: 3, blinkDelay: 0.2, }; var asteroidProperties = { startingAsteroids: 4, maxAsteroids: 20, incrementAsteroids: 2, asteroidLarge: { minVelocity: 50, maxVelocity: 150, minAngularVelocity: 0, maxAngularVelocity: 200, score: 20, nextSize: 'asteroidMedium', pieces: 2, explosion:'explosionLarge' }, asteroidMedium: { minVelocity: 50, maxVelocity: 200, minAngularVelocity: 0, maxAngularVelocity: 200, score: 50, nextSize: 'asteroidSmall', pieces: 2, explosion:'explosionMedium' }, asteroidSmall: { minVelocity: 50, maxVelocity: 300, minAngularVelocity: 0, maxAngularVelocity: 200, score: 100, explosion:'explosionSmall' }, }; TopDownGame.Game = function(){}; TopDownGame.Game.prototype = { create: function() { this.map = this.game.add.tilemap('level1'); this.map.addTilesetImage('tiles', 'tiles'); this.backgroundlayer = this.map.createLayer('background'); this.blockedLayer = this.map.createLayer('blocks'); this.map.setCollisionBetween(1, 2000, true, 'blocks'); this.backgroundlayer.resizeWorld(); this.bulletGroup=this.game.add.group(); this.bulletGroup.enableBody = true; this.bulletGroup.physicsBodyType = Phaser.Physics.ARCADE; this.bulletGroup.createMultiple(bulletProperties.maxCount, 'bullet'); this.bulletGroup.setAll('anchor.x', 0.5); this.bulletGroup.setAll('anchor.y', 0.5); this.bulletGroup.setAll('lifespan', bulletProperties.lifeSpan); this.asteroidGroup=this.game.add.group(); this.asteroidGroup.enableBody = true; this.asteroidGroup.physicsBodyType = Phaser.Physics.ARCADE; this.resetAsteroids(); this.player = this.game.add.sprite(250, 250, 'player'); this.player.anchor.set(0.5); this.game.physics.arcade.enable(this.player); this.player.maxAngular = 500; this.player.angularDrag = 50; this.game.camera.follow(this.player); this.cursors = this.game.input.keyboard.createCursorKeys(); this.key_fire=this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); }, init: function () { this.bulletInterval = 0; this.shipLives = 3; this.score = 0; }, createAsteroid: function (x, y, size, pieces) { if (pieces==undefined) {pieces = 1;} for (var i=0; i<pieces;i++) { var asteroid = this.asteroidGroup.create(x, y, size); asteroid.anchor.set(0.5, 0.5); asteroid.body.angularVelocity = this.game.rnd.integerInRange(asteroidProperties.minAngularVelocity, asteroidProperties.maxAngularVelocity); var randomAngle = this.game.math.degToRad(this.game.rnd.angle()); var randomVelocity = this.game.rnd.integerInRange(asteroidProperties.minVelocity, asteroidProperties.maxVelocity); this.game.physics.arcade.velocityFromRotation(randomAngle, randomVelocity, asteroid.body.velocity); } },resetAsteroids: function () { for (var i=0; i < 150; i++ ) { var side = Math.round(Math.random()); var x; var y; if (side) { x = 300 + Math.round(Math.random()) * 6400; y = 300 + Math.random() * 3200; } else { x = 300 + Math.random() * 6400; y = 300 + Math.round(Math.random()) * 3200; } this.createAsteroid(x, y, 'asteroidLarge'); } }, update: function() { this.game.physics.arcade.collide(this.player, this.blockedLayer, this.borderCollision, null, this); this.game.physics.arcade.overlap(this.player, this.asteroidGroup, this.asteroidCollision, null, this); this.game.physics.arcade.collide(this.asteroidGroup, this.blockedLayer, this.borderBounce, null, this); this.player.body.velocity.x = 0; this.player.body.velocity.y = 0; this.player.body.angularVelocity = 0; if (this.cursors.left.isDown) { this.player.body.angularVelocity = -300; } else if(this.cursors.right.isDown) { this.player.body.angularVelocity = 300; } if (this.cursors.up.isDown) { this.game.physics.arcade.velocityFromRotation(this.player.rotation, playerProperties.velocity, this.player.body.velocity); } if (this.key_fire.isDown) { this.fire(); } }, fire: function () { if (!this.player.alive){ return; } if (this.game.time.now > this.bulletInterval) { //this.sndFire.play(); var bullet = this.bulletGroup.getFirstExists(false); if (bullet) { var length = this.player.width * 0.5; var x = this.player.x + (Math.cos(this.player.rotation) * length); var y = this.player.y + (Math.sin(this.player.rotation) * length); bullet.reset(x, y); bullet.lifespan = bulletProperties.lifeSpan; bullet.rotation = this.player.rotation; this.game.physics.arcade.velocityFromRotation(this.player.rotation, bulletProperties.speed, bullet.body.velocity); this.bulletInterval = this.game.time.now + bulletProperties.interval; } } }, asteroidCollision : function (target, asteroid) { target.kill(); asteroid.kill(); //if (target=='player') { //this.destroyShip(); //} //this.splitAsteroid(asteroid); //this.updateScore(asteroidProperties[asteroid.key].score); //if (!this.asteroidGroup.countLiving()) { //game.time.events.add(Phaser.Timer.SECOND * gameProperties.delayToStartLevel, this.nextLevel, this); //} //var explosionGroup = asteroidProperties[asteroid.key].explosion + "Group"; //var explosion = this[explosionGroup].getFirstExists(false); //explosion.reset(asteroid.x, asteroid.y); //explosion.animations.play('explode', null, false, true); }, borderCollision : function (target) { target.kill(); }, borderBounce : function (asteroid) { if (asteroid!='player') { asteroid.body.bounce.set(1); } }, }; Link to comment Share on other sites More sharing options...
Recommended Posts