【问题标题】:Use Function Parameters as Values?使用函数参数作为值?
【发布时间】:2021-01-04 16:49:06
【问题描述】:

我最近开始学习 JavaScript,并且正在尝试制作一个小型平台游戏。我有一些基本的功能,比如重力、运动和对象创建。但我想让世界构建更容易一些,所以我做了一个函数来创建块。一切都很好,但是为了在玩家和方块之间产生碰撞,我希望能够从那些特定的方块中获取变量,并用它来阻止我的角色。现在我将块设置为一个变量,并尝试调用该变量,但它显示为红色。无论如何我可以解决这个问题吗?还是有更好的碰撞方式?

此处并未显示所有代码,仅显示相关内容。

var b1;

function block(x, y, w, h, color) {
    c.fillStyle = color;
    c.fillRect(x, y, w, h);
}

function update() {
  if((pX >= b1.x - pW) && (pY >= b1.y - pH)) {
        pX = b1.x - pW;
  }
}

function draw() {
  b1 = block(500, 350, 100, 100, 'gray');
}

【问题讨论】:

    标签: javascript function 2d game-physics collision


    【解决方案1】:

    您可以使用函数创建Objects,使用new operator

    这不是创建对象的唯一方法,有很多方法,但它是与您的代码最匹配的方法。

    Objects 是 JavaScript 的基本构建块(就像 Java 的类),在开始使用它们之前需要深入了解它们

    基本示例

    // It is customary for object instantiated via new to have capitalized names
    function Block(x, y, w, h, color) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.color = color;
    }
    var b1 = new Block(500, 350, 100, 100, 'gray');
    drawBlock(b1);
    
    function drawBlock(block) {
        ctx.fillStyle = block.color;
        ctx.fillRect(block.x, block.y, block.w, block.h);
    }
    
    function update() {
      if((pX >= b1.x - pW) && (pY >= b1.y - pH)) {
            pX = b1.x - pW;
      }
    }
    

    // It is customary for object instantiated via new to have capitalized names
    function Block(x, y, w, h, color) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.color = color;
    }
    Block.prototype = {
        draw() {
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y, this.w, this.h);
        },
    };
    
    var b1 = new Block(500, 350, 100, 100, 'gray');
    b1.draw();
    

    const Block = (x, y, w, h) => ({x, y, w, h});// this method does not require new operator
    var b1 = Block(500, 350, 100, 100, 'gray');
    drawBlock(b1);
    function drawBlock(block) {
        ctx.fillStyle = block.color;
        ctx.fillRect(block.x, block.y, block.w, block.h);
    }
    

    const blockCommon = {
        draw() {
            ctx.fillStyle = this.color;
            ctx.fillRect(this.x, this.y, this.w, this.h);
        },
    };
    const Block = (x, y, w, h) => ({x, y, w, h, ...blockCommon});
    var b1 = Block(500, 350, 100, 100, 'gray');
    b1.draw();
    

    或十几种或更多创建对象的方法。

    【讨论】:

    • 感谢您告诉我这非常有用。但我也想知道是否有办法使碰撞成为全球性的。所以 b1.x 只是一个对象,但是有没有办法为每个对象调用变量,或者我需要为我制作的每个块编写碰撞代码?
    • @Loaphs 涵盖几乎所有语言的通用编程技巧。如果你发现自己重复了任何代码,那么你做错了,有更好的方法。创建一个带有两个参数p, b 的函数update,其中p 引用了一个封装pXpW 等的obj... 例如p.x, p.w 等,而b 是一个Block 类似对象函数看起来像 function update(p, b) { if ((p.x >= b.x - p.w) && (p.y >= b.y - p.h)) { p.x = b.x - p.w;} } 并且可以用于任何 blockp(无论是什么),您将永远不必再次编写该函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2018-12-10
    相关资源
    最近更新 更多