Maantje Posted April 30, 2015 Share Posted April 30, 2015 Good day everyone I am currently in the process of making my first little project with phaser. The project is a simple little game with a car on a highway trying to dodge incoming traffic. I started with the placing the sprites and moving them around with was surprisingly easy. But now that i got that done i tried to add physics to be specific the arcade one and play around with collisions. This is where i ran into a problem the collisions work fine and i manage to remove the player sprite on "impact". But when i increase the spawn rate of my enemy cars. the enemy's car sprite "hitbox" gets removed before i can hit it because of the variable getting overwritten i suppose. I think i am supposed to use groups for this purpose but i can't figure out how to exactly i keep running into errors. Hopefully some one here on the forums can help me Here is the code :var road;var roadstripe;var enemycar;var player;var timer;var enemycargroup;var enemycarspeed = 5000;var speed = 2000;var baselinetime;var currenttimevar score = 0;var t;var game = new Phaser.Game(640, 480, Phaser.AUTO, 'gameContainer', { preload: preload, create: create, update: update });function preload() { baselinetime = game.time.now; //Scalling game to screen this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; this.scale.pageAlignVertically = true; this.scale.setScreenSize( true ); //Loading sprites game.load.image('road', 'assets/roadbg.png'); game.load.image('roadstripe', 'assets/roadstripe.png'); game.load.image('grass', 'assets/grass.png'); game.load.image('player', 'assets/playerb.jpg'); //Setting background color game.stage.backgroundColor = '#33A933'; }function create() { //Enabling Arcade physics game.physics.startSystem(Phaser.Physics.ARCADE); //adding sprites //Grass on the left game.add.sprite(0, 0, 'grass'); game.add.sprite(0, 125, 'grass'); game.add.sprite(0, 250, 'grass'); game.add.sprite(0, 375, 'grass'); //grass on the right game.add.sprite(550, 0, 'grass'); game.add.sprite(550, 125, 'grass'); game.add.sprite(550, 250, 'grass'); game.add.sprite(550, 375, 'grass'); //main road. road = game.add.sprite(75, 0, 'road'); //the car for the player player = game.add.sprite(320, 340, 'player'); //setting properties for the player car player.anchor.set(0.5); player.width = 80; player.height = 80; player.angle = 90; player.body = null; //calling functions moveRoadStripe() spawnEnemyCars() resetGame(); game.physics.arcade.enable([player, enemycar]); player.body.enable= true; var text = "SCORE:"; var style = { font: "20px Arial", fill: "#ffFFFF", align: "center" }; t = game.add.text(10, 10, text+" "+score, style); }function update() { this.game.debug.body(player); this.game.debug.body(enemycar); currenttime = game.time.now; score = (currenttime - baselinetime) / 100; t.setText("SCORE: "+ Math.floor(score)); resetGame(); var steering = false; var playerrotationspeed = 8 game.physics.arcade.overlap(player, enemycar, collisionHandler, null, this); if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { if(player.x < 120) { player.x = 120; player.angle.x = 90; } player.x -= playerrotationspeed; player.angle -= 1; if(player.angle < 78) { player.angle= 78; } steering = true } if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { if(player.x > 510) { player.x = 510; player.angle.x = 90; } player.x += playerrotationspeed; player.angle += 1; if(player.angle > 102) { player.angle= 102; } steering = true } if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT) && game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { player.angle = 90; } if(steering == false) { while(player.angle < 90){ if (game.time.now < game.time.now + 300){ player.angle += 1; } } while(player.angle > 90){ if (game.time.now < game.time.now + 300){ player.angle -= 1; } } } if (game.time.now > timer) { moveRoadStripe(185); moveRoadStripe(440) } if (game.time.now > cartimer) { spawnEnemyCars(); } enemycar.bringToTop(); player.bringToTop() t.bringToTop();}function spawnEnemyCars(){ //some vars. var side; var enemycarRotation var enemycarspeed //random Int for wich side of roade the car is gona spawn. if(game.rnd.integerInRange(0,3) > 0) { side = "left" enemycarRotation = -90; enemycarspeed = 4000; } if(side == "left") { if(game.rnd.integerInRange(0, 1) == 1) { enemycar = game.add.sprite(250, -200, 'player'); } else { enemycar = game.add.sprite(135, -200, 'player'); } } else { if(game.rnd.integerInRange(0, 1) == 1) { enemycar = game.add.sprite(500, -200, 'player'); } else { enemycar = game.add.sprite(377, -200, 'player'); } enemycarRotation = 90; enemycarspeed = 10000; } //Setting enemy car properties enemycar.width = 80; enemycar.height = 80; enemycar.angle = enemycarRotation; enemycar.anchor.set(0.5); game.physics.arcade.enable([player, enemycar]); enemycar.enable = true; //moving car down along the y axis game.add.tween( enemycar).to({ y: 2000 }, enemycarspeed, Phaser.Easing.Linear.None, true); //setting time for spawn rate. cartimer = game.time.now + 1200;}function moveRoadStripe(side) { //Adding a road stripe srite roadstripe = game.add.sprite(side, -50, 'roadstripe'); roadstripe.width = 8; roadstripe.height = 22; //Moving the roadstripe down along the y axis for the ilusion of movement game.add.tween(roadstripe).to({ y: 2000 }, speed, Phaser.Easing.Linear.None, true); //Setting time for the spawn rate timer = game.time.now + 125;}function resetGame(){ if (game.input.keyboard.isDown(Phaser.Keyboard.R)) { game.state.start(game.state.current); } if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { alert('paused'); }}function collisionHandler() { player.kill();} Link to comment Share on other sites More sharing options...
Recommended Posts