JavaScript游戏中的面向对象的设计
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript游戏中的面向对象的设计,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10770字,纯文字阅读大概需要16分钟。
内容图文
// constructor function
function MyExample() {
// property of an instance when used with the 'new' keyword
this.isTrue = true;
};
MyExample.prototype.getTrue = function() {
return this.isTrue;
}
MyExample();
// here, MyExample was called in the global context,
// so the window object now has an isTrue property—this is NOT a good practice
MyExample.getTrue;
// this is undefined—the getTrue method is a part of the MyExample prototype,
// not the function itself
var example = new MyExample();
// example is now an object whose prototype is MyExample.prototype
example.getTrue; // evaluates to a function
example.getTrue(); // evaluates to true because isTrue is a property of the
// example instance
// Base class
function Character() {};
Character.prototype.health = 100;
Character.prototype.getHealth = function() {
return this.health;
}
// Inherited classes
function Player() {
this.health = 200;
}
Player.prototype = new Character;
function Monster() {}
Monster.prototype = new Character;
var player1 = new Player();
var monster1 = new Monster();
player1.getHealth(); // 200- assigned in constructor
monster1.getHealth(); // 100- inherited from the prototype object
function ParentClass() {
this.color = 'red';
this.shape = 'square';
}
function ChildClass() {
ParentClass.call(this); // use 'call' or 'apply' and pass in the child
// class's context
this.shape = 'circle';
}
ChildClass.prototype = new ParentClass(); // ChildClass inherits from ParentClass
ChildClass.prototype.getColor = function() {
return this.color; // returns "red" from the inherited property
};
- 它不是 DRY 的。类名称和原型随处重复,让读和重构变得更为困难。
- 构造函数在原型化期间调用。一旦开始子类化,就将不能使用构造函数中的一些逻辑。
- 没有为强封装提供真正的支持。
- 没有为静态类成员提供真正的支持。
- 所有原型化都是用对象组合(可以在一条语句中定义类和子类)完成的。
- 用一个特殊的构造函数为将在创建新的类实例时运行的逻辑提供一个安全之所。
- 它提供了静态类成员支持。
- 它对强封装的贡献止步于让类定义保持在一条语句内(精神封装,而非代码封装)。
// create an abstract, basic class for all enemies
// the object used in the .extend() method is the prototype
var Enemy = Base.extend({
health: 0,
damage: 0,
isEnemy: true,
constructor: function() {
// this is called every time you use "new"
},
attack: function(player) {
player.hit(this.damage); // "this" is your enemy!
}
});
// create a robot class that uses Enemy as its parent
//
var RobotEnemy = Enemy.extend({
health: 100,
damage: 10,
// because a constructor isn't listed here,
// Base.js automatically uses the Enemy constructor for us
attack: function(player) {
// you can call methods from the parent class using this.base
// by not having to refer to the parent class
// or use call / apply, refactoring is easier
// in this example, the player will be hit
this.base(player);
// even though you used the parent class's "attack"
// method, you can still have logic specific to your robot class
this.health += 10;
}
});
- 在用户没有盯着游戏时减少客户机上的工作量
- 节省移动设备上的用电。
- 如果更新循环与呈现循环有关联,那么可以有效地暂停游戏。
// requestAnim shim layer by Paul Irish
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var Engine = Base.extend({
stateMachine: null, // state machine that handles state transitions
viewStack: null, // array collection of view layers,
// perhaps including sub-view classes
entities: null, // array collection of active entities within the system
// characters,
constructor: function() {
this.viewStack = []; // don't forget that arrays shouldn't be prototype
// properties as they're copied by reference
this.entities = [];
// set up your state machine here, along with the current state
// this will be expanded upon in the next section
// start rendering your views
this.render();
// start updating any entities that may exist
setInterval(this.update.bind(this), Engine.UPDATE_INTERVAL);
},
render: function() {
requestAnimFrame(this.render.bind(this));
for (var i = 0, len = this.viewStack.length; i < len; i++) {
// delegate rendering logic to each view layer
(this.viewStack[i]).render();
}
},
update: function() {
for (var i = 0, len = this.entities.length; i < len; i++) {
// delegate update logic to each entity
(this.entities[i]).update();
}
}
},
// Syntax for Class "Static" properties in Base.js. Pass in as an optional
// second argument to.extend()
{
UPDATE_INTERVAL: 1000 / 16
});
内容总结
以上是互联网集市为您收集整理的JavaScript游戏中的面向对象的设计全部内容,希望文章能够帮你解决JavaScript游戏中的面向对象的设计所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。