【问题标题】:Having trouble using instance mode使用实例模式时遇到问题
【发布时间】:2019-05-16 22:04:30
【问题描述】:

在我的代码中使用实例模式时出现错误。我的迷你游戏没有出现我不确定错误来自哪里。没有实例模式它工作正常,但我需要使用实例模式,以便我可以在另一个文件中引用此代码。我使用这个链接作为参考。 http://p5js.org/examples/instance-mode-instantiation.html

let sketch = function(p) {

    let blob;
    let blobs = [];
    let zoom = 1;
    let timer = 20;
    let hits = false;
    let score = 0;

    p.setup = function() {
        p.createCanvas(600, 600);
        blob = new Blob(0, 0, 64);
        for (let i = 0; i < 300; i++) {
            let x = p.random(-p.width,p.width);
            let y = p.random(-p.height,p.height);
            blobs[i] = new Blob(x, y, 15);
        }
    };

    p.draw = function() {
        p.background(0);

        p.translate(width/2, height/2);
        let newzoom = 64 / blob.r;
        p.zoom = p.lerp(zoom, newzoom, 0.1);
        p.scale(zoom);
        p.translate(-blob.pos.x, -blob.pos.y);

        for (var i = blobs.length-1; i >=0; i--) {
            blobs[i].show();
            if (blob.eats(blobs[i])) {
                blobs.splice(i, 1);
            }
        }

        if (frameCount % 60 == 0 && timer > 0) { // if the frameCount is divisible by 60, then a second has passed. it will stop at 0
            p.timer --;
        }

        if (timer == 0 && score >=250) {
            p.text("You Win", 0, 0);
            p.noLoop();
        }

        if (blob.eats){
            p.console.log("hit");
        }
        if (timer == 0 && score <= 250){
            p.text("You Lose", 0, 0);
            p.textSize(200);
            p.noLoop();
        }

        blob.show();
        blob.update();
    };
};
let myp5 = new p5(sketch);

class Blob {


     constructor(x, y, r) {
            this.pos = createVector(x, y);
  this.r = r; 
  this.vel = createVector(0,0);
    }
    show(p) {
        p.ellipse(this.pos.x, this.pos.y, this.r, this.r);
    }
    eats(other) {
      let d = p5.Vector.dist(this.pos, other.pos);
        if (d < this.r + other.r) {
      let sum = PI * this.r * this.r + PI * other.r * other.r;
      score ++;
      this.r = sqrt(sum / PI);
      //this.r += other.r;
      return true;
    } else {
      return false;
    }
  }

【问题讨论】:

    标签: javascript processing instance p5.js mode


    【解决方案1】:

    虽然widthheightframeCount 是画布的属性,但console 不是。

    p.translate(width/2, height/2);
    p.translate(p.width/2, p.height/2);

    if (frameCount % 60 == 0 &amp;&amp; timer &gt; 0) {
    if (p.frameCount % 60 == 0 &amp;&amp; timer &gt; 0) {

    console.log("hit");
    console.log("hit");

    我不知道Blob的实现。但是您必须将画布对象 (p) 传递给 show 方法和 let constructor

    变量score不能在Blob类中访问

    查看示例,我将建议的更改应用于您的原始代码:

    class Blob {
        constructor(p, x, y, r) {
            this.pos = p.createVector(x, y);
            this.r = r; 
            this.vel = p.createVector(0,0);
        }
        show(p) {
            p.ellipse(this.pos.x, this.pos.y, this.r, this.r);
        }
        eats(other) {
            let d = this.pos.dist(other.pos);
            if (d < this.r + other.r) {
                let sum = Math.pi * this.r * this.r + Math.pi * other.r * other.r;
                this.r = Math.sqrt(sum / Math.pi);
                //this.r += other.r;
                return true;
            } else {
                return false;
            }
        }
        update() {
            // ...
        }
    }
    
    let sketch = function(p) {
        let blob;
        let blobs = [];
        let zoom = 1;
        let timer = 20;
        let hits = false;
        let score = 0;
    
        p.setup = function() {
            p.createCanvas(600, 600);
            blob = new Blob(p, 0, 0, 64);
            for (let i = 0; i < 300; i++) {
                let x = p.random(-p.width,p.width);
                let y = p.random(-p.height,p.height);
                blobs[i] = new Blob(p, x, y, 15);
            }
        };
    
        p.draw = function() {
            p.background(0);
    
            p.translate(p.width/2, p.height/2);
            let newzoom = 64 / blob.r;
            p.zoom = p.lerp(zoom, newzoom, 0.1);
            p.scale(zoom);
            p.translate(-blob.pos.x, -blob.pos.y);
    
            for (var i = blobs.length-1; i >=0; i--) {
                blobs[i].show(p);
                if (blob.eats(blobs[i])) {
                    score ++;
                    blobs.splice(i, 1);
                }
            }
    
            if (p.frameCount % 60 == 0 && timer > 0) { 
              p.timer --;
            }
    
            if (timer == 0 && score >=250) {
                p.text("You Win", 0, 0);
                p.noLoop();
            }
    
            if (blob.eats){
                console.log("hit"); 
            }
            if (timer == 0 && score <= 250){
                p.text("You Lose", 0, 0);
                p.textSize(200);
                p.noLoop();
            }
    
            blob.show(p);
            blob.update();
        };
    };
    let myp5 = new p5(sketch)
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"&gt;&lt;/script&gt;

    【讨论】:

    • 我添加了“blob”的内容。 createVector 实际上应该是 p.createVector 吗?
    • @metrocode 它必须是let d = this.pos.dist(other.pos);。查看答案的更改,因为还有更多问题。
    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2019-05-06
    • 2016-08-16
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多