unityscript中的对象敌人

发布于 2024-10-06 04:15:13 字数 287 浏览 3 评论 0原文

我可以在unitycript中作为一个具有“健康”、“速度”和“耐力”的对象“敌人”。并删除该对象,还是一次删除多个对象?

class enemy {
    health = 100
    speed = 10
    stamina = 200
}

for 0 to 10
{
    enemyBig = new Enemy ()
}


if keydown (space)
{
    delete all.enemyBig 
}

unityscript中的这段代码如何正确?

I can do in unitycript as an object "enemy" that has "health", "speed" and "stamina". And to delete the object, or several at once?

class enemy {
    health = 100
    speed = 10
    stamina = 200
}

for 0 to 10
{
    enemyBig = new Enemy ()
}


if keydown (space)
{
    delete all.enemyBig 
}

How would this code in unityscript correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖正好 2024-10-13 04:15:13

在 javascript 中,要定义类,您需要创建一个函数。

function Enemy {
   this.health = 100;
   ....
};

然后将方法放在原型上(因为javascript使用原型继承。)

Enemy.prototype.theMethod = function () { ... };

当您执行上述操作来定义方法时,该方法就是实例方法;即每个对象都有自己的方法副本。如果你想定义一个“静态”方法,你只需将该方法放在类上,

Enemy.staticMethod = function() {...};

区别在于前者你可以做

var enemy1 = new Enemy();
enemy1.theMethod(); // this in the theMethod refers to enemy1

,而后者你可以做

Enemy.staticMethod(); // there is only one staticMethod for the entire class.

In javascript, to define a class you create a function.

function Enemy {
   this.health = 100;
   ....
};

and then to put methods on the prototype (because javascript uses prototypal inheritance.)

Enemy.prototype.theMethod = function () { ... };

when you do the above to define a method, the method is an instance method; i.e. every object has its own copy of the method. If you want to define a 'static' method, you just put the method on the class

Enemy.staticMethod = function() {...};

the difference is for the former you can do

var enemy1 = new Enemy();
enemy1.theMethod(); // this in the theMethod refers to enemy1

and for the latter you do

Enemy.staticMethod(); // there is only one staticMethod for the entire class.
日久见人心 2024-10-13 04:15:13

要同时追踪所有敌人,您需要一个数组。那里有一个很好的教程:

To keep track of all your enemies at once, what you need is called an array. There's a good tutorial there:

玩物 2024-10-13 04:15:13

实现对象:

function Enemy {
    this.health = 100;
    ...
};

Enemy.prototype.attack = function() {
    this.health -= 10;
    ...
};

var boogerMonster = new Enemy();
boogerMonster.attack();

就删除对象而言,如果没有其他引用,垃圾收集器将处理该对象。

但是,从问题来看,仅仅知道这些信息并不能让你走得更远。 拿一本关于 JavaScript 的书和/或做一些在线研究以真正了解 JavaScript 的基础知识。

To implement an object:

function Enemy {
    this.health = 100;
    ...
};

Enemy.prototype.attack = function() {
    this.health -= 10;
    ...
};

var boogerMonster = new Enemy();
boogerMonster.attack();

As far as deleting an object, the garbage collector will take care of the object if it has no other references to it.

But, from the question, it looks like just knowing this information won't carry you far. Grab a book on JavaScript and/or do some online research to really understand the fundamentals of JavaScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文