tgriley1 Posted September 25, 2014 Share Posted September 25, 2014 Hi guys, I'm new to Phaser and game dev so this may be a stupid question... I want to make an enemy 'patrol' the floor of my basic platformer (js below). My first thought was to some sort of 'collide with world border' event/property to trigger some logic to flip the velocity, i assumed as thers a enemy.body.collideWorldBounds = true; property I'd be able to leverage it somehow. If the above is not possible am i right in assuming I need to create something for the enemy to collide with on the world borders? Any guidance is greatly appreciated!var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() { game.load.image('background', 'assets/background.png'); game.load.image('ground', 'assets/platform.png'); game.load.image('currency', 'assets/currency.png'); game.load.spritesheet('player', 'assets/player.png', 50, 50); game.load.spritesheet('enemy', 'assets/enemy.png', 50, 50);}var player;var enemy;var platforms;var cursors;var currencyGroup;var score = 0;var scoreText;function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.add.sprite(0, 0, 'background'); platforms = game.add.group(); platforms.enableBody = true; var ground = platforms.create(0, game.world.height - 64, 'ground'); ground.scale.setTo(2, 2); ground.body.immovable = true; var ledge1 = platforms.create(400, 400, 'ground'); ledge1.body.immovable = true; var ledge2 = platforms.create(-150, 250, 'ground'); ledge2.body.immovable = true; player = game.add.sprite(0, 0, 'player'); game.physics.arcade.enable(player); player.body.bounce.y = 0.2; player.body.gravity.y = 500; player.body.collideWorldBounds = true; player.animations.add('left', [0, 1], 10, true); player.animations.add('right', [1, 2], 10, true); enemy = game.add.sprite(0, 0, 'enemy'); game.physics.arcade.enable(enemy); enemy.body.bounce.y = 0.2; enemy.body.gravity.y = 500; enemy.body.collideWorldBounds = true; enemy.animations.add('left', [0, 1], 10, true); enemy.animations.add('right', [1, 2], 10, true); enemy.body.velocity.x = 100; currencyGroup = game.add.group(); currencyGroup.enableBody = true; for (var i = 0; i < 12; i++) { var currency = currencyGroup.create(i * 70, 0, 'currency'); currency.body.gravity.y = 300; currency.body.bounce.y = 0.7 + Math.random() * 0.2; } scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' }); cursors = game.input.keyboard.createCursorKeys();}function update() { game.physics.arcade.collide(player, platforms); game.physics.arcade.collide(currencyGroup, platforms); game.physics.arcade.overlap(player, currencyGroup, collectCurrency, null, this); game.physics.arcade.collide(enemy, platforms); player.body.velocity.x = 0; if (cursors.left.isDown) { player.body.velocity.x = -150; player.animations.play('left'); } else if (cursors.right.isDown) { player.body.velocity.x = 150; player.animations.play('right'); } else { player.animations.stop(); player.frame = 1; } if (cursors.up.isDown && player.body.touching.down) { player.body.velocity.y = -400; } game.physics.arcade.collide(enemy, currencyGroup, enemyCollide, null, this);}function collectCurrency(player, currency) { // Removes the star from the screen currency.kill(); // Add and update the score score += 10; scoreText.text = 'Score: ' + score;}function enemyCollide(enemy) { enemy.body.velocity.x = -100;} Link to comment Share on other sites More sharing options...
lewster32 Posted September 25, 2014 Share Posted September 25, 2014 If the enemy is driven by velocity and has something to physically collide with, setting body.bounce.x = 1 will cause it to completely reverse its velocity every time it collides with something on the x axis. The same can be done with y, but in a side-view game this wouldn't be very useful unless your enemy was some kind of super bouncy ball Link to comment Share on other sites More sharing options...
tgriley1 Posted September 25, 2014 Author Share Posted September 25, 2014 Thats great, does the trick nice and easy. I guess my guy is litterally bouncing off the world edges with no drag to slow him down rather then him 'walking' himself? I suspect thats a stupid question lol Another suggestion via chat from JUL was to use a tilemap, which from my understanding (almost none) could place immovable objects around the world...? so i could create tilemap that has objects around the edges with empty space in the middle for stuff i want to apply physics too, for example falling platforms? Link to comment Share on other sites More sharing options...
lewster32 Posted September 25, 2014 Share Posted September 25, 2014 It's a little more complicated than that, and I've not actually worked with TileMaps at all yet so I can't give you specifics, but basically yeah, TileMaps let you create a layout of immovable objects on a grid, which is ideal for stuff like platform games. You can create an 'object layer' too, which allows you to turn those objects into Phaser Sprites which can then act accordingly, and be used for stuff like falling platforms, enemies, collectibles and so on. Link to comment Share on other sites More sharing options...
tgriley1 Posted September 25, 2014 Author Share Posted September 25, 2014 cool, ill get right on it, thanks Link to comment Share on other sites More sharing options...
Recommended Posts