0

I wrote some simple js functions in objects.js file, but I can't reach them in python script

When i paste all code from file to index.html, everything works fine

How to execute functions from file 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";
};
1pietras
  • 23
  • 6

2 Answers2

0

The code fails because the variable instances is not declared in objects.js.

It's not obvious what you want to do with

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

In objects.js, create only takes one argument. If you want to pass the object created by the constructor instance in objects.js, you can use this syntax in Brython code :

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

marqueemoon
  • 376
  • 2
  • 5
0

Function "create" created in a local js-scope. Try to

window.create = create

after function definition and it will be available through

pyjs.create
Vladyslav Savchenko
  • 1,282
  • 13
  • 10