seanBlythe Posted July 8, 2014 Share Posted July 8, 2014 So I'm still playing around with the phaser tutorial - Making your first phaser game. I've added an enemy baddie and I've got him to move left and right along a platform using a timer and a movement function. However, I need help with the timer. Sometimes my baddie will walk far right or left and just keep on walking in that direction. Can someone please explain what each of the parameters do in the following statement: game.time.events.repeat(Phaser.Timer.SECOND * 3, 10, moveBaddie, game) I've also included the full code:var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() { game.load.image('sky', 'assets/sky.png'); game.load.image('ground', 'assets/platform.png'); game.load.image('star', 'assets/star.png'); game.load.spritesheet('dude', 'assets/dude.png', 32, 48); game.load.spritesheet('baddie', 'assets/baddie.png', 32, 32);}var platforms;var score = 0;var scoreText;function create() { // We're going to be using physics, so enable the arcade physics system game.physics.startSystem(Phaser.Physics.ARCADE); // A simple background for a game game.add.sprite(0, 0, 'sky'); // The platforms group contains the ground and the 2 ledges we can jump on platforms = game.add.group(); // we will enable physics for any object that is created in this group platforms.enableBody = true; // here we create the ground var ground = platforms.create(0, game.world.height - 64, 'ground'); // Scale it to fit the width of the game (the original sprite is 400x32 in size) ground.scale.setTo(2, 2); // this stops it from falling away when you jump on it ground.body.immovable = true; // now let's create two ledges var ledge = platforms.create(400, 400, 'ground'); ledge.body.immovable = true; ledge = platforms.create(-150, 250, 'ground'); ledge.body.immovable = true; // The player and its settings player = game.add.sprite(32, game.world.height - 150, 'dude'); // We need to enable physics on the player game.physics.arcade.enable(player); // Player physics properties. Give the little guy a slight bounce player.body.bounce.y = 0.2; player.body.gravity.y = 300; player.body.collideWorldBounds = true; // our two animations, walking left and right player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); // enemy sprite enemy = game.add.sprite(132, game.world.height -150, 'baddie'); game.physics.arcade.enable(enemy); enemy.body.gravity.y = 300; // so enemy baddie doesn't walk off screen enemy.body.collideWorldBounds = true; enemy.animations.add('left', [0, 1], 10, true); enemy.animations.add('right', [2, 3], 10, true); // baddie movement timer creation game.time.events.repeat(Phaser.Timer.SECOND * 3, 10, moveBaddie, game); // enemy sprite end cursors = game.input.keyboard.createCursorKeys(); stars = game.add.group(); stars.enableBody = true; // Here we'll create 12 of them evenly spaced apart for (var i = 0; i < 12; i++) { // Create a star inside of the stars group var star = stars.create(i * 70, 0, 'star'); // lets gravity do its thing star.body.gravity.y = 6; // This just gives each star a slightly random bounce value star.body.bounce.y = 0.7 + Math.random() * 0.2; } scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000'});}function update() { // Collide the player and the stars with the platforms game.physics.arcade.collide(player, platforms); game.physics.arcade.collide(stars, platforms); game.physics.arcade.collide(enemy, platforms); game.physics.arcade.overlap(player, stars, collectStar, null, this); //reset the players velocity (movement) player.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the Left player.body.velocity.x = -150; player.animations.play('left'); } else if (cursors.right.isDown) { //move to the right player.body.velocity.x = 150; player.animations.play('right'); } else { // Stand still player.animations.stop(); player.frame = 4; } //Allow the player to jump if they are touching the ground if (cursors.up.isDown && player.body.touching.down) { player.body.velocity.y = -350; }}// Allow the enemy sprite to movefunction moveBaddie() { // randomise the movement baddieMover = game.rnd.integerInRange(1, 3); // simple if statement to choose if and which way the baddie moves if (baddieMover == 1) { enemy.body.velocity.x = 25; enemy.animations.play('right'); } else if (baddieMover == 2) { enemy.body.velocity.x = -25; enemy.animations.play('left'); } else { enemy.body.velocity.x = 0; enemy.animations.stop(); }} function collectStar (player, star) { // Removes the star from the screen star.kill(); // Add and update the score score += 10; scoreText.text = 'Score: ' + score;} Link to comment Share on other sites More sharing options...
lewster32 Posted July 8, 2014 Share Posted July 8, 2014 game.time.events.repeat( Phaser.Timer.SECOND * 3, // every 3 seconds... 10, // ... for 10 times... moveBaddie, // ... call 'moveBaddie' function... game // ... within the 'game' context - could also safely be 'this');Two things I can see - firstly, because you're using repeat, when the enemy has moved 10 times, it'll just keep going in the last direction forever. Secondly, because the random movement doesn't check which way the enemy is moving, it's entirely likely the enemy could move in the same direction 3 or 4 times in a row. The first fix would be to instead of using repeat, use the loop event:game.time.events.loop(Phaser.Timer.SECOND * 3, moveBaddie, this); Loop will continue forever until stopped. This will be okay so long as the enemy isn't killed or removed, as then you'd have it trying to move the enemy still. In this case you'd need to ensure you remove the timer event. The second fix is a bit more dependent on the gameplay you want to achieve. If you want it to truly randomly wander, keep it as is - just be aware that it could potentially wander in the same direction several times, taking it a long way. If you want it to wander within a fixed range, you should perform some checks to see if its x and/or y positions are within that range, and if not, steer the enemy back inside the range. if (enemy.x < enemy.rangeLeft && baddieMover == 2) { baddieMover = 1; // enemy is too far left, so move it right } else if (enemy.x > enemy.rangeRight && baddieMover == 1) { baddieMover = 2; // enemy is too far right, so move it left } if (baddieMover == 1) { enemy.body.velocity.x = 25; enemy.animations.play('right'); } else if (baddieMover == 2) { enemy.body.velocity.x = -25; enemy.animations.play('left'); } else { enemy.body.velocity.x = 0; enemy.animations.stop(); }You'd set enemy.rangeLeft and enemy.rangeRight maybe when you create the enemy, by using its starting position plus and minus a value to ensure it stays within a certain range of its 'home'. You could also just store enemy.startX and set enemy.range to how far from its start you want it to wander and do it this way: if (enemy.x < enemy.startX - enemy.range && baddieMover == 2) { baddieMover = 1; // enemy is too far left, so move it right } else if (enemy.x > enemy.startX + enemy.range && baddieMover == 1) { baddieMover = 2; // enemy is too far right, so move it left } seanBlythe 1 Link to comment Share on other sites More sharing options...
seanBlythe Posted July 8, 2014 Author Share Posted July 8, 2014 Wow, that's spot on. Thank you for taking the time to help me out. Link to comment Share on other sites More sharing options...
lewster32 Posted July 8, 2014 Share Posted July 8, 2014 No problem - edited it a few times to flesh it out so you may wanna take another look Link to comment Share on other sites More sharing options...
seanBlythe Posted July 8, 2014 Author Share Posted July 8, 2014 Cool, thanks again lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts