గేమ్ మొబిలిటీ

కోర్సు సిఫార్సులు:


మళ్ళీ ఆడండి

ఎలా పదార్థాన్ని కదలించాలి? లో component speed కాల్క్యులేషన్ కోణంలో కొత్త కంపోనెంట్ ను జోడించండి

అట్రిబ్యూట్, ఇది కంపోనెంట్ ప్రస్తుత వేగాన్ని ప్రతినిధీకరిస్తుంది. newPos() మెథడ్ ను కొన్ని మార్పులు చేయడం ద్వారా speed మరియు angle కంపోనెంట్ యొక్క స్థానాన్ని కాల్క్యులేషన్ చేయండి.

డిఫాల్ట్ లో, కంపోనెంట్ పైకి మారినది, దానిని 'పైకి' సెట్ చేయడం ద్వారా speed అట్రిబ్యూట్ ను 1కి సెట్ చేసి, కంపోనెంట్ ప్రస్తుతం ముందుకు కదలడం ప్రారంభిస్తుంది.

实例

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:

实例

మూవేంజల్ అట్రిబ్యూట్ ను 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);
  }
}

亲自试一试

కీబోర్డ్ ఉపయోగించండి

కీబోర్డ్ ఉపయోగించటంలో ఎలా ఎరుపు ప్రతిమ కదలుతుంది? మీరు 'పైకి' ఆర్క్ ఉపయోగించినప్పుడు, ఎరుపు ప్రతిమ పైకి లేదా క్రిందికి కదలదు, బదులుగా ఒక పక్కను మరొక పక్కకు కదలి, మరియు 'ఎడమ' లేదా 'కుడి' ఆర్క్ ఉపయోగించినప్పుడు ఎడమకు లేదా కుడికి చేరుకుంటుంది.

实例

亲自试一试

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