How to do a "each" method in JS

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
So hi guys,


I was wondering because I want to do something like a atb system similar to this : 





the problem in Ruby it's easy...but in JS it's kinda weird...I just don't know how to do a proper "each" method.


here my coding...in simple this really similar to game_Battler I just don't know how to initialize it

var NioPlugin = NioPlugin || {};
var NioPlugin.ATB_GUI = NioPlugin.ATB_GUI || {};


//==============================================================================
// ■ Sprite_ATB
//------------------------------------------------------------------------------
// The Sprite class who handle
//==============================================================================
function Sprite_ATB(){this.initialize.apply(this,arguments);}
Sprite_ATB.prototype.constructor = Sprite_ATB;

Sprite_ATB.prototype = Object.create(Sprite_Base.prototype);

Sprite_ATB.prototype.initialize = function(battler) {
Sprite_Base.prototype.initialize.call(this);
this.initMember();
this.setBattler(battler);
};

Sprite_ATB.prototype.initMember = function() {
this.anchor.x = 0.5;
this.anchor.y = 1;
this._homeX = 0;
this._homeY = 0;
this._finalX = 0;
this._finaly = 0;
this._battler = null;

};

Sprite_ATB.prototype.setBattler = function(battler) {
this._battler = battler;
};

Sprite_ATB.prototype.update = function() {
Sprite_Base.prototype.update.call(this);
while(ImageManager.isReady() != true){
this.loadBitmap();
}
if(this._battler && ImageManager.isReady()){
this.updateVisibility();
this.bitmap = ImageManager.loadSystem("ATB_" + this._battler.name());
} else {
this.bitmap = ImageManager.loadSystem("");
}
};
//===============================================================================
// => END : Sprite_ATB
//===============================================================================

//==============================================================================
// ■ Spriteset_Battle
//------------------------------------------------------------------------------
// The set of sprites on the battle screen.
//==============================================================================

NioPlugin.ATB_GUI.createUpperLayer = Spriteset_Battle.prototype.createUpperLayer;
Spriteset_Battle.prototype.createUpperLayer = function(){
NioPlugin.ATB_GUI.createUpperLayer.call(this);
this.createATBLayout();
this.createATBFace();
};

Spriteset_Battle.prototype.createATBLayout = function() {
this._layout = new Sprite();
this._layout.bitmap = ImageManager.loadSystem("atbLayout");
this._layout.x = 100;
this._layout.y = 100;
this.addChild(this._layout);
};

Spriteset_Battle.prototype.createATBFace = function() {
this.createEnemieFaces();
this.createActorFaces();
};

Spriteset_Battle.prototype.createEnemieFaces = function() {};

// create the ATB actorFace for the atb bar
Spriteset_Battle.prototype.createActorFaces = function() {
this._atbActor = [];
for (var i = 0; i < this._atbActor.length; i++) {
this._atbActor = new Sprite_ATB(i);
this._battleField.addChild(this._atbActor);
};
};

// from a exemple I am unsure if this could work actually
for (var i = $gameParty.members().length - 1; i >= 0; i--) {
var actor = $gameParty.members();
}






thanks for any further answer
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Unless you have to, don't iterate in reverse. Most JS engines expect you to iterate forwards, so they will optimize for this case. I would use one of those alternatives:


var members = $gameParty.members(), actor;
for(var i = 0, max = members.length; i < max; ++i) {
actor = members;
}

// or

var members = $gameParty.members();
for(var i = 0, actor; actor = members[i++]; ) {

}


Though the second version assumes, that all values are non-falsy, because it will break as soon as "actor" is assigned a falsy value (for example undefined, which is the intended way for this to break).


Both of those examples cache values and declare variables outside the loop. This is because the performance cost for reading a variable increases with its scope. $gameParty is a global variables and as such one of the most cost intensive to read. Also, creating a variable costs performance (although less than it would to read $gameParty during every iteration), which is why you should not do this inside a loop.


If you really want to loop in reverse, do it like this:


var members = $gameParty.members(), actor;
for(var i = members.length; i--; ) {
actor = members;
}


This has the added benefit, that you could remove the current actor from "members", without causing the loop to break, because its index isn't relevant in subsequent iterations.
 
Last edited by a moderator:

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
actually this half help me..and half not helping me...actually I am not that dumb for not know how work a for loop ect


the problem is how do I initialize ...my class using this loop
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,867
Messages
1,017,062
Members
137,575
Latest member
akekaphol101
Top