javascript - What am I missing for JS object literal with method? -


I am trying to create a simple object with one method in it, which in this case is a moving block on the canvas . I have written the following code code using the tutorial on

  var block = {color: "# 000000", x: 0, y: 0, width: 30, height: 30 : Function () {ctx.fillStyle = this.color; Ctx.fillRect (this.x, this.that, this.heath, this .hit); }}; Function Update () {block.x = block.x + 30; Block.y = block.a + 30; } Function draw () {var x = document.getElementById ("space"); Var ctx = x.getContext ("2d"); Ctx.clearRect (0, 0, 900, 600); Block.draw (); }  

For some reason it will not work this way, while this will happen when I will write code in the Draw method in the Draw () function. I'm probably missing something, but what?

blocks , and its members have ever ctx , which is local to your attract () function.

Make either global, or pass it to Block.draw :

  var block = {// ... Draw: function ( Ctx) {// ...}}; // ... block.draw (ctx);  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -