未定義のJavaScriptとOOPのプロパティを読み取ることができません

マシュー

「gameLoop」というプロトタイプメソッドを持つ「Game」というオブジェクト(関数)があります。このループを一定の間隔で呼び出す必要があるので、これを実行しようとします。

setInterval(game.gameLoop,setIntervalAmount);

しかし、受け取る "TypeError: Cannot read property 'clearRect' of undefined(…)"

プロトタイプメソッドは次のとおりです。

Game.prototype.gameLoop = function()
{
    this.context.clearRect(0,0,this.canvas.width, this.canvas.height);
    this.context.save();


    this.context.translate(this.canvas.width/2, this.canvas.height/2);
    this.context.scale(this.camera.scale,this.camera.scale);
    this.context.rotate(this.camera.rotate);
    this.context.translate(this.camera.x,this.camera.y);


    for(var i=0;i<this.objects.length;i++)
    {
        this.objects[i].updateSprite();
        this.objects[i].drawSprite(this.context);         
    }
    this.context.restore();
}

私はまだJavascriptのオブジェクト指向プログラミングを理解するのに苦労しています。関数が単なる通常の関数であり、ゲームオブジェクトを渡した作業バージョンがありました。何か案は?

ちなみに、ここに役立つかもしれないいくつかの追加のコードがあります。

function Sprite(imgg,w,h)
{
    this.img = imgg;
    this.x = 350;//Math.random()*700;
    this.y = 350;//Math.random()*700;
    this.vx = 0;//Math.random()*8-4;
    this.vy = 0;//Math.random()*8-4;
    this.width = w;
    this.height = h;
    this.rotatespeed = 0.01;
    this.rotate = 40;

}
Sprite.prototype.drawSprite = function(ctx)
{

    ctx.save();


    ctx.translate(this.x,this.y);
    ctx.rotate(this.rotate);


    ctx.drawImage(this.img,0,0,this.img.width,this.img.height,-this.width/2,-this.height/2,this.width,this.height);


    ctx.restore();
}
Sprite.prototype.updateSprite = function()
{
    this.x += this.vx;
    this.y += this.vy;

    this.rotate += this.rotatespeed;


    if(this.x > 700)
        this.vx = -this.vx;
    if(this.x < 0)
        this.vx = -this.vy;

    if(this.y > 700)
        this.vy = -this.vy;
    if(this.y < 0)
        this.vy = -this.vy;
}
Sprite.prototype.mouseEventListener = function(evt, type)
{
    console.log("Hello");
}


//------------------------------------------
//GLOBAL VARIALBES

var setIntervalAmount = 30;
var scrollAmount = 0.5;
var game;
function Game()
{
    this.camera = new Object();
    this.camera.x = -350;
    this.camera.y = -350;
    this.camera.scale = 1;

    this.camera.rotate = 0;

    this.canvas = document.createElement("canvas");
    document.body.appendChild(this.canvas);
    this.canvas.id="mycanvas";
    this.canvas.width = 700;
    this.canvas.height = 700;
    this.context = this.canvas.getContext("2d");    

    var ctx = this.context;

    ctx.canvas.addEventListener('mousemove', function(event){
        var mouseX = event.clientX - ctx.canvas.offsetLeft;
        var mouseY = event.clientY - ctx.canvas.offsetTop;

        var canvasX = mouseX * ctx.canvas.width / ctx.canvas.clientWidth;
        var canvasY = mouseY * ctx.canvas.height / ctx.canvas.clientHeight;


        //console.log(canvasX+" | "+canvasY);

    });

    this.objects = new Array();  


}
Game.prototype.handleMouse = function(evt,type)
{
    for(var i=0;i<this.objects.length;i++)
    {
        this.objects[i].mouseEventListener(evt,type);
    }
};

Game.prototype.gameLoop = function()
{
    this.context.clearRect(0,0,this.canvas.width, this.canvas.height);
    this.context.save();


    this.context.translate(this.canvas.width/2, this.canvas.height/2);
    this.context.scale(this.camera.scale,this.camera.scale);
    this.context.rotate(this.camera.rotate);
    this.context.translate(this.camera.x,this.camera.y);


    for(var i=0;i<this.objects.length;i++)
    {
        this.objects[i].updateSprite();
        this.objects[i].drawSprite(this.context);         
    }
    this.context.restore();
}


