首页 > 解决方案 > 不能在另一个脚本中使用脚本中的类

问题描述

我猜这是一个基本问题,但我找不到问题所在。我有两个脚本,一个“主”,一个包含一个基本类,我尝试在主脚本中实例化这个类,如下所示:

脚本.js

window.onload = function() {
    var car = new Card('Ressources/Sprites/Axel.png', [50, 50]); 
}

Card.js

class Card {
    constructor(path, position) {
        this.position = position; 
        this.texture = PIXI.Texture.from(sprite);
        this.card = PIXI.Sprite(texture); 
        this.setPosition(position);
    }
    
    function setPosition(position){
        card.x = position.x;
        card.y = position.y;
    }
}

索引.html

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Hello World</title>
    </head>
    <style>* {padding: 0; margin: 0}</style>
    <script src="Ressources/Libraries/pixi.min.js"></script>
    <script src="Scripts/Card.js"></script>
    <script src="script.js"></script>
    
    <body>
        
    </body>
</html>

在控制台中我有这个错误:

未捕获的 ReferenceError:卡未在 window.onload 中定义

标签: javascripthtmlclass

解决方案


推荐阅读