这是我的代码:
Javascript:
var currency = {
coins: 0,
golds: 0,
orb: 0
};
var resources = {
coals: 0,
irons: 0,
silvers: 0,
diamonds: 0
};
var player = {
HP: 0,
baseHP: 100,
weaponHP: 0,
ATK: 0,
baseATK: 1,
weaponATK: 0,
DEF: 0,
baseDEF: 5,
weaponDEF: 0,
DEX: 0,
baseDEX: 5,
weaponDEX: 0,
crit: 0,
baseCrit: 5,
weaponCrit: 0,
level: 1,
currentEXP: 0,
expLeft: 10
};
var unlocked = {
shop: false,
tab: false,
numberOfGolds: false,
mine: false,
resources: false,
throw10Coins: false,
woodenSword: false,
ironSword: false,
hatchet: false,
woodenShield: false,
spikeShield: false,
blade: false,
battleAxe: false,
broadSword: false,
mace: false,
bomb: false,
angleSword: false,
hacker: false
};
var weapon = {
hatchet: {
atk: 2,
def: -1,
dex: 0,
hp: 0,
crit: 0
},
woodenSword: {
atk: 5,
dex: 0,
def: 0,
hp: 0,
crit: 0
},
ironSword: {
atk: 10,
crit: 5,
dex: 0,
def: 0,
hp: 0
},
blade: {
atk: 25,
crit: 20,
dex: 10,
hp: 0,
def: 0
},
mace: {
atk: 30,
def: 5,
dex: -1,
hp: 0,
crit: 0
},
battleAxe: {
atk: 50,
def: 5,
dex: 0,
hp: 0,
crit: 0
},
broadSword: {
atk: 100,
def: 20,
dex: 0,
crit: 0,
hp: 0
},
woodenShield: {
atk: 0,
def: 10,
dex: 0,
hp: 0,
crit: 0
},
spikeShield: {
def: 15,
atk: 5,
dex: 0,
crit: 0,
hp: 0
},
bomb: {
atk: 0,
def: 0,
crit: 0,
hp: 0,
dex: -5
},
angleSword: {
atk: 500,
def: 100,
crit: 25,
hp: 100,
dex: 10
}
}
function stat(x) {
reStat();
player.baseATK = 1;
player.baseDEF = 5;
player.baseDEX = 5;
player.baseHP = 100;
player.baseCrit = 5;
player.weaponATK = weapon[x].atk;
player.weaponDEF = weapon[x].def;
player.weaponDEX = weapon[x].dex;
player.weaponHP = weapon[x].hp;
player.weaponCrit = weapon[x].crit;
player.ATK = player.baseATK + player.weaponATK;
player.DEF = player.baseDEF + player.weaponDEF;
player.DEX = player.baseDEX + player.weaponDEX;
player.HP = player.baseHP + player.weaponHP;
player.crit = player.baseCrit + player.weaponCrit;
document.getElementById('atk').innerHTML = player.baseATK + player.weaponATK;
document.getElementById('def').innerHTML = player.baseDEF + player.weaponDEF;
document.getElementById('dex').innerHTML = player.baseDEX + player.weaponDEX;
document.getElementById('hp').innerHTML = player.baseHP + player.weaponHP;
document.getElementById('crit').innerHTML = player.baseCrit + player.weaponCrit + '%';
}
function reStat() {
player.ATK = 0;
player.DEF = 0;
player.DEX = 0;
player.crit = 0;
player.HP = 0;
}
var gold = {
health: 30
}
function move(flag) {
if (flag == 'true') {
player.x += 1
}
if (flag == 'false') {
void(0)
}
}
function start(place) {
if (place == 'goldMine') {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
var player = {
x: 0,
y: 198,
speed: 3
};
var slope = {
x: 148,
y: 198,
up: false
}
var gold = {
x: 200,
y: 185,
health: 30,
mined: false,
x1: 240,
y1: 185,
mined1: false
}
var time = 30;
var canDamage = false;
var time = 50;
switchQuest();
ctx.font = '15px Monospace';
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillText('\\o/', player.x, player.y);
ctx.fillText("__________________", 0, 197);
ctx.fillText("/", slope.x, slope.y);
ctx.fillText("______________________________________________________", 155, 185);
if (!gold.mined) {
ctx.fillText("/O\\", gold.x, gold.y);
}
ctx.fillText("/O\\", gold.x1, gold.y1);
player.x += player.speed;
if (player.x >= slope.x - 17 && !slope.up) {
player.y -= 13;
slope.up = true;
}
if (player.x >= gold.x - 17 && !gold.mined) {
player.speed = 0;
time -= 1;
}
if (time <= 0) {
stat();
time = 50;
gold.health -= player.weaponATK;
}
if (gold.health == 0) {
player.speed = 3;
gold.mined = true;
}
console.log("Gold Health: " + gold.health);
console.log("ATK: " + player.ATK);
console.log("Time: " + time);
requestAnimationFrame(draw)
}
}
requestAnimationFrame(draw)
}
window.setInterval(
function() {
document.getElementById('numberOfCoins').innerHTML = 'You got ' + currency.coins + ' coins!';
document.getElementById('numberOfGolds').innerHTML = 'You mined ' + currency.golds + ' golds!';
document.getElementById('numberOfCoals').innerHTML = resources.coals;
document.getElementById('numberOfSilvers').innerHTML = resources.silvers;
document.getElementById('numberOfIrons').innerHTML = resources.irons;
document.getElementById('numberOfDiamonds').innerHTML = resources.diamonds;
所以这就是我的问题。在Javascript部分中,我在开始时声明player.ATK = 0
。但是在start()
函数中。当我console.log()
player.ATK
时,它会返回undefined
。如果我console.log()
它在函数内部或在控制台内,它通常会返回变量。谁能帮我这个?这可以只使用Javascript来解决吗?
谢谢!
答案 0 :(得分:1)
你的start()函数定义了一个本地播放器var:
function start(place) {
if (place == 'goldMine') {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
var player = { // <<-- Here
x: 0,
y: 198,
speed: 3
};
这有效地隐藏了此范围内的全局播放器变量。这个新玩家var没有和ATK属性,这就是为什么它似乎未定义。