[英]Can you access javascript custom class objects inside an array and how?
Im working on a RPG-game project made in plain javascript just for fun, but can't seem to understand how javascript build-in classes work since they behave quite a lot differently than I am used to in Java or C#. 我正在从事一个用纯JavaScript制作的RPG游戏项目,只是出于娱乐目的,但似乎无法理解javascript内置类的工作原理,因为它们的行为与我在Java或C#中习惯的行为有很大不同。 So here is the problem:
所以这是问题所在:
If I have made custom class something along the lines like this: 如果我已经按照以下方式制作了自定义类:
Class Player_animation{
constructor(animationX,animationY,animation_state,xPos,yPos,destX,destY){
this.animationX = animationX;
.
. (the basic setup for all the attributes as the first one);
.
set animationX(value){
this._animationX = value;
}
//all the setters as that one
update(){
if(this._animationX===480 && this._animation_state==='idle')
this._animationX=0;
else if(this._animationX===720 && this._animation_state !== 'attack'){
this._animationX=0;
}
else if(this._animationX===840){
this._animationX=0;
this._animationY = 0;
this._animation_state = 'idle';
}
if(this._xPos!== this._destX || this._yPos!== this._destY){
if(this._xPos<this._destX){
this._animation_state = 'facing_right';
this._animationY = 240;
}
if(this._xPos>this._destX){
this._animation_state = 'facing_left';
this._animationY = 360;
}
}
else{
if(this._animation_state === 'facing_right')
this._animationY = 0;
if(this._animation_state === 'facing_left')
this._animationY = 120;
if(this._animation_state!=='attack'){
this._animation_state = 'idle';
}
}
}
}
And i can call an new made class object no problem in my program like this: 我可以在程序中像这样调用新的类对象:
var player_animation = new Player_animation(0,0,'idle',0,0,0,0);
player_animation.update();
Can I somehow make an array of these custom classes that I call with that function. 我能以某种方式对我用该函数调用的这些自定义类进行数组制作。 I have tried two of the following approaches like this:
我已经尝试过以下两种方法,例如:
var array = [];
array.push[player_animation,player_animation2];
for(var unit in array){
unit.update();
}
Second approach i tried that does not work: 我尝试过的第二种方法不起作用:
var array = [];
array.push[player_animation,player_animation2];
for(var i = 0; i < array.Lenght; i++){
array[i].update();
}
Working code goes through this loop (I know that I should limit the fps here): 工作代码经过这个循环(我知道我应该在这里限制fps):
function frame(){
update();
requestAnimationFrame(frame);
}
function update(){
player_animation.update();
enemy_animation.update();
}
requestAnimationFrame(frame);
Is something like this even possible in plain javascript? 在普通的javascript中甚至可能发生这种情况? My game loop and update work fine with all of the objects defined called separately but that will be a hassle after there will be 10+ objects in game.
我的游戏循环和更新可以与单独定义的所有对象一起正常工作,但是在游戏中有10多个对象之后,这将很麻烦。 In Java i was able to store all of the game_objects in an array where their functions would be called through a for loop so finding it hard to understand why it does not work as that in javascript.
在Java中,我能够将所有game_objects存储在一个数组中,通过for循环可以调用它们的功能,因此很难理解为什么它不能像javascript中那样起作用。
You have many problems, none of them relating to how classes work in JavaScript. 您有很多问题,都与类在JavaScript中的工作方式无关。
array.push[player_animation,player_animation2];
push
is a function . push
是一种功能 。 You call it with ()
not []
. 您使用
()
而不是[]
调用它。
for(var unit in array){
in
loops over the property names in an object . in
循环访问对象中的属性名称 。 Avoid using it for arrays. 避免将其用于数组。 If you do use it, then you would need
array[unit]
to get the value and not simply unit
. 如果您确实使用它,那么您将需要
array[unit]
来获取值,而不仅仅是unit
。
for(var i = 0; i < array.Lenght; i++){
It is length
not Lenght
. 不是
length
而是Lenght
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.