我只是写出我的代码,我保存它来运行它,它不再工作了,我找不到问题,因为我在这方面相当新手,我正在使用p5.js,这是我的代码:
var s;
function setup()
{
createCanvas(700, 700);
s = new Snake();
}
function draw()
{
background(51);
s.update();
s.show();
}
function Snake()
{
this.x = 0;
this.y = 0;
this.xspeed = 1;
this.yspeed = 0;
this.update = function()
{
this.x = this.x + this.xspeed;
this.y = this.y + this.yspeed;
}
this.show = function()
{
fill(255);
rect(this.x, this.y, 10, 10);
}
this.direction(x, y)
{
this.xpeed = x;
this.yspeed = y;
}
}下面是HTML:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.sound.min.js"></script>
<script src="main().js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<style> body {padding: 0; margin: 0;} </style>
</head>
<body>
</body>
</html>发布于 2017-07-01 02:53:21
this.direction(x, y)应为this.direction = function(x, y)
运行以下代码:
var s;
function setup()
{
createCanvas(700, 700);
s = new Snake();
}
function draw()
{
background(51);
s.update();
s.show();
}
function Snake()
{
this.x = 0;
this.y = 0;
this.xspeed = 1;
this.yspeed = 0;
this.update = function()
{
this.x = this.x + this.xspeed;
this.y = this.y + this.yspeed;
}
this.show = function()
{
fill(255);
rect(this.x, this.y, 10, 10);
}
this.direction = function(x, y)
{
this.xpeed = x;
this.yspeed = y;
}
}<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.sound.min.js"></script>
<script src="main().js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<style> body {padding: 0; margin: 0;} </style>
</head>
<body>
</body>
</html>
发布于 2017-10-08 22:25:08
尝试将函数this.direction(x,y)更改为this.direction = function(x.y)
发布于 2019-05-15 09:58:39
我相信你必须用this.direction = function(x, y)修改这条指令this.direction(x, y)
https://stackoverflow.com/questions/44852980
复制相似问题