Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by paulh

I cant seem to workout the logic for this :-(

Im spawning entities of the sides of the screen and use:

 update: function() {
		//console.log(this.pos.y);
		//console.log(this.GravityFactor);
	 
		if (this.pos.x < - 40){ //kill if off left of screen
			this.killcount();
			this.kill();
		}
		if (this.pos.x > ig.system.realWidth + 40){ //kill if off right
			this.killcount();
			this.kill();
		}
		if (this.pos.y > 360){ //kill if below
			this.killcount();
			this.kill();
		}
		else if (this.pos.y < -50){ //kill if above
			this.killcount();
			this.kill();
		}
		this.parent();
		},

The thing is i need, in some instances, to spawn entities beyond the location i want to kill them from .. so yes i could just extend the range so they have to be further off screen before killed but that will slow my game down so im hesitant.

I guess i need to have a "spawn state" whereby the player can still interact with them but they wont get killed as they spawn, any ideas?

1 decade ago by jswart

I would set a state on the entity.
    bKillable: null,

then set, this.bKillable = false during init. Then create a function that checks for the first time that the entity crosses into the playable area of the map, at that point set this.bKillable = true.

Then change your kill code to something like this:

if (this.pos.x < - 40  && this.bKillable){ //kill if off left of screen
            this.killcount();
            this.kill();
        }
        if (this.pos.x > ig.system.realWidth + 40 && this.bKillable){ //kill if off right
            this.killcount();
            this.kill();
        }
        if (this.pos.y > 360 && this.bKillable){ //kill if below
            this.killcount();
            this.kill();
        }
        else if (this.pos.y < -50 && this.bKillable){ //kill if above
            this.killcount();
            this.kill();
        }
        this.parent();
        },

1 decade ago by dominic

Don&039;t use #ig.system.realWidth for game logic - it's the width of the scaled canvas and does not represent game coordinates. Use .width instead.

Also, there&039;s no shame in combining several #if statements:
update: function() {
	if( 
		this.killable && (
			this.pos.x < -40 ||
			this.pos.x > ig.system.width ||
			this.pos.y < -50 ||
			this.pos.y > ig.system.height
		)
	) {
		this.killcount();
		this.kill();
	}

	this.parent();
}

(Note that I named the property that jswart proposed killable instead of bKillable - I'm not a big fan of the Hungarian Notation :))

1 decade ago by jswart

Quote from dominic


(Note that I named the property that jswart proposed killable instead of bKillable - I'm not a big fan of the Hungarian Notation :))


haha, no problem. Some people don't like it. We use it at work for any dynamic languages, so we can see at a glance what we are dealing with on large projects. Became a habit.

1 decade ago by paulh

amasing how much i just learned, thank you both!
Page 1 of 1
« first « previous next › last »