我是编程的初学者,我正在创建一个简单的2人游戏,玩家在屏幕底部左右移动并收集掉落的物体 - 具有不同属性的物体,即加速玩家/慢他们失望了。
我创建了一个要崩溃的对象,但不确定如何创建多个对象。
非常感谢任何帮助!
///// this is the food.js for falling food //////
var Food = function(type){
var position = {
x: 0,
y: 0
};
var foodElement = null;
// this will run at every frame
this.render = function(gravity){
console.log(position.x, position.y);
position.y += gravity;
foodElement.style.top = position.y + "px";
}
// This will only run once
function init(){
foodElement = document.createElement("div");
foodElement.classList.add("food");
foodElement.classList.add(type);
position.x = parseInt(Math.random() * window.innerWidth);
foodElement.style.top = position.y + "px";
foodElement.style.left = position.x + "px";
var gameBoard = document.getElementById("gameboard");
gameBoard.appendChild(foodElement);
}
init();
}
function (createFood) {
}