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

how to rotate the car while moving in phase.js

本文关键字:移动 旋转 汽车 js phase      更新时间:2023-09-26

当我使用此代码时,我面临如下错误: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'); 
};
});

您正在将this.game.car与game.kar混合。除此之外,速度和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>