저는 간단한 자바 스크립트 게임을 작성하고 있습니다. 아바타와 장애물. 이 순간 나는 자바 스크립트 "사각형"에서 클래스를 시뮬레이션했습니다. 코드는 여기에 있습니다 :이 거친 클래스에서 3 개의 클래스를 만드는 방법은 무엇입니까?
function rectangle (x,y,width,height,verticalvelocity,dangerous,image)
{
//returns info
this.x=x;
this.y = y;
this.height= height;
this.width=width;
this.verticalvelocity=verticalvelocity
this.jump= jump;
this.image=image
this.dangerous=dangerous
this.drawImg= function() {
context.drawImage(this.image,this.x,this.y,this.width,this.height)}
//getters
this.ycormdd=function() {return (this.y + (this.height /2));} //returns the y coor of the middlepoint
this.xcormdd= function() {return (this.x + (this.width /2));} //returns the x coor of the middlepoint
this.danger= function() {if (this.dangerous == 0) {return true} else {return false}};
//the setters
this.setdangerous= function (dangerous) {this.dangerous = dangerous};
this.setx= function (x) {this.x = x};
this.sety= function (y) {this.y = y};
this.setwidth= function (width) {this.width = width};
this.setheight= function (height) {this.height = height};
this.setimage= function (image) {this.image = image};
this.setverticalvelocity= function (verticalvelocity) {this.verticalvelocity=verticalvelocity};
}
문제는 내가
var avatar= new rectangle (....)
var obstacle= new rectangle (...)
를 입력하고 어떻게하는지 그건 그냥 그래서 내 아바타와 장애물 모두 직사각형 "클래스"를 사용한다는 것입니다. 내가 아는 한 te는 3 개의 클래스를 만들 필요가있다. 하나의 클래스 아바타, 하나의 클래스 장애물 및 하나의 클래스 사각형. 내 장애물과 아바타 모두 사각형으로 표시되기 때문에 아바타와 사각형 "클래스"모두 사각형 클래스에 액세스 할 필요가 있다고 생각합니다. 그러나 어떻게해야하는지 전혀 알 수 없습니다. 누군가 제발 도와 줄 수 있니? 미리 감사드립니다. 내 미래의 사각형 "클래스"는 다음과 같이 보일 것입니다 :
function rectangle (x,y,width,height,image)
{
//returns info
this.x=x;
this.y = y;
this.height= height;
this.width=width
this.image=image
//draws a rectangle
this.drawImg=function() {
context.drawImage(this.image,this.x,this.y,this.width,this.height)}
//getters
this.ycormdd=function() {return (this.y + (this.height /2));} //returns the y coor of the middlepoint
this.xcormdd= function() {return (this.x + (this.width /2));} //returns the x coor of the middlepoint
//the setters
this.setx= function (x) {this.x = x};
this.sety= function (y) {this.y = y};
this.setwidth= function (width) {this.width = width};
this.setheight= function (height) {this.height = height};
this.setImage = function (image) {this.image = image};
}
하지만 아바타와 장애물 클래스를 생성해야합니다. 내가 아바타 클래스에 필요한
기능은 다음과 같습니다
- setverticalvelocity
- getverticalvelocity
- (+ 직사각형의 기능)
그리고 내 장애물에 대한, 나는이 필요합니다
- 위험한
- getdangerous.
- (사각형에서 + 기능)
나는 누군가가 내 질문을 이해 바랍니다. : p
당신은 기능 rectangle.prototype을 사용해야합니다. 예 : this.drawImg 대신 rectangle.prototype.drawImg. –
아바타와 장애물을위한 새로운 수업을 만들고 싶습니다. 그것들은 직사각형 클래스를 상속받습니다. 이 기사를 확인해보십시오. http://www.crockford.com/javascript/inheritance.html – Robodude
사실이 기사를 읽은 후에,이 기사가 도움이 될 것 같습니다. http://phrogz.net/JS/classes /OOPinJS2.html – Robodude