图像后碰撞也?

时间:2016-01-27 08:59:41

标签: javascript phaser-framework

我正在创建一个用于碰撞检测的停车场游戏。

我的问题是在图像更新后检查碰撞。

it's taking collision there I want to take at starting

check this one also

我的Phaser代码:

var app = angular.module('myapp',[]);
app.controller('mycontrol',function($scope){
var game = new Phaser.Game(1100, 590, Phaser.AUTO, 'cargame');
var mainState = { 
 preload:function(){
     game.load.image('wood','images/wood1.png'); 
    game.load.image('car', 'images/maincar.png');

},

create:function(){


    game.physics.startSystem(Phaser.Physics.ARCADE);
    this.car =this.game.add.sprite(game.world.centerX,game.world.centerY,'car');
   //this.car.body.collide = true;
    this.car.anchor.setTo(0.5,0.5);
     //this.wood1.anchor.setTo(0.5,0.5);

    game.physics.arcade.enable(this.car);                     
    this.car.body.allowRotation = true; 
     platforms = game.add.group();
     platforms.enableBody = true;

       var ground = platforms.create(200,300, 'wood');
    ground.body.immovable = true;
},                                                              

update:function(){



            if(game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){
                this.car.body.angularVelocity = -200;
            }
            else if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
                this.car.body.angularVelocity = 200;
            }
            else if(game.input.keyboard.isDown(Phaser.Keyboard.UP)){
               this.car.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(this.car.angle, 150));
            }
            else if(game.input.keyboard.isDown(Phaser.Keyboard.DOWN)){
               this.car.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(this.car.angle, -150));
            }
            else{
                this.car.body.angularVelocity =0;  
                 this.car.body.velocity.x =0;
                this.car.body.velocity.y = 0;
            }
            game.physics.arcade.collide(this.car, platforms);




},

};




$scope.car1 = function(){
    game.state.add('main', mainState);  
game.state.start('main'); 

};
});

和html

<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="controller2.js"></script>
<script type="text/javascript" src="phaser.min.js"></script>
</head>
<body ng-app="myapp" ng-controller="mycontrol">
<div id="cargame" ng-click="car1()">
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

来自phaser docs(Physics.Arcade,collide method):

  

可以提供可选的processCallback。如果给出这个功能   当两个精灵被发现碰撞时将被调用。它是   在任何分离发生之前调用,给你机会   执行额外的检查。如果函数返回true则为   进行碰撞和分离

所以,你可以这样做:

game.physics.arcade.collide(this.car, platforms, myCallBack);

然后,你定义'myCallBack'功能,你可以用碰撞对象做任何你想做的事。