我正在用javascript做一个游戏,我正在用一个类来保存所有的精灵。然而,当我尝试运行它时,safari给出了“类:保留字‘SyntaxError’的意外用法”。我找不到它应该这么做的理由。下面是我的代码:
var spritesContext;
//Sprite class
class sprite{
constructor(imageName, imageX, imageY, imageHeight, imageWidth, context, canvas){
this.imageName = imageName;
this.imageX = imageX;
this.imageY = imageY;
this.imageHeight = imageHeight;
this.imageWidth = imageWidth;
this.canvas = canvas;
this.context = context;
spritesContext = context;
console.log("Sprite constructed");
}
draw(){
var character = new Image();
character.src = "../Images/" + this.imageName + ".png";
character.onload = function(){
spritesContext.drawImage(character, this.imageX, this.imageY, this.imageWidth, this.imageHeight);
console.log("Inner Draw is working.");
}
console.log("Draw is working.");
}
function getHeight(){
return this.imageHeight;
}
function getWidth(){
return this.imageWidth;
}
function getX(){
return this.imageX;
}
function getY(){
return this.imageY;
}
function moveUpX(e){
this.imageX = (this.imageX + e);
}
function moveUpY(e){
this.imageY = (this.imageY + e);
}
function moveBackX(e){
this.imageX = (this.imageX - e);
}
function moveBackY(e){
this.imageY = (this.imageY - e);
}
function changeImage(e){
this.imageName = e;
}
function getImage(){
return this.imageName;
}
function changeX(e){
this.imageX = e;
}
function changeY(e){
this.imageY = e;
}
}发布于 2016-03-01 10:35:51
要使您的代码工作,更具体地说是关键字class,您需要在严格模式下启用某些浏览器来解释它。
要处于严格模式,请将以下内容添加到文件的顶部:
'use strict';发布于 2019-12-17 00:08:25

当被问到这个问题时,Safari不支持"class“。
https://stackoverflow.com/questions/35713635
复制相似问题