গেম মোশন

选修课


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

再玩一遍 如何移动物体? পদ্ধতিতে ডিফল্টে, কম্পোনেন্ট ওপরের দিকে পাল্টা হয়, এবং component

পদ্ধতিতে একটি newPos() প্রতিভূতি যা কম্পোনেন্টের বর্তমান গতিবেগকে প্রতিনিধিত্ব করে, ডিফল্টে, কম্পোনেন্ট ওপরের দিকে পাল্টা হয়, এবং পদ্ধতিতে কিছু পরিবর্তন করে নিয়ে angle এবং

কম্পোনেন্টের অবস্থান গণনা করতে ডিফল্টে, কম্পোনেন্ট ওপরের দিকে পাল্টা হয়, এবং speed

ইনস্ট্রান্স

function component(width, height, color, x, y) {
  প্রতিভূতি 1 করে নিয়ে যখন এই কম্পোনেন্ট সূচকতার কারণে এগিয়ে যাবে。
  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);
  }
}

আপনার হাতে পরীক্ষা করুন

this.gamearea = gamearea;

ঘুর্ণান্ত moveAngle নতুন প্রতিভূতির নামে যা বর্তমান চলার মান বা ঘুর্ণান্ত কোণ নির্দেশ করে। আমরা আরও দুইদিকে ঘুরতে চাই। একটি নামকরণ করুন newPos() পদ্ধতিতে অনুযায়ী moveAngle প্রতিভূতি গণনা angle:

ইনস্ট্রান্স

প্রয়োগ করুন, moveangle প্রতিভূতিকে 1 করে দেখুন কিছু ঘটবে:

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);
  }
}

আপনার হাতে পরীক্ষা করুন

কিবোর্ড ব্যবহার করে

কিভাবে কিবোর্ড ব্যবহার করে লাল বক্সটি সরবে? আপনি 'উপরে' আরোহণ দিয়ে লাল বক্সটি উপরে এবং নীচে সরবে না, বরং এক পাশ থেকে অন্য পাশে সরবে এবং ডান এবং বাম আরোহণ দিলে সরবে এবং ডান-বাম পাশে পাশে ঘুরবে。

ইনস্ট্রান্স

আপনার হাতে পরীক্ষা করুন

গেম এরিয়াটিকে ফোকাস করুন এবং তারপর আরোহী এবং অবতীত বাবদ চিহ্ন ব্যবহার করে লাল বক্সটিকে সরান