စင်တာ စတင်

通过利用游戏旋转章节中讲解的绘制组件的新方式,动作现在更加灵活了。


如何移动物体?

component 构造函数中添加一个 speed 属性,该属性代表组件当前的速度。

还要对 newPos() 方法进行一些更改,以根据 speedangle 计算组件的位置。

ပုံမှာ အခြေခံအရိုက် ၁ ဖြစ်၍ အပေါ်အရိုက် ၁ ကို တိုက်ပါက အသုံးပြုလာသည် speed အရိုက် ၁ ကို တိုက်ပါက အသုံးပြုလာသည်

实例

function component(width, height, color, x, y) {
  this.gamearea = gamearea;
  this.width = width;
  this.height = height;
  this.angle = 0;
  this.speed = 1;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.save();
    ctx.translate(this.x, this.y);
    ctx.rotate(this.angle);
    ctx.fillStyle = color;
    ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
    ctx.restore();
  }
  this.newPos = function() {
    this.x += this.speed * Math.sin(this.angle);
    this.y -= this.speed * Math.cos(this.angle);
  }
}

亲自试一试

အရှေ့ပြောင်း

ကျွန်ုပ်တို့က အရှေ့သို့ နှင့် အနောက်သို့ အသွားအပြား လုပ်နိုင်ချင်ပါ၍ ရှိသည်။ အမည်မှာ moveAngle အသစ် အရိုက် ကို အသုံးပြုပါ၊ ယခု အပျက်အထားသား ပြောင်းလွှဲမှု သို့မဟုတ် အသွားအပြား ကို ကြည့်ပါ newPos() တွင် moveAngle အရိုက် angle:

实例

လုပ်ကြံ့ moveangle အရိုက် ၁ ကို တိုက်ပြီး ဘယ်လိုပြောင်းလွှဲမည်ကို ကြိုးစားပါ

function component(width, height, color, x, y) {
  this.width = width;
  this.height = height;
  this.angle = 0;
  this.moveAngle = 1;
  this.speed = 1;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.save();
    ctx.translate(this.x, this.y);
    ctx.rotate(this.angle);
    ctx.fillStyle = color;
    ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
    ctx.restore();
  }
  this.newPos = function() {
    this.angle += this.moveAngle * Math.PI / 180;
    this.x += this.speed * Math.sin(this.angle);
    this.y -= this.speed * Math.cos(this.angle);
  }
}

亲自试一试

ကျောက်ကန်းကို အသုံးပြုပါ

ကျောက်ကန်းကို အသုံးပြုပါက အနန့်အသားပေါ်မှ အကြီးအမိုးပေါ်သို့ ဘယ်လိုပြောင်းလွှဲသနည်း။ ကျောက်ကန်းကို “အမြင့်” အနန့်အသားကို အသုံးပြုပါက အကြီးအမိုးပေါ်သို့ သို့မဟုတ် ကွေးကွေးကွေးပေါ်သို့ ဘယ်လိုပြောင်းလွှဲမည် မဟုတ်၊ အခါအားလျော်၍ တစ်ဝက်မှ တစ်ဝက်သို့ သွားလာကာ အရှေ့ဘက်သို့ ပြောင်းလွှဲပြီး သို့မဟုတ် သွားခေါ် နှင့် ခေါ်လာ အနန့်အသားကို အရှေ့ဘက်သို့ ဘယ်လိုပြောင်းလွှဲသနည်း。

实例

亲自试一试

确保游戏区域获得焦点,然后使用箭头键移动红色方块。