/*Game.prototype.drawGame = function()
{
    var gameLoop = setInterval(function(){

        this.context.clearRect(0,0,this.canvas.width, this.canvas.height);
        this.context.save();


        this.context.translate(this.canvas.width/2, this.canvas.height/2);
        this.context.scale(this.camera.scale,this.camera.scale);
        this.context.rotate(this.camera.rotate);
        this.context.translate(this.camera.x,this.camera.y);


        for(var i=0;i<this.objects.length;i++)
        {
            this.objects[i].updateSprite();
            this.objects[i].drawSprite(this.context);         
        }
        this.context.restore();

    },setIntervalAmount); 

}*/



function mouseWheelListener() 
{
    var evt = window.event;
    console.log(evt.wheelDelta);
    if(evt.wheelDelta < 0)
        game.camera.scale /= (1+scrollAmount);
    else 
        game.camera.scale *= (1+scrollAmount);

}
function mouseDownListener()
{
    var evt = window.event;
    var type = "down"
    game.handleMouse(evt,type);
}
function mouseUpListener()
{
    var evt = window.event;
    var type = "up"
    game.handleMouse(evt,type);
}
function mouseMoveListener()
{
    var evt = window.event;
    var type = "move"
    game.handleMouse(evt,type);
}

//------------------

window.addEventListener('load',function(event){startgame();});

var dog = new Image();
dog.src = "grid.gif";

function startgame()
{
    game = new Game();

    for(var i=0;i<1;i++)
        game.objects.push(new Sprite(dog,250,250));


    setInterval(game.gameLoop,setIntervalAmount);

    document.getElementById("mycanvas").addEventListener("wheel", mouseWheelListener);
    document.getElementById("mycanvas").addEventListener("mousedown", mouseDownListener);
    document.getElementById("mycanvas").addEventListener("mouseup", mouseUpListener);
    document.getElementById("mycanvas").addEventListener("mousemove", mouseMoveListener);
}
Sreekanth

唯一の問題は、コードで、gameloopメソッドを実行しているコンテキストです。

通常、setInterval、setTimeout関数は、グローバル/ウィンドウコンテキストで実行されます。したがって、メソッド内でこれを指定すると、オブジェクトに対して実行している場合でも、技術的にはグローバルコンテキストを参照していることになります。

したがって、問題が発生しないようにするために、setIntervalの最初の引数として、メソッド参照の代わりに必要な関数を実行するメソッドを常に用意してください。

setInterval(function(){/* 
    game.gameLoop()
*/}, 1000);

このように、グローバルコンテキストでsetInterval関数を実行していますが、ゲームオブジェクトのメソッドを明示的に呼び出しています。

function Sprite(imgg, w, h) {
  this.img = imgg;
  this.x = 350; //Math.random()*700;
  this.y = 350; //Math.random()*700;
  this.vx = 0; //Math.random()*8-4;
  this.vy = 0; //Math.random()*8-4;
  this.width = w;
  this.height = h;
  this.rotatespeed = 0.01;
  this.rotate = 40;

}
Sprite.prototype.drawSprite = function(ctx) {

  ctx.save();


  ctx.translate(this.x, this.y);
  ctx.rotate(this.rotate);


  ctx.drawImage(this.img, 0, 0, this.img.width, this.img.height, -this.width / 2, -this.height / 2, this.width, this.height);


  ctx.restore();
}
Sprite.prototype.updateSprite = function() {
  this.x += this.vx;
  this.y += this.vy;

  this.rotate += this.rotatespeed;


  if (this.x > 700)
    this.vx = -this.vx;
  if (this.x < 0)
    this.vx = -this.vy;

  if (this.y > 700)
    this.vy = -this.vy;
  if (this.y < 0)
    this.vy = -this.vy;
}
Sprite.prototype.mouseEventListener = function(evt, type) {
  //console.log("Hello");
}


//------------------------------------------
////GLOBAL VARIALBES

var setIntervalAmount = 200;
var scrollAmount = 0.5;
var game;

function Game() {
  this.camera = new Object();
  this.camera.x = -350;
  this.camera.y = -350;


  this.camera.scale = 1;

  this.camera.rotate = 0;

  this.canvas = document.createElement("canvas");
  document.body.appendChild(this.canvas);
  this.canvas.id = "mycanvas";
  this.canvas.width = 700;
  this.canvas.height = 700;
  this.context = this.canvas.getContext("2d");

  var ctx = this.context;

  ctx.canvas.addEventListener('mousemove', function(event) {
    var mouseX = event.clientX - ctx.canvas.offsetLeft;
    var mouseY = event.clientY - ctx.canvas.offsetTop;

    var canvasX = mouseX * ctx.canvas.width / ctx.canvas.clientWidth;
    var canvasY = mouseY * ctx.canvas.height / ctx.canvas.clientHeight;


    //console.log(canvasX+" | "+canvasY);

  });

  this.objects = new Array();


}
Game.prototype.handleMouse = function(evt, type) {
  for (var i = 0; i < this.objects.length; i++) {
    this.objects[i].mouseEventListener(evt, type);
  }
};

Game.prototype.gameLoop = function() {
  this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  this.context.save();


  this.context.translate(this.canvas.width / 2, this.canvas.height / 2);
  this.context.scale(this.camera.scale, this.camera.scale);
  this.context.rotate(this.camera.rotate);
  this.context.translate(this.camera.x, this.camera.y);


  for (var i = 0; i < this.objects.length; i++) {
    this.objects[i].updateSprite();
    this.objects[i].drawSprite(this.context);
  }
  this.context.restore();
}


/*Game.prototype.drawGame = function()
{
    var gameLoop = setInterval(function(){

        this.context.clearRect(0,0,this.canvas.width, this.canvas.height);
        this.context.save();


        this.context.translate(this.canvas.width/2, this.canvas.height/2);
        this.context.scale(this.camera.scale,this.camera.scale);
        this.context.rotate(this.camera.rotate);
        this.context.translate(this.camera.x,this.camera.y);


        for(var i=0;i<this.objects.length;i++)
        {
            this.objects[i].updateSprite();
            this.objects[i].drawSprite(this.context);         
        }
        this.context.restore();

    },setIntervalAmount); 

}*/



function mouseWheelListener() {
  var evt = window.event;
  console.log(evt.wheelDelta);
  if (evt.wheelDelta < 0)
    game.camera.scale /= (1 + scrollAmount);
  else
    game.camera.scale *= (1 + scrollAmount);

}

function mouseDownListener() {
  var evt = window.event;
  var type = "down"
  game.handleMouse(evt, type);
}

function mouseUpListener() {
  var evt = window.event;
  var type = "up"
  game.handleMouse(evt, type);
}

function mouseMoveListener() {
  var evt = window.event;
  var type = "move"
  game.handleMouse(evt, type);
}

//------------------

window.addEventListener('load', function(event) {
  startgame();
});

var dog = new Image();
dog.src = "https://i.stack.imgur.com/W0mIA.png";

function startgame() {
  game = new Game();

  for (var i = 0; i < 1; i++)
    game.objects.push(new Sprite(dog, 250, 250));


  setInterval(function() {
    game.gameLoop();
  }, setIntervalAmount);


  document.getElementById("mycanvas").addEventListener("wheel", mouseWheelListener);
  document.getElementById("mycanvas").addEventListener("mousedown", mouseDownListener);
  document.getElementById("mycanvas").addEventListener("mouseup", mouseUpListener);
  document.getElementById("mycanvas").addEventListener("mousemove", mouseMoveListener);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

JavaScriptのpromiseで未定義のプロパティ 'then'を読み取ることができません

分類Dev

javascriptエラーで未定義のプロパティ 'preventDefault'を読み取ることができません

分類Dev

javascriptで未定義のプロパティ 'clearRect'を読み取ることができません

分類Dev

エラー:未定義のプロパティ 'nota'を読み取ることができません。(Javascript)

分類Dev

Javascriptは未定義のプロパティ 'x'を読み取ることができません

分類Dev

Javascriptは未定義のプロパティ 'x'を読み取ることができませんか?

分類Dev

JavaScript-未定義のプロパティを読み取ることができません

分類Dev

JavaScript-未定義のプロパティ 'toLowerCase'を読み取ることができません

分類Dev

未定義のプロパティ「アクティビティ」を読み取ることができません

分類Dev

未定義のプロパティを読み取ることができません。定義する方法は?

分類Dev

DashboardComponentで未定義のプロパティ「パイプ」を読み取ることができません

分類Dev

未定義のプロパティ「ディスパッチ」を読み取ることができません。React

分類Dev

Uncaught TypeError:GatsbyとgraphQlで未定義のプロパティ 'data'を読み取ることができません

分類Dev

NodejsとSequelizeで未定義のプロパティ「findOne」を読み取ることができません

分類Dev

Reduxの取得は未定義のプロパティマップを読み取ることができません

分類Dev

分度器とappium-未定義のプロパティを読み取ることができません

分類Dev

ReactJSのNavLinkの未定義のプロパティ「場所」を読み取ることができません

分類Dev

未定義のAPOLLOGRAPHQLの「then」のプロパティを読み取ることができません

分類Dev

window.onscrollで未定義の「replace」のプロパティを読み取ることができません

分類Dev

TypeError:insertOneのinsertDocumentsで未定義のプロパティ '_id'を読み取ることができません

分類Dev

WordpressのsmoothState.jsで未定義のプロパティ 'addClass'を読み取ることができません

分類Dev

ReactjsToastrを使用して未定義のプロパティの成功を読み取ることができません

分類Dev

未定義のプロパティ「ヘルパー」を読み取ることができません

分類Dev

TypeError:未定義のプロパティ 'コンパイル'を読み取ることができません

分類Dev

TypeError:未定義のプロパティ 'パラメータ'を読み取ることができません

分類Dev

Angular:未定義のプロパティ「コントロール」を読み取ることができません

分類Dev

ReactJSテスト:未定義のプロパティ 'have'を読み取ることができません

分類Dev

TypeError:未定義のReact、Ant選択のプロパティ「値」を読み取ることができません

分類Dev

TypeError:未定義の角度のプロパティ 'childNodes'を読み取ることができません

Related 関連記事

  1. 1

    JavaScriptのpromiseで未定義のプロパティ 'then'を読み取ることができません

  2. 2

    javascriptエラーで未定義のプロパティ 'preventDefault'を読み取ることができません

  3. 3

    javascriptで未定義のプロパティ 'clearRect'を読み取ることができません

  4. 4

    エラー:未定義のプロパティ 'nota'を読み取ることができません。(Javascript)

  5. 5

    Javascriptは未定義のプロパティ 'x'を読み取ることができません

  6. 6

    Javascriptは未定義のプロパティ 'x'を読み取ることができませんか?

  7. 7

    JavaScript-未定義のプロパティを読み取ることができません

  8. 8

    JavaScript-未定義のプロパティ 'toLowerCase'を読み取ることができません

  9. 9

    未定義のプロパティ「アクティビティ」を読み取ることができません

  10. 10

    未定義のプロパティを読み取ることができません。定義する方法は?

  11. 11

    DashboardComponentで未定義のプロパティ「パイプ」を読み取ることができません

  12. 12

    未定義のプロパティ「ディスパッチ」を読み取ることができません。React

  13. 13

    Uncaught TypeError:GatsbyとgraphQlで未定義のプロパティ 'data'を読み取ることができません

  14. 14

    NodejsとSequelizeで未定義のプロパティ「findOne」を読み取ることができません

  15. 15

    Reduxの取得は未定義のプロパティマップを読み取ることができません

  16. 16

    分度器とappium-未定義のプロパティを読み取ることができません

  17. 17

    ReactJSのNavLinkの未定義のプロパティ「場所」を読み取ることができません

  18. 18

    未定義のAPOLLOGRAPHQLの「then」のプロパティを読み取ることができません

  19. 19

    window.onscrollで未定義の「replace」のプロパティを読み取ることができません

  20. 20

    TypeError:insertOneのinsertDocumentsで未定義のプロパティ '_id'を読み取ることができません

  21. 21

    WordpressのsmoothState.jsで未定義のプロパティ 'addClass'を読み取ることができません

  22. 22

    ReactjsToastrを使用して未定義のプロパティの成功を読み取ることができません

  23. 23

    未定義のプロパティ「ヘルパー」を読み取ることができません

  24. 24

    TypeError:未定義のプロパティ 'コンパイル'を読み取ることができません

  25. 25

    TypeError:未定義のプロパティ 'パラメータ'を読み取ることができません

  26. 26

    Angular:未定義のプロパティ「コントロール」を読み取ることができません

  27. 27

    ReactJSテスト:未定義のプロパティ 'have'を読み取ることができません

  28. 28

    TypeError:未定義のReact、Ant選択のプロパティ「値」を読み取ることができません

  29. 29

    TypeError:未定義の角度のプロパティ 'childNodes'を読み取ることができません

ホットタグ

アーカイブ