0
私のゲームには別の問題があります(これと同じものがquestion)。 p要素は、現在のプレイヤーアクションで更新されていません。ここでは、コード(fiddle)は次のとおりです。要素が現在のプレイヤーのアクションで更新されていません
HTML
<canvas id='canvas'>Your browser does not support the canvas element.</canvas>
<p id='lol'>Action</p>
CSS
#canvas{
border: 1px solid black;
}
はJavaScript
//Get canvas and context
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
//Capture keypresses
var keys = [];
document.onKeyDown = function (e) {keys[e.keyCode] = true;};
document.onKeyUp = function (e) {keys[e.keyCode] = false;};
//Set game variables
var characters = {
charList: []
};
var player = {
character: 'none',
currentAction: 'none',
health: '100',
energy: '100',
x: 0,
y: 0,
dx: 0,
dy: 0,
facing: 'right'
};
//Character constructor
function Character (Name, Strength, Speed, Intelligence, Reflexes, Age, Height, Weight, Weapon, UsesMagic, MagicType, Armor, Hair, Eyes, Skin, Clothes, Species, Type, Accessories) {
this.name = Name;
this.strength = Strength;
this.speed = Speed;
this.intelligence = Intelligence;
this.reflexes = Reflexes;
this.age = Age;
this.height = Height;
this.weight = Weight;
this.weapon = Weapon;
this.usesMagic = UsesMagic;
this.magicType = MagicType;
this.armor = Armor;
this.hair = Hair;
this.eyes = Eyes;
this.skin = Skin;
this.clothes = Clothes;
this.species = Species;
this.type = Type;
if (Accessories) {
this.accessories = Accessories;
}
characters.charList.push(Name);
}
characters.Xantar = new Character('Xantar', 1, 1, 1, 1, 1, 1, 1, 'sword', true, 'derp', [], 'hair is brown\?', 'duh', 'foo', [], 'animale', 'wolf', []);
alert(characters.Xantar.weapon + ' ' + characters[characters.charList[0]].type);
//Activate mainloop and get value for pausing purposes
var mainloopInterval = setInterval(mainloop, 5);
//Main game loop
function mainloop(){
//Allow player to do actions
if(player.currentAction == 'none' || player.currentAction == 'jump'){
if(keys[38]){
player.currentAction == 'jump';
player.dy = -10;
}
if(keys[32]){ //space
if(keys[65]){ //a
if(keys[68]){ //d
if(keys[83]){ //s
player.currentAction = 'asdAttack';
} else {
player.currentAction = 'adAttack';
}
} else if(keys[83]){
player.currentAction = 'asAttack';
} else {
player.currentAction = 'aAttack';
}
} else if(keys[68]){
if(keys[83]){
player.currentAction = 'dsAttack';
} else {
player.currentAction = 'dAttack';
}
} else if(keys[83]){
player.currentAction = 'sAttack';
} else{
player.currentAction = 'spaceAttack';
}
}
} else {
//Action handling
}
document.getElementById('lol').innerHTML = player.currentAction;
}
私は助けていただきありがとうございます。前もって感謝します!
お困りですか? 'p'要素は警告の後に' Action'から 'none'に明確に更新されますか? – h2ooooooo
p要素は、キーを押すと何かに変更されるはずです。 – Feathercrown
これは、 'document.onkeydown'ではなく' document.onKeyDown'を使用しているからです。大事な事件。 – h2ooooooo