简体   繁体   English

您可以访问数组内的javascript自定义类对象吗?如何访问?

[英]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++){ 
  • JavaScript is case-sensitive JavaScript区分大小写
  • JavaScript requires that property names be spelt correctly JavaScript要求属性名称拼写正确

It is length not Lenght . 不是length而是Lenght

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中的函数中访问数组对象? - How to access array objects inside a Function in javascript? 您如何访问匿名函数内部的javascript类属性是同一类 - How can you access a javascript class property inside of an anonymous function is the same class 如何访问对象函数(javascript)中的objects变量? - How can i access the objects variable inside an objects function (javascript)? 您能解释一下JSON对象如何存储在Javascript对象中吗? - Can you explain how JSON objects are stored inside a Javascript object? 我如何访问javascript函数返回的数组中的对象 - How do i access the objects inside the array returned by a javascript function 如何访问 Javascript 中的对象数组内的元素以获取 movieapp 原型 - How to access elements inside an array of objects in Javascript for a movieapp prototype 如何访问在中指定的自定义类对象的Java列表 <s:iterator> 我的javascript函数中的jsp文件标签? - How to access the java List of a custom class objects specified in <s:iterator> tag of jsp file inside my javascript function? 如何向JavaScript数组中的所有对象添加函数? - How can you add a function to all objects in a JavaScript array? 您可以在Javascript中创建对对象数组的引用吗? - Can you create references to an array of objects in Javascript? 如何在javascript中其他对象内部的对象内部访问属性 - How can I access properties inside objects inside other objects in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM