如何在phase.js中移动时旋转汽车

时间:2016-01-11 09:41:07

标签: javascript phaser-framework

当我使用此代码时,我遇到如下错误:Uncaught TypeError: Cannot read property 'angle' of undefined.

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

create:function(){
    game.physics.startSystem(Phaser.Physics.ARCADE);
       this.car =this.game.add.sprite(game.world.centerX,game.world.centerY,'car');
        this.car.anchor.setTo(0.5,0.5);

        game.physics.arcade.enable(this.car);
        this.car.body.allowRotation = true;  
},

update:function(){


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

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

};
});

1 个答案:

答案 0 :(得分:0)

您正在将this.game.car与game.car混合使用。 除此之外,还必须将速度和angularVelocity应用于精灵体。

试试这个:



var game = new Phaser.Game(700, 590, Phaser.AUTO, 'cargame');
var mainState = { 
 preload:function(){
    game.load.image('car', 'notfonud');
},

create:function(){
    game.physics.startSystem(Phaser.Physics.ARCADE);
    this.car =this.game.add.sprite(game.world.centerX,game.world.centerY,'car');
    this.car.anchor.setTo(0.5,0.5);

    game.physics.arcade.enable(this.car);
    this.car.body.allowRotation = 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, 200));
            }
            else{
                this.car.body.angularVelocity = 0;
                 this.car.body.velocity.x =0;
                this.car.body.velocity.y = 0;
            }
},
};
game.state.add('main', mainState);  
game.state.start('main');

<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.4/phaser.min.js"></script>
<div id="cargame"></div>
&#13;
&#13;
&#13;