0

我在 objects.js 文件中编写了一些简单的 js 函数,但我无法在 python 脚本中访问它们

当我将文件中的所有代码粘贴到 index.html 时,一切正常

如何从文件 objects.js 执行函数?

索引.html:

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<link rel="stylesheet" href="doc/doc_brython.css">
</head>

<body onload="brython({debug:1, cache:'none'})">
<canvas id="spriteCanvas" width="640" height="480" style="border:1px     solid #FF8844"></canvas>

<script type="text/javascript" src="objects.js"></script>
<script type="text/javascript" src="src/brython.js"></script>

<script type="text/python">
from browser import window
from browser import document as doc
pyjs=window

pyjs.create("instance",256,128);
pyjs.create("instance",256,256);
pyjs.create("player",128,256);
</script>
</body>
</html>

对象.js:

console.log("Brytan v0.1");

var canvas= document.getElementById("spriteCanvas");

function create(inst)
{instances.push(inst); instances[instances.length-1].id=instances.length; return instances[instances.length-1];}    

function instance(x,y)
{
canvas = document.getElementById("spriteCanvas");
context = canvas.getContext("2d");
this.context=context;
this.canv=canvas
this.img = new Image();
this.imagePath = "";

this.id=0;
this.x=x;
this.y=y;
this.w=32;
this.h=32;

this.update=function()
{   
    /// Mozna nadpisac ta funkcje w innych obiektach
}
this.draw=function()
{
    this.img.onload = drawImage(
    this.context, this.img, 
    this.x, this.y,
    this.w, this.h
    );
}

this.destroy= function() // Swiec GC nad jego dusza
{instances[this.id-1]=0;}

this.img.src="./pieniazek.png";
};
4

2 回答 2

0

在本地 js-scope 中创建的函数“create”。尝试

window.create = create

在函数定义之后,它将通过

pyjs.create
于 2019-04-15T13:42:56.453 回答
0

代码失败,因为该变量instances未在 objects.js 中声明。

你想做什么并不明显

pyjs.create("instance",256,128)

在 objects.js 中,create只接受一个参数。如果要instance在 objects.js 中传递构造函数创建的对象,可以在 Brython 代码中使用此语法:

pyjs.create(pyjs.instance.new(256, 128));

于 2017-01-15T08:48:03.223 回答