【问题标题】:Property 'createVector' does not exist属性“createVector”不存在
【发布时间】:2021-09-04 07:36:57
【问题描述】:

我正在尝试使用带有 p5.js 的 Typescript 编写一些草图。我已经通过 npm 安装了 p5 和 @types/p5。

在我的草图中,我尝试使用 createVector 方法创建一个 Vector,但打字稿似乎找不到它。我只是收到错误“类型'typeof import(“local_path_stuff/node_modules/@types/p5/index.d.ts”)'上不存在属性'createVector'。”

有人对修复此错误有任何建议吗?

/// @ts-check
/// <reference path="../../node_modules/@types/p5/global.d.ts" />
import p5 from "p5";

export abstract class BaseEntity {
    protected pos: p5.Vector;
    protected id: number;
    protected entityType: number;
    constructor(id: number, entityType: number, pos?: p5.Vector){
        this.id = id;
        this.entityType = entityType;
        if(pos) {
            this.pos = pos;
        } else {
            this.pos = p5.createVector(0,0); //Error here
        }
    }
    abstract update(dt: number): void;
    abstract draw(s: p5): void
}

【问题讨论】:

    标签: typescript p5.js


    【解决方案1】:

    我自己想通了。我需要传入一个 P5 类型的实例。

    /// @ts-check
    /// <reference path="../../node_modules/@types/p5/global.d.ts" />
    import type p5 from "p5";
    
    export abstract class BaseEntity {
        protected pos: p5.Vector;
        protected id: number;
        protected entityType: number;
        constructor(s: p5, id: number, entityType: number, pos?: p5.Vector){
            this.id = id;
            this.entityType = entityType;
            if(pos) {
                this.pos = pos;
            } else {
                this.pos = s.createVector(0,0);
            }
        }
        abstract update(dt: number): void;
        abstract draw(s: p5): void
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-04
      • 1970-01-01
      • 2013-10-10
      • 2020-12-31
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多