0
良い日、私は自分のコードで問題に遭遇しているようです。JS - カスタムオブジェクトの戻り値を使用できません
:私はカスタムオブジェクトを持っている...私は追加して、 `keysDown配列」からキーを削除し始め2つのイベントリスナに持って ...を私は何かが欠けする必要がありますが、私はそれを見つけることができません
は、それから私は、新しいVectorオブジェクトを返すカスタム関数を持っている:
function Input() {
var v = new Vector(0,0);
if(keysDown["w".toCharCode(0)]) {
v.y = 1;
} else if (keysDown["s".toCharCode(0)]) {
v.y = -1;
}
return v;
}
は、それから私は、毎秒60回の間隔で実行される反復的な機能を持っており、この中に私は自分を翻訳したいです受け取った入力を使用してベクトル:
var charPosition = new Vector(5,5);
setInterval(function(){
var input = Input();
charPosition.translate(input.x, input.y);
},(1000/60));
私はインターバル関数内の別々のvar inputX
、var inputY
でそれを処理していたときに入力が完璧に動作します。しかし、それを抽出してそれから戻り値の型を作成するとすぐに、このように出力は得られません。ここで
は、スクリプトの全体のソースです:私は、関数の入力()ビットを変更することで問題を解決したよう
<script>
addEventListener("keydown", function(e){
keysDown[e.keyCode] = true;
},false);
addEventListener("keyup", function(e){
delete keysDown[e.keyCode];
},false);
var canvasWidth = 640;
var canvasHeight = 480;
var canvas = document.getElementById("canvas");
var keysDown = {};
var oldKeysDown = {};
function Vector(x,y){
this.x = x;
this.y = y;
this.lerp = function(startVector,endVector,percent){
var v = new Vector();
v.x = startVector.x + percent * (endVector.x - startVector.x);
v.y = startVector.y + percent * (endVector.y - startVector.y);
return v;
};
this.translate = function(x,y){
this.x += x;
this.y += y;
};
this.toString = function(){
return "("+this.x+":"+this.y+")";
};
this.set = function(x,y){
this.x = x;
this.y = y;
};
}
function Entity(){
// Instantiate entity variables
this.position = new Vector();
this.sprite = new Image();
this.velocity = new Vector();
this.entityID = "";
this.entity = "";
// Entity update function
// This is where code-side movement and collision detection should be done.
this.update = function(){
this.position.translate(this.velocity.x,this.velocity.y);
// Detect screen-edge
if(this.position.x - this.sprite.width/2 < 0)
{
this.position.x = 0+(this.sprite.width/2);
}
if(this.position.x + this.sprite.width/2 > canvasWidth)
{
this.position.x = canvasWidth-(this.sprite.width/2);
}
if(this.position.y - this.sprite.height/2 < 0)
{
this.position.y = 0+(this.sprite.height/2);
}
if(this.position.y + this.sprite.height/2 > canvasHeight)
{
this.position.y = canvasHeight-(this.sprite.height/2);
}
};
this.init = function(elementID)
{
this.elementID = elementID;
this.entity = document.getElementById(this.elementID);
this.position.x = canvasWidth/2-(char.sprite.width/2);
this.position.y = canvasHeight/2-(char.sprite.height/2);
this.entity.style.left = (this.position.x - this.sprite.width/2) + "px";
this.entity.style.bottom = (this.position.y - this.sprite.height/2)+ "px";
this.entity.innerHTML = "<img src='" + this.sprite.src + "' alt='0'>";
};
// Entity draw method - This ensures that the object on the webpage is updated to the same as the code-values;
this.draw = function(){
this.entity.style.left = (this.position.x - this.sprite.width/2) + "px";
this.entity.style.bottom = (this.position.y - this.sprite.height/2)+ "px";
};
}
function Input(){
var v = new Vector(0,0);
if(keysDown['W'.charCodeAt(0)]){
v.y = 1;
}else if (keysDown['S'.charCodeAt(0)]){
v.y = -1;
}
if(keysDown['D'.charCodeAt(0)]){
v.x = 1;
}else if (keysDown['A'.charCodeAt(0)]){
v.x = -1;
}
return v;
}
// Initializing a new character
var char = new Entity();
char.sprite.src = "spr_character.png";
char.init("character");
// This is where the magic happen, this is set to try and run at 60 frames per second (1000ms/60frames)
setInterval(function(){
var inp = Input();
// Doing console.log(inp.toString()) returns the correct input
char.position.translate(inp.x,inp.y);
char.update();
char.draw();
},(1000/60));
</script>
入力がベクターを作成するのはなぜですか?あなたはxとyを渡すためにそれを使用するだけで奇妙に思えます。 – epascarello
メソッドを決して工場に入れることはありません。 –
あなたは何を意味しているか分かりません。 @epascarello – Hurly