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 Joncom

Where ever possible, I try not to write duplicate code. But sometimes I don't see any way around it. Here's an example where I'm not really sure what can be done....

If I could somehow use the value of this.value in the selection of which animation to use, this would clean up real nice.

Anybody know a better way than this?

moveAnimStart: function(alternateFeet)
{
    switch(this.facing)
    {
	case 'left':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkLeftA;
		else this.currentAnim = this.anims.walkLeftB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runLeftA;
		else this.currentAnim = this.anims.runLeftB;
	    }
	    break;
	case 'right':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkRightA;
		else this.currentAnim = this.anims.walkRightB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runRightA;
		else this.currentAnim = this.anims.runRightB;
	    }
	    break;
	case 'up':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkUpA;
		else this.currentAnim = this.anims.walkUpB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runUpA;
		else this.currentAnim = this.anims.runUpB;
	    }
	    break;
	case 'down':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkDownA;
		else this.currentAnim = this.anims.walkDownB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runDownA;
		else this.currentAnim = this.anims.runDownB;
	    }
	    break;
    }
    if(alternateFeet) this.leftFoot = !this.leftFoot;
    this.currentAnim.rewind();
},

1 decade ago by Joncom

Here's another example. If this.skin is not set to a valid choice, I want to act as if this.skin=='boy'...

reskin: function()
{
    this.offset = { x: 0, y: 16 };
    switch(this.skin)
    {
	// kind of like enum
	case 'boy':
	case 'girl':
	case 'fat':
	case 'kid':
	case 'labgeek':
	    this.animSheet = new ig.AnimationSheet( 'media/people/rs.' + this.skin + '.png', 16, 32 );
	    // add the animations
	    this.addAnim( 'walkUpA', 0.13333, [2,0], true );
	    this.addAnim( 'walkUpB', 0.13333, [1,0], true );
	    this.addAnim( 'walkDownA', 0.13333, [14,12], true );
	    this.addAnim( 'walkDownB', 0.13333, [13,12], true );
	    this.addAnim( 'walkLeftA', 0.13333, [8,6], true );
	    this.addAnim( 'walkLeftB', 0.13333, [7,6], true );
	    this.addAnim( 'walkRightA', 0.13333, [8,6], true );
	    this.addAnim( 'walkRightB', 0.13333, [7,6], true );
	    this.addAnim( 'runUpA', 0.08333, [4,3], true );
	    this.addAnim( 'runUpB', 0.08333, [5,3], true );
	    this.addAnim( 'runDownA', 0.08333, [16,15], true );
	    this.addAnim( 'runDownB', 0.08333, [17,15], true );
	    this.addAnim( 'runLeftA', 0.08333, [10,9], true );
	    this.addAnim( 'runLeftB', 0.08333, [11,9], true );
	    this.addAnim( 'runRightA', 0.08333, [10,9], true );
	    this.addAnim( 'runRightB', 0.08333, [11,9], true );
	    this.addAnim( 'slowup', 0.26667, [2,0,1,0] );
	    this.addAnim( 'slowdown', 0.26667, [14,12,13,12] );
	    this.addAnim( 'slowleft', 0.26667, [8,6,7,6] );
	    this.addAnim( 'slowright', 0.26667, [8,6,7,6] );
	    this.addAnim( 'idleup', 0.1, [0], true );
	    this.addAnim( 'idledown', 0.1, [12], true );
	    this.addAnim( 'idleleft', 0.1, [6], true );
	    this.addAnim( 'idleright', 0.1, [6], true );
	    // flip right-facing animations
	    this.anims.walkRightA.flip.x = true;
	    this.anims.walkRightB.flip.x = true;
	    this.anims.runRightA.flip.x = true;
	    this.anims.runRightB.flip.x = true;
	    this.anims.slowright.flip.x = true;
	    this.anims.idleright.flip.x = true;
	    // set initial animation
	    this.moveAnimStop();
	    break;
	default:
	    this.animSheet = new ig.AnimationSheet( 'media/people/rs.boy.png', 16, 32 );
	    // add the animations
	    this.addAnim( 'walkUpA', 0.13333, [2,0], true );
	    this.addAnim( 'walkUpB', 0.13333, [1,0], true );
	    this.addAnim( 'walkDownA', 0.13333, [14,12], true );
	    this.addAnim( 'walkDownB', 0.13333, [13,12], true );
	    this.addAnim( 'walkLeftA', 0.13333, [8,6], true );
	    this.addAnim( 'walkLeftB', 0.13333, [7,6], true );
	    this.addAnim( 'walkRightA', 0.13333, [8,6], true );
	    this.addAnim( 'walkRightB', 0.13333, [7,6], true );
	    this.addAnim( 'runUpA', 0.08333, [4,3], true );
	    this.addAnim( 'runUpB', 0.08333, [5,3], true );
	    this.addAnim( 'runDownA', 0.08333, [16,15], true );
	    this.addAnim( 'runDownB', 0.08333, [17,15], true );
	    this.addAnim( 'runLeftA', 0.08333, [10,9], true );
	    this.addAnim( 'runLeftB', 0.08333, [11,9], true );
	    this.addAnim( 'runRightA', 0.08333, [10,9], true );
	    this.addAnim( 'runRightB', 0.08333, [11,9], true );
	    this.addAnim( 'slowup', 0.26667, [2,0,1,0] );
	    this.addAnim( 'slowdown', 0.26667, [14,12,13,12] );
	    this.addAnim( 'slowleft', 0.26667, [8,6,7,6] );
	    this.addAnim( 'slowright', 0.26667, [8,6,7,6] );
	    this.addAnim( 'idleup', 0.1, [0], true );
	    this.addAnim( 'idledown', 0.1, [12], true );
	    this.addAnim( 'idleleft', 0.1, [6], true );
	    this.addAnim( 'idleright', 0.1, [6], true );
	    // flip right-facing animations
	    this.anims.walkRightA.flip.x = true;
	    this.anims.walkRightB.flip.x = true;
	    this.anims.runRightA.flip.x = true;
	    this.anims.runRightB.flip.x = true;
	    this.anims.slowright.flip.x = true;
	    this.anims.idleright.flip.x = true;
	    // set initial animation
	    this.moveAnimStop();
	    break;
    }
},

