Javascript 面向对象问题
我在javasriptoop方面有问题。目前,我有两个短文件 player.jsJavascript 面向对象问题,javascript,oop,Javascript,Oop,我在javasriptoop方面有问题。目前,我有两个短文件 player.js function Player(x,y,source){ this.hp = 100; this.speed = 5; this.posx = x; this.posy = y; this.img = new Raster({ source: source, position: new Point(x,y) }); this.
function Player(x,y,source){
this.hp = 100;
this.speed = 5;
this.posx = x;
this.posy = y;
this.img = new Raster({
source: source,
position: new Point(x,y)
});
this.displayPlayer();
}
Player.prototype = {
constructor: Player,
displayPlayer:function(){
this.img.position = new Point(this.posx, this.posy);
}
};
index.html
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://paperjs.org/assets/js/paper.js" type="text/javascript"></script>
<style>html{background-color:#DDDDDD;}</style>
<script src="player.js" type="text/javascript"></script>
<script src="powerup.js" type="text/javascript"></script>
<script type="text/paperscript" canvas="myCanvas">
red = new Player(100,100,'red.png');
</script>
</head>
<body>
<canvas id="myCanvas" width="600" height="600" style="background-color:#FFFFFF !important;"></canvas>
</body>
</html>
html{背景色:#DDDDDD;}
红色=新玩家(100100,'red.png');
我得到的只是一个没有任何东西的空白屏幕
链接:
光栅
未定义,给您一个参考错误
是否确实正确链接到/Paper.js 可能您有一些纸张配置错误。所有paper.js结构都隐藏在paper对象中。所以你必须这样称呼它
new paper.Raster(....
function Player(x,y,source){
this.hp = 100;
this.speed = 5;
this.posx = x;
this.posy = y;
this.img = new paper.Raster({
source: source,
position: new paper.Point(x,y)
});
this.displayPlayer();
}
Player.prototype = {
constructor: Player,
displayPlayer:function(){
this.img.position = new paper.Point(this.posx, this.posy);
}
};
代码是这样的
new paper.Raster(....
function Player(x,y,source){
this.hp = 100;
this.speed = 5;
this.posx = x;
this.posy = y;
this.img = new paper.Raster({
source: source,
position: new paper.Point(x,y)
});
this.displayPlayer();
}
Player.prototype = {
constructor: Player,
displayPlayer:function(){
this.img.position = new paper.Point(this.posx, this.posy);
}
};
这不是一个解决方案,这是一个脏代码。Uncaught ReferenceError:光栅未定义Hanks,让我检查一下。我不知道paper.js,但可能此“”中的代码必须使用paper private scope运行。重新检查示例。