我正在做一个个人项目,现在我想放置两艘船,一艘船必须在没有我控制的情况下反弹(Ship2.png),另一艘船必须用键盘上的箭头(Ship.png)来处理它.

问题是,当我想添加图像"shi2.png"时,它似乎被覆盖了,不再放置图像"ship.png".

我已经将图像加载到第this.loadImages = function()部分

问题出在这一部分:object.ship = new spaceship(200, 100,object.images["ship"]);如果我添加"Ship2",它将被覆盖,并且不再显示"Ship".

你对我如何解决这个问题有什么建议吗?

enter image description here enter image description here

const CREATION = 100;
const PRECARGE = 200;
const START   = 300;

class spaceship{

   constructor(x, y,image){
      this.x = x;
      this.y = y;
      this.image=image;
      this.speed = 20;//initial speed, before any key
      this.get_direction=null;
      this.set_direction=null;
   }        
   getX() {
     return this.x;
   }
   getY(){
     return this.y;
   }
   getSpeed(){
      return this.speed;
   }    
   setX(x){
      this.x = x;
   }
   setY(y) {
      this.y = y;
   }  
   setSpeed(speed){
      this.speed=speed;
   }     
   setimage(image) {
      this.image = image;
   }
   getimage() {
      return this.image;
   }    
   draw(ctx) {
     ctx.drawImage(this.getimage(),0,0,100,50,this.getX(),this.getY(),100,50);
   }
 }//end of spaceship class 


function Animation(){    
   
   this.state = CREATION;
   this.images  = new Array();
   this.canvas = document.getElementById("canvas");
   this.context = this.canvas.getContext("2d");
   this.aux_canvas = document.createElement("canvas"); // "canvas" refer to the <canvas> tag.
   this.aux_context = this.aux_canvas.getContext("2d")
      
   this.canvas.width=document.body.clientWidth;  //current window size
   this.canvas.height=document.body.clientHeight;
   this.aux_canvas.width=document.body.clientWidth;
   this.aux_canvas.height=document.body.clientHeight;

   this.ship = null;
      
   var object=this;
   
   this.loadImages = function(){

      this.images["ship"] = new Image();
      this.images["ship"].name="ship";
      this.images["ship"].src= "https://i.stack.imgur.com/xpgUd.png";

      this.images["ship2"] = new Image();
      this.images["ship2"].name="ship2";
      this.images["ship2"].src= "https://i.stack.imgur.com/3letf.png";
   }

   this.update = function(){
      this.aux_context.clearRect(0, 0, this.canvas.width, this.canvas.height); //clean the canvas of ships
      this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);    // in both canvases the background is not erased
      this.ship.draw(this.aux_context);
      
      // Check bounds
      const movingLeft = this.ship.get_direction === this.ship.getX && this.ship.getSpeed() < 0;
      const touchingLeftBound = this.ship.x <= 0;

      const movingRight = this.ship.get_direction === this.ship.getX && this.ship.getSpeed() > 0;
      const touchingRightBound = this.ship.x >= this.canvas.width - this.ship.image.width;

      const movingUp = this.ship.get_direction === this.ship.getY && this.ship.getSpeed() < 0;
      const touchingTopBound = this.ship.y <= 0;

      const movingDown = this.ship.get_direction === this.ship.getY && this.ship.getSpeed() > 0;
      const touchingBottomBound = this.ship.y >= this.canvas.height - this.ship.image.height;
      
      if (
        (movingLeft && touchingLeftBound) ||
        (movingDown && touchingBottomBound) ||
        (movingUp && touchingTopBound) ||
        (movingRight && touchingRightBound)
      ) {
        this.ship.setSpeed(this.ship.getSpeed() * -1);
      }
  
      this.context.drawImage(this.aux_canvas,0,0,this.aux_canvas.width,this.aux_canvas.height,0,0,this.canvas.width,this.canvas.height);
      this.ship.set_direction(this.ship.get_direction()+(this.ship.getSpeed()));

   }

   this.displacement = function(e) {

      e = e || window.event;

      if (e.keyCode == '38'|| e.keyCode == '40') {
         //up
         object.ship.set_direction=object.ship.setY;
         object.ship.get_direction=object.ship.getY;    
         if (e.keyCode == '38') //up
            object.ship.setSpeed(-20);
         else//down
            object.ship.setSpeed(20);    
      }
      else if (e.keyCode == '37' || e.keyCode == '39') {
         object.ship.set_direction=object.ship.setX;
         object.ship.get_direction=object.ship.getX;        
         if (e.keyCode == '37') //left
            object.ship.setSpeed(-20);
         else//right
            object.ship.setSpeed(20);         
      }

 }

   this.executeMachineStates = function(){
      var imagesUploaded=true;
      if (object.state == CREATION) {
         object.loadImages();
         object.state = PRECARGE;
         setTimeout(object.executeMachineStates, 100);

      } else {
         if(object.state==PRECARGE){

            console.log("state: PRECARGE");
            for(var i=0;i<object.images.length;i++)
               if(object.images[i].complete!=true)
                  imagesUploaded=false;                  
            if(imagesUploaded==true){
               //200 and 100 is the ship's initial position
               object.ship = new spaceship(200, 100,object.images["ship"]);
               object.ship.get_direction=object.ship.getX;  //initial movement
               object.ship.set_direction=object.ship.setX;  //on x-axis
               object.state = START;
               document.onkeydown = object.displacement;
            }
            setTimeout(object.executeMachineStates, 100);
         }else{
            if(object.state==START){
               object.update();
               setTimeout(object.executeMachineStates, 100);
            }
         }
      }
   }

}//end of class/function Animation

animation= new Animation();
animation.executeMachineStates();
body {
    background-image:url("space.png");
    background-repeat: no-repeat;
    background-size: 100%;
}
<html>
<head>
<title>Martians</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="margin: 0;padding: 0">
   <canvas id="canvas">
   </canvas>

   <script src="script.js"></script>

</body>
</html>

推荐答案

问题已经解决了.我做的第一件事是放置一条线:

this.ship2 = null;

this.ship2.draw(this.aux_context);

在第//Check bounds部分中,为Ship 2声明相同的变量(movingLeft2、TouchingLeftBord2、movingRight2...)

最后,添加Ship 2:

object.ship2 = new spaceship(400, 400,object.images["ship2"]);
               object.ship2.get_direction=object.ship2.getX;  //initial movement
               object.ship2.set_direction=object.ship2.setX;  //on x-axis
               object.state = START;

Javascript相关问答推荐

为什么函数不使用以前函数返回的参数?

如果使用React Select ,如何将CSS类添加到元素中?

Angular 拦截器错误处理删除方法问题

我不明白这个react 组件有什么问题

为什么JavaScript双边字符串文字插值不是二次的?

如何提取Cypress中文本

如何访问react路由v6加载器函数中的查询参数/搜索参数

有Angular 的material .未应用收件箱中的价值变化

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

警告!合同执行期间遇到错误[执行已恢复](Base Layer 2)

字节数组通过echo框架传输到JS blob

从mat—country—select获取整个Country数组

判断表格单元格中是否存在文本框

使搜索栏更改语言

如何在 cypress 中使用静态嵌套循环

Chart.js-显示值应该在其中的引用区域

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

按什么顺序接收`storage`事件?

无法设置RazorPay订阅API项目价格

我无法在Api Reaction本机上发出GET请求