Ade Posted June 4, 2016 Share Posted June 4, 2016 Help I have an assignment that is due in on Tuesday, and I am really struggling with the code. I have create a mini car game. Where the car has to avoid hitting multiple dogs running randomly towards the car. I don’t know how to create the Javascript code in Phaser so the dogs run only on the road, from left to right and the longer the game is played, the faster the dogs will on the road. Link to comment Share on other sites More sharing options...
Taggrin Posted June 5, 2016 Share Posted June 5, 2016 You don't need them to follow the road, do you? The road has no curves so you can basically let the dogs run on a specific Y position which will indicate they will be running in either the top or bottom lane, giving the illusion they walk on the road (similar to what you do with the background). If you want a smooth bypass you just need to be sure the objects on the bottom lane have a higher layer (z index) so it makes sense. What I always do with infinite spawns is that I spawn an object (dog) to the right out of the screen (lets say your game screen is 640px, so for example spawn at 800). Give them a speed (which you can increase over time by multiplying it with the time variable) and just let them go. When the dog runs out of the screen (lets say -200px) just teleport it to a random location to the right (lets say 700-1000) so it doesn't appear in the same interval. With some random number generation you can determine it will be on the bottom or top lane during the next pass. Only requires just one dog object. Since this is an assignment I feel you should make the majority yourself (as that is the whole point of learning through an assignment), but I can give you some things to get started with. It doesn't work (no sprite loading etc.) and it is heavily unbalanced, but it at least should get you going. create: function() { //Physics arcade this.physics.startSystem(Phaser.Physics.ARCADE); //These are the lane y coordinates heights the dog can spawn (top or bottom) lanes = [250,350]; //Create the dog at random location outside the screen (700-1000) and random lane dog = this.add.sprite(this.rnd.integerInRange(700,1000),lanes[this.rnd.integerInRange(0,1)],'dogsprite'); this.physics.enable(dog,Phaser.Physics.ARCADE); //Make the dog go left dog.body.velocity.x = -50; }, update: function() { //Increase dog speed over time (this will be way too fast, but you can easily change that) dog.body.velocity.x = -50*this.time.now; //Check if dog is out of the screen if (dog.body.x < -200) { //Teleport to random position to the right dog.body.x = this.rnd.integerInRange(700,1000); //Set to random lane dog.body.y = lanes[this.rnd.integerInRange(0,1)]; } }, Link to comment Share on other sites More sharing options...
Ade Posted June 5, 2016 Author Share Posted June 5, 2016 Thanks so much, I will try it as soon I get home. I owe you a drink. Thanks Link to comment Share on other sites More sharing options...
Recommended Posts