我在objects.js文件中写了一些简单的js函数,但我无法在python脚本中找到它们
当我将所有代码从文件粘贴到index.html时,一切正常
如何从文件objects.js执行函数?
的index.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>
objects.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";
};
答案 0 :(得分:0)
代码失败,因为变量instances
未在objects.js中声明。
你想用
做什么并不明显
pyjs.create("instance",256,128)
在objects.js中,create
只接受一个参数。如果要在objects.js中传递构造函数instance
创建的对象,可以在Brython代码中使用此语法:
pyjs.create(pyjs.instance.new(256, 128));
答案 1 :(得分:0)
在本地js范围内创建的函数“ create”。尝试
window.create = create
在函数定义之后,它将通过
提供pyjs.create