EDIT Ok, I found a better way to do reskin()

reskin: function()
{
    switch(this.skin)
    {
	case 'boy':
	case 'girl':
	case 'fat':
	case 'kid':
	case 'labgeek':
	    var use = true;
	    break;
	default:
	    var use = false;
	    break;
    }
    this.offset = { x: 0, y: 16 };
    if(!use) this.skin == 'boy';
    this.animSheet = new ig.AnimationSheet( 'media/people/rs.' + this.skin + '.png', 16, 32 );
    // add the animations
    this.addAnim( 'walkUpA', 0.13333, [2,0], true );
    this.addAnim( 'walkUpB', 0.13333, [1,0], true );
    this.addAnim( 'walkDownA', 0.13333, [14,12], true );
    this.addAnim( 'walkDownB', 0.13333, [13,12], true );
    this.addAnim( 'walkLeftA', 0.13333, [8,6], true );
    this.addAnim( 'walkLeftB', 0.13333, [7,6], true );
    this.addAnim( 'walkRightA', 0.13333, [8,6], true );
    this.addAnim( 'walkRightB', 0.13333, [7,6], true );
    this.addAnim( 'runUpA', 0.08333, [4,3], true );
    this.addAnim( 'runUpB', 0.08333, [5,3], true );
    this.addAnim( 'runDownA', 0.08333, [16,15], true );
    this.addAnim( 'runDownB', 0.08333, [17,15], true );
    this.addAnim( 'runLeftA', 0.08333, [10,9], true );
    this.addAnim( 'runLeftB', 0.08333, [11,9], true );
    this.addAnim( 'runRightA', 0.08333, [10,9], true );
    this.addAnim( 'runRightB', 0.08333, [11,9], true );
    this.addAnim( 'slowup', 0.26667, [2,0,1,0] );
    this.addAnim( 'slowdown', 0.26667, [14,12,13,12] );
    this.addAnim( 'slowleft', 0.26667, [8,6,7,6] );
    this.addAnim( 'slowright', 0.26667, [8,6,7,6] );
    this.addAnim( 'idleup', 0.1, [0], true );
    this.addAnim( 'idledown', 0.1, [12], true );
    this.addAnim( 'idleleft', 0.1, [6], true );
    this.addAnim( 'idleright', 0.1, [6], true );
    // flip right-facing animations
    this.anims.walkRightA.flip.x = true;
    this.anims.walkRightB.flip.x = true;
    this.anims.runRightA.flip.x = true;
    this.anims.runRightB.flip.x = true;
    this.anims.slowright.flip.x = true;
    this.anims.idleright.flip.x = true;
    // set initial animation
    this.moveAnimStop();
},

Still unsure about my original problem though.

1 decade ago by Joncom

Quote from Joncom
moveAnimStart: function(alternateFeet)
{
    switch(this.facing)
    {
	case 'left':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkLeftA;
		else this.currentAnim = this.anims.walkLeftB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runLeftA;
		else this.currentAnim = this.anims.runLeftB;
	    }
	    break;
	case 'right':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkRightA;
		else this.currentAnim = this.anims.walkRightB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runRightA;
		else this.currentAnim = this.anims.runRightB;
	    }
	    break;
	case 'up':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkUpA;
		else this.currentAnim = this.anims.walkUpB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runUpA;
		else this.currentAnim = this.anims.runUpB;
	    }
	    break;
	case 'down':
	    if(this.speed==this.walkSpeed)
	    {
		if(this.leftFoot) this.currentAnim = this.anims.walkDownA;
		else this.currentAnim = this.anims.walkDownB;
	    }
	    else // assume he is running
	    {
		if(this.leftFoot) this.currentAnim = this.anims.runDownA;
		else this.currentAnim = this.anims.runDownB;
	    }
	    break;
    }
    if(alternateFeet) this.leftFoot = !this.leftFoot;
    this.currentAnim.rewind();
},


Ok, here is the solution I came up with for that one:

moveAnimStart: function(alternateFeet)
{
    // determine rate
    if(this.speed==this.walkSpeed) var rate = 'walk'; // is walking
    else var rate = 'run'; // else assume running
    
    // determine foot
    if(this.leftFoot) var foot = 'A';
    else var foot = 'B'
    
    // set animation
    this.currentAnim = this.anims[rate+this.capitaliseFirstLetter(this.facing)+foot];
		
    if(alternateFeet) this.leftFoot = !this.leftFoot; // alternate feet
    this.currentAnim.rewind(); // starting at first frame
},
Page 1 of 1
« first « previous next › last »