简体   繁体   English

带有javascript和html5的“背景”游戏动画

[英]“Background” Game animations with javascript and html5

Suppose my base class is the following: 假设我的基类如下:

function Tile(obj) {
    //defaults (alot of variables here like colors and opacity)
}
Tile.prototype.Draw = function () {
    ctx.fillStyle = "rgba(" + this.Red + "," + this.Green + "," + this.Blue + "," + this.Opacity + ")";
    ctx.fillRect(this.X, this.Y, this.Width, this.Height);
};

my class that inherits from the Tile class 我从Tile类继承的类

Apple.prototype = new Tile();
Apple.prototype.constructor = Apple;
function Apple(obj) {
    Tile.apply(this, arguments);
}

So what I would like to do with my apple objects is have its opacity fluctuate between 0 and 1 so that it constantly fades in and out but I would like this to happen independently of the 'game loop'. 因此,我想对我的苹果对象执行的操作是使其不透明度在0到1之间波动,以便它不断地淡入和淡出,但是我希望这种情况独立于“游戏循环”发生。 (So that the fading animation is smooth regardless of the game speed) (因此,无论游戏速度如何,衰落动画都是平滑的)

    function StartGameLoop() {
        console.log("*** Starting Game ***");
        gameLoop = setInterval(Game, gameSpeed);
    }

    function Game() {
        if (!IS_GAME_ON) {                          // Did the game end?
            EndGame();
        } else if (!PAUSE) {                        // Is the game not paused?
            console.log("Tick");
            Logic();                                // do any math
        }
    }

I can't wrap my head around how to do this but I had an idea of putting a setInterval in the Apple constructor that calls the draw function over and over again but I can't get it to work. 我不能全神贯注于如何执行此操作,但是我有一个想法,将setInterval放在Apple构造函数中,该构造函数一遍又一遍地调用draw函数,但是我无法使其正常工作。 Like this: 像这样:

Apple.prototype = new Tile();
Apple.prototype.constructor = Apple;
function Apple(obj) {
    var AnimationDirection;
    var animate = setInterval(this.Draw, 50);
    Tile.apply(this, arguments);
}
Apple.prototype.Draw = function () {
    ctx.fillStyle = "rgba(" + this.Red + "," + this.Green + "," + this.Blue + "," + this.Opacity + ")";
    ctx.fillRect(this.X, this.Y, this.Width, this.Height);

    if (this.Opacity <= 0) {
        this.AnimationDirection = 0.1;
    }
    else if (this.Opacity >= 1) {
        this.AnimationDirection = -0.1;
    }

    this.Opacity += this.AnimationDirection;
};

It works as you'd expect when the first Apple.Draw() is called but the calls from the setInterval are not working properly. 当调用第一个Apple.Draw()但setInterval的调用不能正常工作时,它可以按您期望的那样工作。 Any ideas? 有任何想法吗?

(PS: Also the two ctx lines in the Draw function are repeated in both the Tile and the Apple classes, is there any way I can kick it to the Tile parent for the filling instead of having the code duplication?) (PS:在Tile和Apple类中都重复了Draw函数中的两行ctx行,有什么办法可以将它踢到Tile父级进行填充而不是重复代码吗?)

I believe the cause is that when the Draw() function fires as part of the setInterval call, this isn't what you think it is. 我相信原因是当Draw()函数作为setInterval调用的一部分触发时, this不是您认为的那样。

Instead, use a variable inside your constructor to store the value of this when it refers to the Apple object created, and use an anonymous function for setInterval() to be able to refer to that new variable and call the Draw() function correctly on it, eg something like: 相反,使用一个变量的构造函数中存储的值this时候它指的是创建苹果的对象,并使用匿名函数setInterval()以便能够指的是新的变量,并调用Draw()正确的函数它,例如:

function Apple(obj) {
    var AnimationDirection, me = this;
    var animate = setInterval(function() {
        me.Draw();
    }, 50);
    Tile.apply(this, arguments);
}

Since you are now calling the Draw() method on an Apple object (rather than the global window), this will correctly refer to that object. 由于您现在要在Apple对象(而不是全局窗口)上调用Draw()方法, this它将正确地引用该对象。

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

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