敌人加载,但不移动

时间:2018-04-09 19:47:15

标签: javascript game-physics

感谢您的帮助,现在下面的代码正常运行。

以前的问题(已解决):

我正在创建一个游戏,敌人加载,但不会移动。玩家移动,所以我一直在寻找特定于敌人移动的错误,但我还没有能够确定错误。

敌人通过此功能被召唤来移动。

function moveAll() {
    redShip.move(); // moves player, working
    enemy.move(); // moves enemy, works now after correction.
}

敌人的移动功能

const UFO_SPEED = 1.9;
const UFO_TIME_BETWEEN_CHANGE_DIR = 85;

UFOClass.prototype = new movingWrapPositionClass();

function UFOClass() {
    this.x = Math.random()*600;
    this.y = Math.random()*800;
    this.xv = 0;
    this.yv = 0;
    this.cyclesTilDirectionChange = 0; // added line to make work


    this.superclassMove = this.move;
    this.move = function() {
        this.superclassMove();

    this.cyclesTilDirectionChange--;
    if(this.cyclesTilDirectionChange <= 0) {
        var randAng = Math.random() * Math.PI*2.0;
        this.xv = Math.cos(randAng) * UFO_SPEED;
        this.yv = Math.sin(randAng) * UFO_SPEED;
        this.cyclesTilDirectionChange = UFO_TIME_BETWEEN_CHANGE_DIR;
        }
    }

玩家和敌人之间的共同移动功能

function movingWrapPositionClass() {
     this.move = function() {
     this.x += this.xv;
     this.y += this.yv;
     this.handleScreenWrap();
}

添加正在运行的Ship函数

const SPACESPEED_DECAY_MULT = 0.99;
const THRUST_POWER = 0.15;
const TURN_RATE = 0.06;

shipClass.prototype = new movingWrapPositionClass();

function shipClass() {
    this.myShot = new shotClass();
    this.x = 400;
    this.y = 300;
    this.ang = -3;
    this.xv = 0;
    this.yv = 0;
    this.myShipPic; // which picture to use


    this.keyHeld_Gas = false;
    this.keyHeld_TurnLeft = false;
    this.keyHeld_TurnRight = false;

    this.controlKeyUp;
    this.controlKeyRight;
    this.controlKeyDown;
    this.controlKeyLeft;
    this.controlKeyForShotFire;


    this.setupInput = function(upKey, rightKey, downKey, leftKey, shotKey) {
        this.controlKeyUp = upKey;
        this.controlKeyRight = rightKey;
        this.controlKeyDown = downKey;
        this.controlKeyLeft = leftKey;  
        this.controlKeyForShotFire = shotKey;

    }

this.superclassMove = this.move;
this.move = function() {
    this.x += Math.cos(this.ang) * this.xv;
    this.y += Math.sin(this.ang) * this.yv;

    this.superclassMove();
    this.xv *= SPACESPEED_DECAY_MULT;
    this.yv *= SPACESPEED_DECAY_MULT;

    if(this.keyHeld_Gas) {
        this.xv += THRUST_POWER;
        this.yv += THRUST_POWER;
        this.superclassMove();

    }
    if(this.keyHeld_TurnLeft) {
        this.ang -= TURN_RATE;
        }  
    if(this.keyHeld_TurnRight) {
        this.ang += TURN_RATE;  
        }

0 个答案:

没有答案