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 Gamma

I've been trying hard to make it so that my character can shoot with a different animation while at the same time standing. I don't want my character just standing and shooting randomly. I'll be making animations for running while shooting and standing while shooting, but how do I implement the shooting animations for running and standing.

I tried adding;

[Update Function]

if(this.standing && ig.input.pressed('shoot')) {
this.currentAnim = this.anims.standshooting;
}					

But it doesn't show the animation at all. If you guys want to know how I have my stuff set up here it is below.

[Update Function]

/ move left or right
        	var accel = this.standing ? this.accelGround : this.accelAir;
        	if( ig.input.state('left') ) {
        		this.accel.x = -accel;
        		this.flip = true;
        	}else if( ig.input.state('right') ) {
        		this.accel.x = accel;
        		this.flip = false;
		}else if( ig.input.state('backwards') ) {
                this.accel.x = -accel;
                this.flip = false;
            
        	}else{
        		this.accel.x = 0;
        	}
		
        	// jump
        	 if (ig.input.pressed('jump') && this.standing)
    {
        this.vel.y = -this.jump;
        this.jumpX = 1;
    }

    if (ig.input.state('jump'))
    {
        this.vel.y -= this.jump*0.1*this.jumpX;
        this.jumpX = (this.jumpX > 0.1 ? this.jumpX*0.89 : 0);
    }
            // shoot
            if( ig.input.pressed('shoot') ) {
            	ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );
            }
	    
            if( ig.input.pressed('switch') ) {
            	this.weapon ++;
            	if(this.weapon >= this.totalWeapons)
            		this.weapon = 0;
                switch(this.weapon){
                	case(0):
                		this.activeWeapon = "EntityPistol";
                		break;
                	case(1):
                		this.activeWeapon = "EntityDouble";
                	break;
                }
                this.setupAnimation(this.weapon);
            }
            // set the current animation, based on the player's speed
            if( this.vel.y < 0 ) {
            	this.currentAnim = this.anims.jump;
            }else if( this.vel.y > 0 ) {
            	this.currentAnim = this.anims.fall;
            }else if( this.vel.x > 0 ) {
            	this.currentAnim = this.anims.run;
	    }else if( this.vel.x < 0 ) {
            	this.currentAnim = this.anims.run;
            }else{
            	this.currentAnim = this.anims.idle;
            }
            this.currentAnim.flip.x = this.flip;
        	// move!
	       this.currentAnim.flip.x = this.flip;
            if( this.invincibleTimer.delta() > this.invincibleDelay ) {
                this.invincible = false;
                this.currentAnim.alpha = 1;
            }
        	this.parent();
		
        },

Thank you all in advance.

1 decade ago by Joncom

I find it very difficult to read your code when the indentation is all out-of-whack. I'd recommend using an IDE that supports auto-formatting of your code by the touch of a button. I use Sublime Text 2 with the jsFormat plugin.

The result:

// move left or right
var accel = this.standing ? this.accelGround : this.accelAir;
if(ig.input.state('left')) {
    this.accel.x = -accel;
    this.flip = true;
} else if(ig.input.state('right')) {
    this.accel.x = accel;
    this.flip = false;
} else if(ig.input.state('backwards')) {
    this.accel.x = -accel;
    this.flip = false;

} else {
    this.accel.x = 0;
}

// jump
if(ig.input.pressed('jump') && this.standing) {
    this.vel.y = -this.jump;
    this.jumpX = 1;
}

if(ig.input.state('jump')) {
    this.vel.y -= this.jump * 0.1 * this.jumpX;
    this.jumpX = (this.jumpX > 0.1 ? this.jumpX * 0.89 : 0);
}

// shoot
if(ig.input.pressed('shoot')) {
    ig.game.spawnEntity(this.activeWeapon, this.pos.x, this.pos.y, {
        flip: this.flip
    });
}

if(ig.input.pressed('switch')) {
    this.weapon++;
    if(this.weapon >= this.totalWeapons) this.weapon = 0;
    switch(this.weapon) {
    case(0):
        this.activeWeapon = "EntityPistol";
        break;
    case(1):
        this.activeWeapon = "EntityDouble";
        break;
    }
    this.setupAnimation(this.weapon);
}

// set the current animation, based on the player's speed
if(this.vel.y < 0) {
    this.currentAnim = this.anims.jump;
} else if(this.vel.y > 0) {
    this.currentAnim = this.anims.fall;
} else if(this.vel.x > 0) {
    this.currentAnim = this.anims.run;
} else if(this.vel.x < 0) {
    this.currentAnim = this.anims.run;
} else {
    this.currentAnim = this.anims.idle;
}
this.currentAnim.flip.x = this.flip;
// move!
this.currentAnim.flip.x = this.flip;
if(this.invincibleTimer.delta() > this.invincibleDelay) {
    this.invincible = false;
    this.currentAnim.alpha = 1;
}

this.parent();

Your code should in fact work, but perhaps when you tried, you did so in the wrong order. Try putting it at the very bottom, but just above this.parent();

Edit - Actually upon inspecting your code a little more... The reason that it did not work for you is because you can only catch the ig.input.pressed() event once and then it clears. Meaning if you checked it once, made an entity, and then checked it again to see about updating the animation, well it's been reset from the first time and won't work for the second call.

You can fix this by setting the animation within the same code block as where you spawn the entity this.activeWeapon.

1 decade ago by Gamma

Thanks I got it to work. Except how do I apply this to jumping as well, becuase for standing I can always add:

this.standing

After making the condition go towards standing and shooting at the same time, but how would I apply this to jumping?

1 decade ago by rizm

Hey, @gamme how did you get this to work? I am having the exact same issue you were having where

if(this.standing && ig.input.pressed('shoot')) {
this.currentAnim = this.anims.standshooting;
}                    

was not updating the player entity to show the shooting frame

R.

1 decade ago by Rybar

check for input.state instead. input.pressed is only called once per keypress.

if(this.standing && ig.input.state('shoot')) {
this.currentAnim = this.anims.standshooting;
}   

1 decade ago by Gamma

@rizm
I figured out how to make the character shoot while standing but it didn't look quite right, so I'm going to be fixing it. I'm quite busy today, but tonight I'll be revealing the code behind my method.

@Rybar
I'll try that out. This would work for jumping too correct?

1 decade ago by Gamma

@rizm I figured out how to do shooting while standing animations, I'm sorry that I am late for telling you about this.

This only works if your players movement is based on velocity. If you have that covered, all you have to do is adjust your code to the one below.

Update Function:

 // set the current animation, based on the player's speed
            if( this.vel.y < 0 ) {
            	this.currentAnim = this.anims.jump;
            }else if( this.vel.y > 0 ) {
            	this.currentAnim = this.anims.fall;
            }else if( this.vel.x > 0 ) {
            	this.currentAnim = this.anims.run;
	    }else if( this.vel.x < 0 ) {
            	this.currentAnim = this.anims.run;
           } else {
        if( ig.input.state('shoot') ) {

//This place your animation below, this the example I used: "this.anims.shootwhile....."

          this.currentAnim = this.anims.shootwhilestanding; 
        } else if ( this.standing ) {
          this.currentAnim = this.anims.idle;
        }
      }

Page 1 of 1
« first « previous next › last »