0

使用该setInterval函数,我尝试使用 javascript 在code.org上每秒创建一个精灵,所以我的第一个版本的代码看起来像

setInterval( function() {
  createSprite(200,200,20,20)
}, 1000)

我的问题是,放入函数setInterval内部Draw会导致它无法正常工作,并且在一秒钟后的每个滴答声都会创建一个精灵,并且当它setInterval没有放入函数Draw时,它也不会像我想要的那样绘制精灵。

我尝试过的一种解决方案是将Draw函数放入其中,setInterval但无法识别并给出错误消息"Draw is defined, but it is not called in your program"

是否有不同的版本setInterval可以在Draw函数内部工作,成功放入Draw内部的setInterval方法,即使精灵在外部也能显示出来的Draw方法,或者解决这个问题的不同方法?

具体来说,我正在寻找的是每秒创建一个精灵,让它显示在屏幕上,能够在每次生成新精灵时为每个精灵选择不同的速度,并且能够将此功能放在一个如果功能并且仍然按预期工作。

一段代码显示了部分有效的东西,如下所示:

https://studio.code.org/projects/gamelab/ApXezLpMzV3TfEfHx1CrhFyuteYDSKWe_6Hx0NdJgnc

它的工作原理是它每秒生成一个精灵,但是如果我尝试为一个生成的精灵分配一个速度,它只适用于第一个精灵,如下所示:

https://studio.code.org/projects/gamelab/ApXezLpMzV3TfEfHx1CrhFyuteYDSKWe_6Hx0NdJgnc

我认为可以解决的唯一方法是声明一个类,然后在 setInterval 函数中创建这个类的一个精灵,但我不知道该怎么做。

4

2 回答 2

3

所以我认为你的问题是精灵只在一秒钟后生成,对吧?

如果是这样,请试试这个:

createSprite(200,200,20,20);
setInterval( function(){ createSprite(200,200,20,20)},1000);
于 2020-08-02T14:34:20.703 回答
0

看评论:

// just a helper function to generate random velocities
function random_in_range(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min }

var monster = createSprite(200,200,20,20);
monster.velocityX = 5;

setInterval(function() {
  // as you needed to set velocity for a newly created sprite
  // you need to have this sprite as a variable
  var fireball = createSprite(monster.x,monster.y,4,4);
  // now you can set it's velocity
  fireball.velocityX = random_in_range(1,6);
  // basically that's it.
  // as you don't do anything else with a "fireball"
  // you can just forget about it, and you don't need to save it anywhere
}, 1000);

function draw() {
  background("white");
  createEdgeSprites();
  monster.bounceOff(edges);
  drawSprites();
}

与工作代码的链接


查看您的原始代码和您的问题,我认为您可能对 JS 有一些基本的误解(对不起,如果我判断错误)。在代码的注释中解释了它们。我还添加了一些行来说明我的观点:

var spawner = createSprite(200,200,20,20);
var remember_spawner = spawner // ADDED
// After a new sprite created it does't change 'spawner' variable
createSprite(150,200,15,15).velocityY = 3; // ADDED
console.log(remember_spawner === spawner) // ADDED // you see it's the same

// you need to assign the newly created sprite to a variable
// you can think of this as:
// I need give them different names so that they can understand to whom am I taking to
// and if you don't name them they won't listen to you at all
var spawner2 = createSprite(100,200,10,10);  // ADDED
spawner2.velocityY = 1  // ADDED // now you cat change velocity of another sprite
console.log(spawner !== spawner2) // ADDED // and it is another sprite

// var thing_to_be_spawned = createSprite(spawner.x,spawner.y,4,4);
  // You probably don't need this. And you don't use the variable anyway.
  // This sprite created at the same time as the spawner.
  // But if you need it, and you want it to move - change it's velocity (thing_to_be_spawned.velocityX = something) not the velocity of spawner
  // And it would be better if you'll name the function passed to setInterval
  // and call it instead of repeating it. Like so:
  // function fire() { ... } // define the function
  // fire() // call the function
  // setInterval(fire, 1000) // call the function every second

setInterval(function(){
  console.log("This function executed every second") // ADDED
  console.log("Fire!") // ADDED
  createSprite(spawner.x,spawner.y,4,4);
  console.log("Done") // ADDED
},1000);

console.log("Next line executed only once") // ADDED
spawner.velocityX=5;
// The fact that it's placed after setInterval() does mean that
// it will be executed after call to setInterval()
// but it doesn't mean that it will be executed after the function passed to setInterval(). Let's call it fire(), shall we?
// Actually, the fire() function will be called only after all the code in this file
// And setInterval() is also called only once, only the fire() passed to it called every second

function draw() { // Here you don't call draw() you only define it, so that code.org can call it whenever it wants
  // looking at the code draw function we can't tell when or how often it's called
  // it depends on the implementation details of code.org.
  // We can add console.log to find out
  console.log("Draw called") // ADDED
  background("white");
  createEdgeSprites();
  spawner.bounceOff(edges);
  drawSprites();
}
console.log("'fire()' not called yet") // ADDED

控制台日志可以在studio.code.org的Debug Console中看到。它通过单击Debug Commands打开。用它!我的意思是。我也可以说“使用调试命令!” 但是调试在studio.code.org中实现得很差,并且可能会产生误导......您绝对应该尝试一下,但您应该信任.console.log()console.log()

于 2020-08-13T16:20:15.083 回答