自上而下的汽车游戏中的漂移标记

时间:2016-06-12 16:32:46

标签: javascript game-physics phaser-framework

我正在玩phaserJS来制作一个从上到下的汽车游戏的简单例子。 我认为这样会很好,加速时会给汽车增加一些漂移痕迹。所以我想出了两个graphics元素,然后在每一帧上更新它们的位置。

这样的事情:

var game = new Phaser.Game(500, 500, Phaser.AUTO, 'game');
	var mainState = { 
		preload:function(){
			game.load.image('car', 'notfound');

		},
	 	create: function() {
	 		game.stage.backgroundColor = '#ffffff'
			game.physics.startSystem(Phaser.Physics.P2JS);

			car = game.add.sprite(window.innerWidth/2, window.innerHeight/2, 'car');


			game.physics.p2.enable(car, false);
			car.anchor.setTo(0.5, 0.8);

			this.graphics = game.add.graphics(0,0);
			this.graphics2 = game.add.graphics(0,0);
			this.graphics.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*5),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*5));
		    this.graphics2.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*(-5)),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*(-5)));
		    this.trololo;
		},	
		update: function() {
			car.body.damping = 0.9;
		    car.body.setZeroRotation();
		    
          
		    if (game.input.keyboard.isDown(Phaser.Keyboard.UP)){
		    	
		    	this.graphics.lineStyle(2, 0x111111, 1);
		    	this.graphics2.lineStyle(2, 0x111111, 1);
		    	
		    	this.graphics.lineTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*5),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*5));
		    	this.graphics2.lineTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*(-5)),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*(-5)));

		    	
		    	if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
					car.body.rotateLeft(100);
				}
			    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
			    	car.body.rotateRight(100);
			    }
		    	car.body.thrust(400);
		    }
		    else{
		    	this.graphics.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*5),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*5));
		    	this.graphics2.moveTo(car.body.x+(Math.cos(this.math.degToRad(car.body.angle))*(-5)),car.body.y+(Math.sin(this.math.degToRad(car.body.angle))*(-5)));
		    	if(Math.abs(car.body.velocity.x) > 6 || Math.abs(car.body.velocity.y) > 6){
			    	if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
						car.body.rotateLeft(10);
					} 
				    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
				    	car.body.rotateRight(10);
				    }
			    }
		    } 
			    
		}


	};
	game.state.add('main', mainState);  
	game.state.start('main');
<div id="game"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.8/phaser.min.js"></script>

现在,我希望这个标记逐渐消失......有关如何实现这一点的任何线索?

1 个答案:

答案 0 :(得分:1)

我看到两种方法:

  1. 粒子引擎 - 在一段时间后淡出每个轨道粒子
  2. 在专用(屏幕外)画布上绘制轨迹并应用模糊滤镜/减量alpha值
  3. 纯JS中方法2的演示:

    var bgCanvas = document.getElementById('bg-canvas'),
        bgContext = bgCanvas.getContext('2d'),
        fgCanvas = document.getElementById('fg-canvas'),
        fgContext = fgCanvas.getContext('2d');
    
    function bgFade() {
      var imageData = bgContext.getImageData(0, 0, bgCanvas.width, bgCanvas.height);
      var data = imageData.data;
      for (var i = 3; i < data.length; i += 4) {
        data[i] -= 5;
      }
      bgContext.putImageData(imageData, 0, 0);
    }
    
    function update() {
      bgFade();
    
      bgContext.beginPath();
      bgContext.arc(Math.random() * bgCanvas.width, Math.random() * bgCanvas.height, 4, 0, Math.PI * 2);
      bgContext.fillStyle = '#000000';
      bgContext.fill();
    
      fgContext.beginPath();
      fgContext.arc(Math.random() * fgCanvas.width, Math.random() * fgCanvas.height, 4, 0, Math.PI * 2);
      fgContext.fillStyle = '#ff0000';
      fgContext.fill();
    
      requestAnimationFrame(update);
    }
    
    requestAnimationFrame(update);
    #stage canvas {
      position: absolute;
      left: 0;
      top: 0;
    }
    <div id="stage">
      <canvas id="bg-canvas"></canvas>
      <canvas id="fg-canvas"></canvas>
    </div>

    请注意,在背景画布上绘制的所有内容都会淡出,而前景画布上绘制的所有内容都会保留。