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 theSociableme

I have a body entity and a head entity. When you touch the body I want the head to react. i.e. to shoot - same as the key press.

What is the best way to do this?

main.js
        ig.input.bind( ig.KEY.C, 'shoot' );
        ig.input.bind(ig.KEY.MOUSE1, 'CanvasTouch');

farmerBody.js
update: function() {
            if (ig.input.pressed('CanvasTouch') && this.inFocus()) {
                //  What Do I put here to 
                //  trigger the head to shoot which
               //  is the same as pressing c on the keyboard
            }
        },

        inFocus: function() {
            return (
               (this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
               ((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
               (this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
               ((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
            );
         }

farmerHead.js
update: function() {
            if(this.currentAnim == this.anims.spit){
                if(this.spitTimer > 20){
                    this.currentAnim = this.anims.idle;
                    this.spitTimer = -1;
                    this.spiting = false;
                }
                this.spitTimer += 1;
            }

            if(ig.input.state('CanvasTouch')){
                this.headRotation = ig.input.mouse.y - 310;
                this.body.SetXForm(this.body.GetPosition(), this.headRotation/500);
            }
            if( ig.input.pressed('shoot') && (this.seedsAvailable > 0) && (this.spiting == false)) {
                var x = this.pos.x + 12;
                var y = this.pos.y + 4;
                this.currentAnim = this.anims.spit;
                var seed = ig.game.spawnEntity( EntitySeed, x, y, {flip:this.flip} );
                seed.body.ApplyImpulse( new b2.Vec2(20 ,this.headRotation), this.body.GetPosition() );
                this.seedsAvailable -= 1;
                this.spiting = true;
            }
            this.parent();

        },

How do I change this around to make touching the body spit/shoot?

Any help is appreciated.

1 decade ago by alexandre

Delegation?

If you are loading your level, try:

main.js
loadLevel: function(l)
{
	this.parent(l);

	// Setup belly scratchin' delegation
	var belly = this.getEntityByName('belly');
	var head = this.getEntityByName('head');
	if (head && belly)
		belly.delegate = head;
},

farmerBody.js
name: 'belly',
delegate: null,

update: function()
{
	if (ig.input.pressed('CanvasTouch') && this.inFocus())
	{
		//  What Do I put here to 
		//  trigger the head to shoot which
		//  is the same as pressing c on the keyboard
		if (this.delegate)
			this.delegate.bellyTouched(this);
	}
},

inFocus: function()
{
	return (
		(this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
		((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
		(this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
		((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
	);
}

farmerHead.js
name: 'head',

update: function()
{
	if (this.currentAnim == this.anims.spit)
	{
		if (this.spitTimer > 20)
		{
			this.currentAnim = this.anims.idle;
			this.spitTimer = -1;
			this.spiting = false;
		}
		this.spitTimer += 1;
	}

	if (ig.input.state('CanvasTouch'))
	{
		this.headRotation = ig.input.mouse.y - 310;
		this.body.SetXForm(this.body.GetPosition(), this.headRotation/500);
	}

	if (ig.input.pressed('shoot') && (this.seedsAvailable > 0) && (this.spiting == false))
		this.spit();

	this.parent();
},

spit: function()
{
	var x = this.pos.x + 12;
	var y = this.pos.y + 4;
	this.currentAnim = this.anims.spit;
	var seed = ig.game.spawnEntity( EntitySeed, x, y, {flip:this.flip} );
	seed.body.ApplyImpulse( new b2.Vec2(20 ,this.headRotation), this.body.GetPosition());
	this.seedsAvailable -= 1;
	this.spiting = true;
},

// Belly delegate methods
bellyTouched: function(sender)
{
	this.spit();
},

1 decade ago by theSociableme

Thank you so Much!!

That is SUPER HELPFUL!!!!

I didn't know about getEntityByName either.

Thanks Again,

Mark

1 decade ago by jeffreybiles

Thanks for this! Solved my problem beautifully.

Is delegate a javascript feature or an impact feature? When I google it, all I can find is jquery's delegate.
Page 1 of 1
« first « previous next › last »