我正嘗試使用javascript創建一個小遊戲,並且一切都很順利,直到我必須開始一些東西時,我需要實例化一個「子彈」並將其開啓,但當調用「新的子彈()「我有一個」Uncaught TypeError:undefined不是一個函數「。 我如何實例化另一個對象方法內的對象?在javascript實例化方法中實例化一個對象
這是我做
function bullet(){
//here it would be state for the bullet, like x and y and thigs
console.log("bullet created");
}
function gun(){
//state for the gun, amount of bullets and sort
console.log("gun created");
this.fire = function(){
//here we instantiate a bullet and fire it
console.log("begin fire");
var bullet = new bullet();
console.log("created bullet to fire");
}
}
var gun = new gun();
gun.fire();
將'var gun'和'var bullet'重命名爲其他內容... – Givi