我在 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";
};