我有一个类似的继承链,Starship -> Capital -> Omega我希望能够从Omega类的对象中检索"Omega".
function Starship(){
}
function Capital(){
Starship.call(this);
}
Capital.prototype = Object.create(Starship.prototype);
function Omega(){
Capital.call(this);
}
Omega.prototype = Object.create(Capital.prototype);
var omega = new Omega();
omega instanceof Omega // true
omega instanceof Capital// true
omega instanceof Starship // true
omega.constructor.name // Starship
Run Code Online (Sandbox Code Playgroud)
有没有办法检索omega所属的最年轻的类,即"Omega"或者我应该添加类似于this.type = "Omega"Omega函数的东西?
我目前正在动画的过程中一些激光效果在画布上,目的是让我的游戏更有乐趣.
出于这个目的,我需要"激光"武器的绘图和动画火,就像星球大战一样.
到目前为止,我基本上只绘制一条红色或蓝色的短线,然后在它上面画一条更细的白色线条,这样就给人一种渐变的印象.我也用linecap ="round";
我目前的代码:
function drawProjectile(weapon, ox, oy, x, y){
var trailEnd = getPointInDirection(weapon.projSize*-2, getAngleFromTo({x: ox, y: oy}, {x: x, y: y}), x, y);
fxCtx.lineCap = "round";
fxCtx.beginPath(); //the background, wider beam
fxCtx.moveTo(x+cam.o.x, y+cam.o.y);
fxCtx.lineTo(trailEnd.x+cam.o.x, trailEnd.y+cam.o.y);
fxCtx.closePath();
fxCtx.strokeStyle = weapon.animColor //blue or red
fxCtx.lineWidth = weapon.projSize;
fxCtx.stroke();
fxCtx.globalAlpha = 0.5; // the inner, thin, white beam
fxCtx.strokeStyle = "white";
fxCtx.lineWidth = 2;
fxCtx.stroke();
fxCtx.globalAlpha = 1;
fxCtx.lineCap = "butt";
}
Run Code Online (Sandbox Code Playgroud)
有人可以建议如何改善我的激光束效果吗?
单击按钮后,我会在画布上制作一些动画。动画完成后,我想调用一个函数,该函数作为参数传递到requestAnimationFrame首先初始化的函数中。
这可能吗?
我尝试了各种方法,使用匿名函数等。
var anim = false;
var game = new Game();
function Game(){
this.turn = 1;
this.move = function(){
this.animate(game.advanceTurn);
}
this.advanceTurn = function(){
this.turn++;
}
this.animate = function(callback){
var done = false;
anim = window.requestAnimationFrame(game.animate);
// animation code here
if (done){
window.cancelAnimationFrame(anim);
anim = false;
callback();
}
}
Run Code Online (Sandbox Code Playgroud)
即单击按钮,制作动画,动画完成,调用 game.advanceTurn。
当记录提供的参数时,控制台首先会记录我的回调函数,然后将其替换为我认为是计时器测量的内容(符合 MDN 的描述rAF)。
class Unit {
constructor(){
}
}
var str = "Unit";
var a = new window[str](); // error
var b = new window["Unit"](); // error
var u = new Unit(); // works
(u instanceof Unit) // true
Run Code Online (Sandbox Code Playgroud)
我只是最近才在声明类方面切换到ES 6语法。我非常确定以前我可以实例化这样的类,但是自从我使用“类”语法以来,通过windowclassName实例化对象就不再起作用了。
我到底在这里想念什么?