简体   繁体   English

我希望球撞到墙上时颜色变为红色。如何添加该代码?

[英]I want the color of the ball to change the red when it hits the wall.How do I add that code?

My Problem uploaded in codepen Click To see Click to see我的问题上传到codepen Click To see点击查看

Click To Preview Preview Click To Preview预览

look here.看这里。 You have to add contex.fillStyle = "here put color you want";你必须添加 contex.fillStyle = "here put color you want"; into the if sections;D You can look at this on my pen https://codepen.io/dzm11/pen/PoBwOrp进入 if 部分;D 你可以在我的笔上看这个https://codepen.io/dzm11/pen/PoBwOrp

    window.onload = function () {
  //کنواهای بدنه را دریافت میکند
  var canvas = document.getElementById("Canvas");
  var contex = canvas.getContext("2d");
  //ساخت چند متغیر-این برای پوزیشن ایکس و وای است
  var x = canvas.width / 2;
  var y = canvas.height / 2;
  //ساخت بیشتر از 2 متغیر-برای سرعته-اگر میخواهید حرکت را سریع ببینید-مقادیر را افزایش میدهد
  var dx = 2;
  var dy = -2;

  draw();

  function draw() {
    //تازه سازی کنواها
    contex.clearRect(0, 0, canvas.width, canvas.height);
    contex.beginPath();
    //اینجا با استفاده از آرک توپ زرد خودمان را ایجاد میکنیم
    contex.arc(x, y, 10, 0, Math.PI * 2);
    // contex.fillStyle="yellow";
    contex.fill();

    //دیوار
    if (x + dx > 480) {
      dx = -dx;
      contex.fillStyle = "red";
    }
    if (x + dx < 0) {
      dx = -dx;
      contex.fillStyle = "yellow";
    }
    if (y + dy > 320) {
      dy = -dy;
      contex.fillStyle = "blue";
    }
    if (y + dy < 0) {
      dy = -dy;
      contex.fillStyle = "black";
    }
    x += dx;
    y += dy;
  }
  setInterval(draw, 10);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试制作“ Ball”类,当它碰到墙时我希望它反弹,但是它仍然被挡在了角落里 - I am trying to make this “Ball” class and i want it to bounce when it hits the wall, but it just remains blocked in a corner 单击交换按钮时,如何使红色div更改背景颜色以在红色和绿色之间切换? - How do I get my red div to change background color to toggle between red and green when I click on the swap button? 如何将菜单上的Cufon字体的悬停颜色更改为红色? - How do I change the hover color to red for Cufon fonts on menu? 每次反弹时如何改变球的颜色? - How to change the ball color every time it bounces off the wall? 每次击球时将球的颜色更改为随机颜色 - Changing the color of the ball to a random colour every time it hits the wall 如何向该画布球添加图像? - How do I add image to this canvas ball? 我希望按钮在单击时改变颜色吗? - Do I want the buttons to change color when I click on them? 当我点击圆圈时,如何使用 Javascript 使圆圈改变颜色? 另外,如果所有圆圈都是红色,我该如何显示文字? 一世 - How do I make the circles change color with Javascript when I click on them? Also, how do I make text appear if all circles are red? I 如何更改此代码的背景颜色 - How do I change the background color on this code 当计时器分钟达到某个目标时,我如何更改页面的背景颜色? - How am i able to change the background color of the page when the timer minutes hits at a certain target?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM