与绘图到画布等功能挂钩

发布于 2024-12-22 16:16:40 字数 1358 浏览 1 评论 0原文

大家好,在另一个关于函数推送的问题中 -这里 然后我得到了这段代码 -

var postDrawHooks = [];
var draw = function(){
// do stuff
postDrawHooks.forEach(function(hook){hook()});
}

var playerUpdate = function(){...};
postDrawHooks.push(playerUpdate);

效果非常好,并且允许您将外部函数推入另一个函数(例如游戏循环),如果您想向对象添加更新,则无需为每个游戏编辑 engine.js 文件推动钩子。无论如何,我需要一种方法来推动绘图,如 Context2D.drawImage(blah blah blah);现在,当我尝试像上面那样将其推到下面时,我会收到尝试从 null 绘制的错误。

post_draw_render.push(context2D.drawImage(player.tilesheet, player.frameX,player.frameY, 46, 45, player.PosX, player.PosY+1, 46, 45));

我什至无法在绘图函数之外的任何地方使用 Context2D.drawImage,而且我不知道为什么有任何想法? 引擎 -

//engine vars//
function init() {
canvas = document.getElementById('canvas');
context2D = canvas.getContext('2d');
setInterval(draw, 1000/FPS);
}
function draw() {
post_draw_function.forEach(function(hook){hook()});
gameloop();
context2D.clearRect(0, 0, canvas.width, canvas.height);
post_draw_render.forEach(function(hook){hook});
//Basic Draw - context2D.drawImage(playerImg, player.PosX, player.PosY);
//Tilesheet draw - context2D.drawImage(tilesheet, sx, sy, sw, sh, dx, dy, dw, dh);
context2D.fillStyle = "white";
context2D.font = 'bold 25px Times New Roman';
}

Hey guys In another question about function pushing-Here
I was then given this code -

var postDrawHooks = [];
var draw = function(){
// do stuff
postDrawHooks.forEach(function(hook){hook()});
}

var playerUpdate = function(){...};
postDrawHooks.push(playerUpdate);

Which works very nicely, and allows you to push an outide function into another one like your game loop without having to edit your engine.js file for every game if you want to add an update to an object just push the hook. Anyways I need a way to push drawing like Context2D.drawImage(blah blah blah); now when I try to push this like above below I get the error trying to draw from null.

post_draw_render.push(context2D.drawImage(player.tilesheet, player.frameX,player.frameY, 46, 45, player.PosX, player.PosY+1, 46, 45));

I can't even use Context2D.drawImage anywhere oustide of my draw function and I don't know why any Ideas?
engine -

//engine vars//
function init() {
canvas = document.getElementById('canvas');
context2D = canvas.getContext('2d');
setInterval(draw, 1000/FPS);
}
function draw() {
post_draw_function.forEach(function(hook){hook()});
gameloop();
context2D.clearRect(0, 0, canvas.width, canvas.height);
post_draw_render.forEach(function(hook){hook});
//Basic Draw - context2D.drawImage(playerImg, player.PosX, player.PosY);
//Tilesheet draw - context2D.drawImage(tilesheet, sx, sy, sw, sh, dx, dy, dw, dh);
context2D.fillStyle = "white";
context2D.font = 'bold 25px Times New Roman';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-12-29 16:16:40

您推入数组的对象不是函数,而是调用drawImage 函数的返回值。

我想你想要

post_draw_render.push(function () {
  context2D.drawImage(player.tilesheet, player.frameX,player.frameY, 46, 45, player.PosX, player.PosY+1, 46, 45);
});

The object you are pushing into the array is not a function, it's the return value of a call to the drawImage function.

I think you want

post_draw_render.push(function () {
  context2D.drawImage(player.tilesheet, player.frameX,player.frameY, 46, 45, player.PosX, player.PosY+1, 46, 45);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文