简体   繁体   English

Javascript画布游戏-移动时倾斜太空飞船

[英]Javascript Canvas Game - Tilting spaceship when moving

I am a beginner with javascript, trying to make a javascript space game where you move a spaceship to avoid asteroids. 我是javascript的初学者,试图制作一个javascript太空游戏,在该游戏中,您将飞船移动到避开小行星的位置。

Here's a preview link. 这是一个预览链接。 It should work on desktop or mobile: http://dylanmadigan.net/_digital-work/game-2/5-tilt/index.html 它应该可以在台式机或移动设备上运行: http : //dylanmadigan.net/_digital-work/game-2/5-tilt/index.html

I want the ship to tilt slightly to the left or right when moving. 我希望船在移动时向左或向右稍微倾斜。 Is it better to tilt the ship with sprite sheet,or to actually rotate the graphic in javascript? 最好用Sprite Sheet倾斜船,或者实际旋转javascript中的图形?

I am currently doing this with a sprite sheet. 我目前正在用一张精灵表来做。 The ship animation is two frames (to make booster flames flicker). 船舶动画为两帧(以使助燃器火焰闪烁)。 I have two frames dedicated to each straight, left, and right, on a single sprite sheet. 我在单个子画面上分别有两个分别用于直线,左和右的帧。 view player sprite sheet here This appears to result in flickering when the sprite moves left and right. 在此处查看播放器的精灵表 。当精灵左右移动时,这似乎导致闪烁。 And every once in a while it will just disappear. 而且每隔一段时间它就会消失。 I'm not sure why. 我不知道为什么。

The sprite sheet is preloaded, and I thought that would prevent flickering. Sprite表已预加载,我认为这样可以防止闪烁。 It did, until I decided to add the 4 more frames for the tilted spaceship. 的确如此,直到我决定为倾斜的宇宙飞船再添加4个镜架。

Would it be better if I used javascript to rotate the graphic instead of a sprite sheet? 如果我使用JavaScript旋转图形而不是Sprite工作表会更好吗? I just thought the sprite sheet would be easier. 我只是认为精灵表会更容易。

It does not matter how you rotate the ship, via a drawn sprite or rotated in the canvas. 通过绘制的精灵或在画布中旋转,如何旋转船都没有关系。 Each method has its advantages and disadvantages. 每种方法都有其优点和缺点。

Rotating a sprite about its center 围绕其中心旋转精灵

To render a sprite that is scaled,and rotated about its center, and has a alpha fade (because...? why not) 渲染经过缩放并围绕其中心旋转并具有alpha渐变的精灵(因为...?为什么不这样做)

function drawSprite(ctx,img,x,y,scale,rot,alpha){
    ctx.setTransform(scale,0,0,scale,x,y); // Position and scale
    ctx.rotate(rot); // rotate
    ctx.globalAlpha = alpha;  // set alpha
    ctx.drawImage(img,-img.width/2,-img.height/2); // draw sprite offset half its size
}

When done drawing sprites you will need to reset the transform and alpha 完成绘制精灵后,您将需要重置变换和Alpha

ctx.setTransform(1,0,0,1,0,0); // sets default transform
ctx.globalAlpha = 1.0; // resets alpha

You can just have one ship sprite and make the engines a separate sprite (just remove the ship and keep the same size to make positioning easy) Then you can control the engine glow independently of the ship using the alpha to fade in and out. 您可以只拥有一个飞船精灵,并使引擎成为一个单独的精灵(只需移开飞船并保持相同的大小即可轻松进行定位),然后可以使用alpha淡入和淡出来独立于飞船控制引擎发光。 You can also set the ctx.globalCompositeOperation = "lighter" or "screen" to get a better engine glow. 您还可以将ctx.globalCompositeOperation = "lighter""screen"设置为更好的引擎发光效果。 Reset the comp mode to "source-over" for normal rendering. 将补偿模式重置为"source-over"以进行常规渲染。

You should also look into using requestAnimationFrame and a single game loop rather than use all the setTimeouts. 您还应该研究使用requestAnimationFrame和单个游戏循环,而不是使用所有setTimeouts。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM