javascript if statement help?

C++, C#, Java, PHP, ect...
Post Reply
Oroton
Posts: 39
Joined: Wed Dec 02, 2015 4:56 am

javascript if statement help?

Post by Oroton »

So
I have a sprite variable, and when I draw it i use,

Code: Select all

Sprite.prototype.draw = function() {
	if (!this.flashOn) {
		ctx.drawImage(
			this.tileset.image,         
			this.states[this.currentState].frames[this.states[this.currentState].currentFrame].split(',')[0] * this.tileset.tileWidth,
			this.states[this.currentState].frames[this.states[this.currentState].currentFrame].split(',')[1] * this.tileset.tileHeight,
                        this.tileset.tileWidth,       
			this.tileset.tileHeight,      
			Math.round(this.x),
			Math.round(this.y),
			this.width,
			this.height
		);
	}
}
If I create a sprite, like this

Code: Select all

var spriteLeftAnim = new Animation(['1,1', '1,0'], 200); 
var spriteRightAnim = new Animation(['0,1', '0,0'], 200);
var spriteUpAnim = new Animation(['2,1', '2,0'], 200);
var spriteDownAnim = new Animation(['3,1', '3,0'], 200);

var spriteDefinitions = {
	turtle: new SpriteDefinition(
		tilesets.turtle,
		{        			
                        'left': spriteLeftAnim,
                        'right': spriteRightAnim,
                        'down': spriteDownAnim,
                        'up': spriteUpAnim
                }, //Added by Oroton,
		300, //200?
		5,
		true,
		true
	)

It runs perfectly,
I have to create another function for to run solid objects and i use this

Code: Select all

var chestAnim = new staticObj(['3,0']);
	chest: new SpriteDefinition(
		tilesets.maptiles,
		{
			down: chestAnim
		}
	
these are the functions..

Code: Select all


function Animation(frames, frameDuration) {
			this.frames = frames;
			this.currentFrame = 0;
			this.frameTimer = Date.now();
			this.frameDuration = frameDuration;
		}

function staticObj(frames) {
			this.frames = frames;
			this.currentFrame = 0;
		}

What I want to do is instead of writing

Code: Select all

var chestAnim = new staticObj(['3,0']);
	chest: new SpriteDefinition(
		tilesets.maptiles,
		{
			down: chestAnim
		}

Write for the solid objects, but what happens is when i draw the item, it under currentState.frames..

Code: Select all

var chestAnim = new staticObj(['3,0']);
	chest: new SpriteDefinition(
		tilesets.maptiles,
		{
			down: '3,0'
		}
or comething like

Code: Select all

{
down: chest.frames{'3,0'}
}
if that makes sense.
Post Reply

Return to “Coding”