我遇到了以下问题:我正在创建一个创建子弹的对象构造函数,当我尝试使用它时,“子弹”对象在控制台日志中返回undefined。我不知道什么是错的。
抱歉凌乱的代码和糟糕的英语。并提前感谢。 编辑:如果你打算投票,请告诉我原因,以便我知道如何改进。
LinkList
var canvas, ctx, Player,players, intervalo,x,y, keyUp,keyDown,keyLeft,keyRight,rX,rY,rRadius,rAngle,hei, orbitals, Orbital, mouseX, mouseY, tiros, Bullet, bullets;
keyUp = 87; keyDown = 83; keyRight = 68; keyLeft = 65;
hei = 0;
test = 0; tiros = 0;
function load(){
canvas = document.getElementById('box');
ctx = canvas.getContext('2d');
ctx.lineWidth = 3;
function player(){
this.x = canvas.width/2;
this.y = canvas.height/2;
this.speed = 2;
this.width = 10;
this.height = 10;
this.up = false;
this.down = false;
this.left = false;
this.right = false;
}
function orbital(){
this.x = x;
this.y = y;
this.width = 3;
this.height = 3;
this.radius = 20;
this.angle = hei;
this.speed = 0;
}
Orbital = new Array();
for (var i = 0;i < 10; i++){
Orbital.push(new orbital())
orbitals = Orbital[i];
hei += 10;
orbitals.angle = hei;
}
Player = new Array();
Player.push(new player())
players = Player[0];
window.addEventListener("keydown", checkKeyDown, false);
function checkKeyDown(e) {
if (event.keyCode == keyUp){
players.up = true;
console.log("Up key is pressed");
} else if (event.keyCode == keyDown){
players.down = true;
console.log("Down key is pressed");
} else if (event.keyCode == keyLeft){
players.left = true;
console.log("Left key is pressed");
} else if (event.keyCode == keyRight){
players.right = true;
console.log("Right key is pressed");
}
}
window.addEventListener("keyup", checkKeyUp, false);
function checkKeyUp(e){
if (event.keyCode == keyUp){
players.up = false;
console.log("Up key is released");
} else if (event.keyCode == keyDown){
players.down = false;
console.log("Down key is released");
} else if (event.keyCode == keyLeft){
players.left = false;
console.log("Left key is released");
} else if (event.keyCode == keyRight){
players.right = false;
console.log("Right key is released");
}
}
document.onmousemove = mouseMove;//Detectando posição do mouse;
function mouseMove(e) {
e = e || window.event
mouseX = e.pageX;
mouseY = e.pageY;
document.getElementById('demo').innerHTML = mouseX + " " + mouseY;
}
//Here is where the bullet begins:
function bullet(){
this.x = players.x;
this.y = players.y;
this.width = 5;
this.height = 5;
this.speed = 20;
var dX = mouseX - players.x;
var dY = mouseY - players.y;
var distance = Math.sqrt(dX*dX + dY*dY);
this.speedX = (dX/distance) * 10;
this.speedY = (dY/distance) * 10;
}
Bullet = new Array();
canvas.onmousedown = function () {
Bullet.push(new bullet())
tiros++;
bullets = Bullet[tiros];
document.getElementById('demo2').innerHTML = tiros;
};
}
function play(){
intervalo = setInterval(animate, 1000/60);
}
function animate(){
ctx.clearRect(0,0,canvas.width,canvas.height);// Os if's tem q estar em uma ordem específica onde o if que detecta o movimento tem q estar antes do if q detecta a colisão com o muro.
//Key detection, that makes the player move.
if (players.up){
players.y -= players.speed;
}
if (players.down){
players.y += players.speed;
}
if (players.right){
players.x += players.speed;
}
if (players.left){
players.x -= players.speed;
}
// End
//Detecção de colisão no muro
if (players.y < 50){
players.y += players.speed;
}
if (players.y > canvas.height - players.height - 50){
players.y -= players.speed;
}
if (players.x < 50){
players.x += players.speed;
}
if (players.x > canvas.width - players.width - 50){
players.x -= players.speed;
}
//end
ctx.fillStyle = "brown";
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(50,50);
ctx.moveTo(0,500);
ctx.lineTo(50,450);
ctx.moveTo(700,0);
ctx.lineTo(650,50);
ctx.moveTo(700,500);
ctx.lineTo(650,450);
ctx.strokeRect(50,50,600,400);
ctx.stroke();
angleToTurn = -Math.atan2((mouseX-15) - players.x, (mouseY-15) - players.y )*(180/Math.PI);//Pega a posição do players.x e do mouse para criar o angulo de movimento;
for (var z = 0; z < Orbital.length; z++){
ctx.fillStyle = "red";
orbitals = Orbital[z];
orbitals.angle = angleToTurn+85;
orbitals.x = players.x+(orbitals.radius*Math.cos(orbitals.angle*(Math.PI/180)))+(players.width/2)-2;
orbitals.y = players.y+(orbitals.radius*Math.sin(orbitals.angle*(Math.PI/180)))+(players.height/2)-2;
if (orbitals.angle > 360){
orbitals.angle = 0;
}
ctx.fillRect(orbitals.x,orbitals.y,orbitals.width,orbitals.height);
}
//Here is the part that returns undefined at the console log
if (tiros >= 1){
bullets.x += bullets.speedX;
bullets.y += bullets.speedY;
document.getElementById('demo2').innerHTML = "if";
ctx.fillRect(bullets.x,bullets.y,bullets.width,bullets.height);
}
ctx.fillStyle = "black";
ctx.fillRect(players.x,players.y,players.width,players.height);
}
答案 0 :(得分:0)
问题似乎是tiros
在被使用之前递增,应该在之后。尝试:
canvas.onmousedown = function () {
Bullet.push(new bullet())
bullets = Bullet[tiros++];
document.getElementById('demo2').innerHTML = tiros;
};