Jump to content

Towerdefense laggy minions


Fluppi
 Share

Recommended Posts

Hey,

I'm bulding a Towerdefensgame and my minions are moving very laggy. Im using tweens. Always if a tween is complete the next tween will be added.

The Problem is, that the way of the minion always can be changed, so I have to set the new tween always if the minion reached the new field.

Also the first wave is more "smooth" than the other waves..

Phaser-Version: v2.6.1

Game-Demo: http://darkfluppi.de/td/

Code for moving the minions:

function moveDatMinion(minion) {
    if (!minion.data.moving) {
        minion.data.moving = true;
        var diff = {
            x: (minion.width / 2),
            y: (minion.height / 2)
        };
        if (minion.data.fieldsSlowed > 0) {
            minion.data.fieldsSlowed--;
        } else {
            minion.data.minionSpeed = minion.data.minionFixSpeed;
            minion.data.slowed = false;
        }
        var pos = {
            x: Math.floor(getPosition(minion.position.x - 32)),
            y: Math.floor(getPosition(minion.position.y - 32))
        };

        var posCoord = {
            x: minion.position.x - 32,
            y: minion.position.y - 32
        };

        if (!(pos.x === 11 && pos.y === 5) && minion.alive) {
            var way = {
                u: 999,
                d: 999,
                l: 999,
                r: 999
            };
            var goto;
            if ((pos.x - 1) > -1) {
                way.l = fieldDistances[pos.x - 1][pos.y];
            }
            if ((pos.x + 1) < 12) {
                way.r = fieldDistances[pos.x + 1][pos.y];
            }
            if ((pos.y - 1) > -1) {
                way.u = fieldDistances[pos.x][pos.y - 1];
            }
            if ((pos.y + 1) < 12) {
                way.d = fieldDistances[pos.x][pos.y + 1];
            }
            if (way.u >= 0 && way.d >= 0 && way.l >= 0 && way.r >= 0) {
                var min = Math.min(way.u, way.d, way.r, way.l);
                if (min === way.u) {
                    goto = {
                        x: (posCoord.x) + minionOffset.x,
                        y: (posCoord.y - 64) + minionOffset.y
                    };
                } else if (min === way.d) {
                    goto = {
                        x: (posCoord.x) + minionOffset.x,
                        y: (posCoord.y + 64) + minionOffset.y
                    };
                } else if (min === way.r) {
                    goto = {
                        x: (posCoord.x + 64) + minionOffset.x,
                        y: (posCoord.y) + minionOffset.y
                    };
                } else if (min === way.l) {
                    goto = {
                        x: (posCoord.x - 64) + minionOffset.x,
                        y: (posCoord.y) + minionOffset.y
                    };
                }
            }


            if (!minion.data.tween) {
                minion.data.tween = game.add.tween(minion).to(goto, (minion.data.minionSpeed / speedUp), 'Linear', true, 0);
            } else {
                var nextTween = game.add.tween(minion).to(goto, (minion.data.minionSpeed / speedUp), 'Linear', true, 0);
                minion.data.tween.chain(nextTween);
                minion.data.tween = nextTween;
            }
            // minion.animations.play('walk');
            minion.data.tween.onComplete.add(function () {
                // minion.data.tween.stop();
                minion.data.moving = false;
                moveDatMinion(minion);
            }, this);
            minionOnField[getPosition(goto.x - 32)][getPosition(goto.y - 32)] = 1;
            if (minion.data.lastPos) {
                minionOnField[minion.data.lastPos.x][minion.data.lastPos.y] = 0;
            }
            minion.data.lastPos = pos;
            console.log(pos);
            console.log(goto);
        } else {
            stopTweensFor(minion);
        }
    }
}

Does someone has any ideas for a more smooth "walkanimations"?

Greetings

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...