小编use*_*806的帖子

Javascript类继承和所有继承的名称?

我有一个类似的继承链,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函数的东西?

javascript inheritance

8
推荐指数
1
解决办法
55
查看次数

在画布上画出漂亮的激光(星球大战)

我目前正在动画的过程中一些激光效果在画布上,目的是让我的游戏更有乐趣.

出于这个目的,我需要"激光"武器的绘图和动画火,就像星球大战一样.

到目前为止,我基本上只绘制一条红色或蓝色的短线,然后在它上面画一条更细的白色线条,这样就给人一种渐变的印象.我也用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)

有人可以建议如何改善我的激光束效果吗?

javascript html5 canvas

3
推荐指数
1
解决办法
963
查看次数

requestAnimationFrame 带有回调函数?

单击按钮后,我会在画布上制作一些动画。动画完成后,我想调用一个函数,该函数作为参数传递到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)。

javascript

2
推荐指数
1
解决办法
3195
查看次数

用window [string]()实例化一个类对象

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实例化对象就不再起作用了。

我到底在这里想念什么?

javascript class

2
推荐指数
1
解决办法
34
查看次数

标签 统计

javascript ×4

canvas ×1

class ×1

html5 ×1

inheritance ×1