pixijs重新布满子弹

时间:2014-07-21 09:46:56

标签: javascript html5 pixi.js

我正在使用html5,canvas,javascript - pixijs构建射击游戏。下面是子弹代码。它会剥去一颗子弹 - 它仅限于在屏幕上最多允许20发子弹以节省资源。

子弹然后在重置功能中被回收。它整体上工作得很好。现在问题是 - 我有2种子弹类型 - 标准和导弹。基本上当子弹重置时 - 我需要确保它的皮肤正确。我已经尝试过移动crossection数组并重建它 - 但它几乎就像它只是克隆了子弹而最终得到了更多的对象。

var TestProjectileVector = (function (_super) {
    __extends(TestProjectileVector, _super);
    function TestProjectileVector(parentScene, x, y, z, localScale, velocity, gravity, vector, projectiletype) {
        if (typeof x === "undefined") { x = 0; }
        if (typeof y === "undefined") { y = 0; }
        if (typeof z === "undefined") { z = 0; }
        if (typeof localScale === "undefined") { localScale = 1; }
        if (typeof velocity === "undefined") { velocity = 100; }
        if (typeof gravity === "undefined") { gravity = 0; }
        if (typeof vector === "undefined") { vector = MathK.Vector3.zero; }
        if (typeof projectiletype === "undefined") { projectiletype = "bullet"; }
        _super.call(this, parentScene, x, y, z, .13);
        this.verticalAngle = 0;
        this.horizontalAngle = 0;
        this.drag = .99;
        this.isDead = false;
        this.isRebounding = false;
        this.doCollisions = true;
        this.numberOfCrossSections = 6;
        this.crossSections = [];
        this.crossSectionSpacing = 7;
        this.parentScene = parentScene;

        console.log("BUILD A BULLET");

        this.bulletTip = "img/bullet-tip.png";
        this.bulletBase = "img/bullet-end.png";

        if(projectiletype == "grenade"){
            this.bulletTip = "img/bullet-grenade-tip.png";
            this.bulletBase = "img/bullet-grenade-end.png";
        }

        this.spr = PIXI.Sprite.fromImage(this.bulletTip);
        this.spr.anchor.x = this.spr.anchor.y = .5;
        this.gravity = gravity;
        this.velocity = velocity;
        this.alpha = 0;
        this.xVel = this.velocity * vector.x;
        this.zVel = this.velocity * vector.z;
        this.yVel = this.velocity * vector.y;
        var tipSection0 = new perspective.EditablePerspectiveObject(parentScene, x, y, z, .16);
        tipSection0.addChild(this.spr);
        this.parentScene.addPerspectiveObject(tipSection0);
        this.crossSections.push(tipSection0);
        var tipSection = new perspective.EditablePerspectiveObject(parentScene, x, y, z, .16);
        var sprTip = PIXI.Sprite.fromImage(this.bulletTip);
        sprTip.anchor.x = sprTip.anchor.y = .5;
        tipSection.addChild(sprTip);
        this.parentScene.addPerspectiveObject(tipSection);
        this.crossSections.push(tipSection);
        for(var i = 0; i < this.numberOfCrossSections; i++) {
            var crossSection = new perspective.EditablePerspectiveObject(parentScene, x, y, z, .16);
            this.spr = PIXI.Sprite.fromImage(this.bulletBase);
            this.spr.anchor.x = this.spr.anchor.y = .5;
            crossSection.addChild(this.spr);
            this.parentScene.addPerspectiveObject(crossSection);
            this.crossSections.push(crossSection);
        }
    }
    TestProjectileVector.prototype.getPosition = function () {
        return new MathK.Vector3(this.xPos, this.yPos, this.zPos);
    };
    TestProjectileVector.prototype.getNextPosition = function () {
        return new MathK.Vector3(this.xPos + this.xVel * 3, this.yPos + this.yVel * 3, this.zPos + this.zVel * 3);
    };
    TestProjectileVector.prototype.reset = function (x, y, z, localScale, velocity, gravity, vector, projectiletype) {
        if (typeof x === "undefined") { x = 0; }
        if (typeof y === "undefined") { y = 0; }
        if (typeof z === "undefined") { z = 0; }
        if (typeof localScale === "undefined") { localScale = 1; }
        if (typeof velocity === "undefined") { velocity = 100; }
        if (typeof gravity === "undefined") { gravity = 0; }
        if (typeof vector === "undefined") { vector = MathK.Vector3.zero; }
        this.isDead = false;
        this.isRebounding = false;
        this.xPos = x;
        this.yPos = y;
        this.zPos = z;
        this.velocity = velocity;
        this.gravity = gravity;
        this.xVel = this.velocity * vector.x;
        this.zVel = this.velocity * vector.z;
        this.yVel = this.velocity * vector.y;

    };
    TestProjectileVector.prototype.positionCrossSections = function () {
        var velVectorMagnitude = Math.sqrt(this.xVel * this.xVel + this.yVel * this.yVel + this.zVel * this.zVel);
        var offsetVelX = this.isRebounding ? this.reboundingVelX : this.xVel / velVectorMagnitude;
        var offsetVelZ = this.isRebounding ? this.reboundingVelZ : this.zVel / velVectorMagnitude;
        for(var i = 1; i <= this.crossSections.length; i++) {
            var crossSection = this.crossSections[i - 1];
            crossSection.xPos = this.xPos + this.crossSectionSpacing * i * -(offsetVelX);
            crossSection.yPos = this.yPos - this.crossSectionSpacing * i * (this.yVel / velVectorMagnitude);
            crossSection.zPos = this.zPos + this.crossSectionSpacing * i * (-Math.abs(offsetVelZ));
            if(crossSection.yPos > 0) {
                crossSection.yPos = 0;
            }
        }
    };
    TestProjectileVector.prototype.rebound = function () {
        if(!this.isRebounding) {
            this.isRebounding = true;
            var velVectorMagnitude = Math.sqrt(this.xVel * this.xVel + this.yVel * this.yVel + this.zVel * this.zVel);
            this.reboundingVelX = this.xVel / velVectorMagnitude;
            this.reboundingVelZ = this.zVel / velVectorMagnitude;
        }
    };
    TestProjectileVector.prototype.die = function () {
        if(!this.isDead) {
            this.isDead = true;
        }
    };
    TestProjectileVector.prototype.hitBarrier = function () {
        this.xVel *= -.1;
        this.zVel *= -.1;
        this.rebound();
        this.die();
    };
    TestProjectileVector.prototype.update = function () {
        _super.prototype.update.call(this);
        if(this.zPos > 2000) {
            this.zPos = 2000;
            this.hitBarrier();
        }
        this.yVel += this.gravity;
        this.xPos += this.xVel;
        this.yPos += this.yVel;
        this.zPos += this.zVel;
        if(this.yPos > 0) {
            this.yPos = 0;
            if(!this.isRebounding && this.xPos < 1500) {
                this.yVel *= -.5;
                this.xVel *= .5;
                this.zVel *= .5;
            } else {
                this.yVel *= -.3;
                this.xVel *= -.1;
                this.zVel *= -.1;
                this.rebound();
            }
            if(Math.abs(this.yVel) < .001) {
                this.die();
                return;
            }
        }
        this.positionCrossSections();
        this.velocity *= this.drag;
        this.positionCrossSections();
    };
    return TestProjectileVector;
})(perspective.EditablePerspectiveObject);

2 个答案:

答案 0 :(得分:0)

enter image description here

以下是处理构造器以创建子弹的抛射物代码,这是一个重置功能,可将x,y,z坐标放回枪的尖端,准备好再次回收子弹。

我没有看到任何可用于创造无数子弹的东西 - 所以它们被正确剥皮 - 但随后摧毁了旧的子弹。所以保留最后20发子弹。我尝试在重置时重新启动子弹 - 这将是理想的但我无法对构造函数中的代码进行反向工程以正确地执行此操作。

var TestProjectileVector = (function (_super) {
    __extends(TestProjectileVector, _super);
    function TestProjectileVector(parentScene, x, y, z, localScale, velocity, gravity, vector, projectiletype) {
        if (typeof x === "undefined") { x = 0; }
        if (typeof y === "undefined") { y = 0; }
        if (typeof z === "undefined") { z = 0; }
        if (typeof localScale === "undefined") { localScale = 1; }
        if (typeof velocity === "undefined") { velocity = 100; }
        if (typeof gravity === "undefined") { gravity = 0; }
        if (typeof vector === "undefined") { vector = MathK.Vector3.zero; }
        if (typeof projectiletype === "undefined") { projectiletype = "bullet"; }
        _super.call(this, parentScene, x, y, z, .13);
        this.verticalAngle = 0;
        this.horizontalAngle = 0;
        this.drag = .99;
        this.isDead = false;
        this.isRebounding = false;
        this.doCollisions = true;
        this.numberOfCrossSections = 6;
        this.crossSections = [];
        this.crossSectionSpacing = 7;
        this.parentScene = parentScene;

        console.log("BUILD A BULLET");

        this.bulletTip = "img/bullet-tip.png";
        this.bulletBase = "img/bullet-end.png";

        if(projectiletype == "grenade"){
            this.bulletTip = "img/bullet-grenade-tip.png";
            this.bulletBase = "img/bullet-grenade-end.png";
        }

        this.spr = PIXI.Sprite.fromImage(this.bulletTip);
        this.spr.anchor.x = this.spr.anchor.y = .5;
        this.gravity = gravity;
        this.velocity = velocity;
        this.alpha = 0;
        this.xVel = this.velocity * vector.x;
        this.zVel = this.velocity * vector.z;
        this.yVel = this.velocity * vector.y;
        var tipSection0 = new perspective.EditablePerspectiveObject(parentScene, x, y, z, .16);
        tipSection0.addChild(this.spr);
        this.parentScene.addPerspectiveObject(tipSection0);
        this.crossSections.push(tipSection0);
        var tipSection = new perspective.EditablePerspectiveObject(parentScene, x, y, z, .16);
        var sprTip = PIXI.Sprite.fromImage(this.bulletTip);
        sprTip.anchor.x = sprTip.anchor.y = .5;
        tipSection.addChild(sprTip);
        this.parentScene.addPerspectiveObject(tipSection);
        this.crossSections.push(tipSection);
        for(var i = 0; i < this.numberOfCrossSections; i++) {
            var crossSection = new perspective.EditablePerspectiveObject(parentScene, x, y, z, .16);
            this.spr = PIXI.Sprite.fromImage(this.bulletBase);
            this.spr.anchor.x = this.spr.anchor.y = .5;
            crossSection.addChild(this.spr);
            this.parentScene.addPerspectiveObject(crossSection);
            this.crossSections.push(crossSection);
        }
    }
    TestProjectileVector.prototype.getPosition = function () {
        return new MathK.Vector3(this.xPos, this.yPos, this.zPos);
    };
    TestProjectileVector.prototype.getNextPosition = function () {
        return new MathK.Vector3(this.xPos + this.xVel * 3, this.yPos + this.yVel * 3, this.zPos + this.zVel * 3);
    }; 

    TestProjectileVector.prototype.reset = function (x, y, z, localScale, velocity, gravity, vector, projectiletype) {
        if (typeof x === "undefined") { x = 0; }
        if (typeof y === "undefined") { y = 0; }
        if (typeof z === "undefined") { z = 0; }
        if (typeof localScale === "undefined") { localScale = 1; }
        if (typeof velocity === "undefined") { velocity = 100; }
        if (typeof gravity === "undefined") { gravity = 0; }
        if (typeof vector === "undefined") { vector = MathK.Vector3.zero; }
        this.isDead = false;
        this.isRebounding = false;
        this.xPos = x;
        this.yPos = y;
        this.zPos = z;
        this.velocity = velocity;
        this.gravity = gravity;
        this.xVel = this.velocity * vector.x;
        this.zVel = this.velocity * vector.z;
        this.yVel = this.velocity * vector.y;

    };
    TestProjectileVector.prototype.positionCrossSections = function () {
        var velVectorMagnitude = Math.sqrt(this.xVel * this.xVel + this.yVel * this.yVel + this.zVel * this.zVel);
        var offsetVelX = this.isRebounding ? this.reboundingVelX : this.xVel / velVectorMagnitude;
        var offsetVelZ = this.isRebounding ? this.reboundingVelZ : this.zVel / velVectorMagnitude;
        for(var i = 1; i <= this.crossSections.length; i++) {
            var crossSection = this.crossSections[i - 1];
            crossSection.xPos = this.xPos + this.crossSectionSpacing * i * -(offsetVelX);
            crossSection.yPos = this.yPos - this.crossSectionSpacing * i * (this.yVel / velVectorMagnitude);
            crossSection.zPos = this.zPos + this.crossSectionSpacing * i * (-Math.abs(offsetVelZ));
            if(crossSection.yPos > 0) {
                crossSection.yPos = 0;
            }
        }
    };
    TestProjectileVector.prototype.rebound = function () {
        if(!this.isRebounding) {
            this.isRebounding = true;
            var velVectorMagnitude = Math.sqrt(this.xVel * this.xVel + this.yVel * this.yVel + this.zVel * this.zVel);
            this.reboundingVelX = this.xVel / velVectorMagnitude;
            this.reboundingVelZ = this.zVel / velVectorMagnitude;
        }
    };
    TestProjectileVector.prototype.die = function () {
        if(!this.isDead) {
            this.isDead = true;

            /*
            var that = this;
            setTimeout(function(){
                that.zPos = -999;
            }, 12000);*/
        }
    };
    TestProjectileVector.prototype.hitBarrier = function () {
        this.xVel *= -.1;
        this.zVel *= -.1;
        this.rebound();
        this.die();
    };
    TestProjectileVector.prototype.update = function () {
        _super.prototype.update.call(this);
        if(this.zPos > 2000) {
            this.zPos = 2000;
            this.hitBarrier();
        }
        this.yVel += this.gravity;
        this.xPos += this.xVel;
        this.yPos += this.yVel;
        this.zPos += this.zVel;
        if(this.yPos > 0) {
            this.yPos = 0;
            if(!this.isRebounding && this.xPos < 1500) {
                this.yVel *= -.5;
                this.xVel *= .5;
                this.zVel *= .5;
            } else {
                this.yVel *= -.3;
                this.xVel *= -.1;
                this.zVel *= -.1;
                this.rebound();
            }
            if(Math.abs(this.yVel) < .001) {
                this.die();
                return;
            }
        }
        this.positionCrossSections();
        this.velocity *= this.drag;
        this.positionCrossSections();
    };
    return TestProjectileVector;
})(perspective.EditablePerspectiveObject);

答案 1 :(得分:0)

我设法解决了这个问题 - 通过获取当前横截面的长度 - 然后从数组和透视图中删除它们。

然后再次重新应用纹理。

//__remove last skins
var lastBulletSpriteObjs = this.crossSections;
if(lastBulletSpriteObjs.length > 0){
    console.log("lastBulletSpriteObjs", lastBulletSpriteObjs);
    console.log("lastBulletSpriteObjs.length", lastBulletSpriteObjs.length);

    for(var i = 0; i < lastBulletSpriteObjs.length; i++) {
        this.parentScene.removePerspectiveObject(lastBulletSpriteObjs[i]);
    }
}

this.crossSections = [];//nulify the crossSections array