我创造了一个有一个英雄形象的游戏,他的任务是在画布上收集水果。
事实上,每个水果都在画布上拥有它自己的到期时间。 例如,香蕉将出现5秒,橙色将出现10秒等。 我试图做的事情是创建一个对象,该对象的每个实例都有一个在选定时间后更改布尔值的计时器我的代码如下:
function apple(timeToStay,score){
this.kind = "apple";
this.timeTostay = timeToStay;
this.score=score;
this.imgsrc = "images/apple.png";
this.x = 32 + (Math.random() * (canvas.width - 64));
this.y = 32 + (Math.random() * (canvas.height - 64));
this.removeMe = false;
setTimeout(function() { this.removeMe=true; }, timeToStay*1000);
return this;
}
你可以看到我认为用实例设置超时会在5秒后触发,例如如果我创建它var obj = apple(5,5)
在开头obj.removeMe应该是假的但是在5秒后它应该变成真。
答案 0 :(得分:2)
这种情况正在发生,因为您作为参数传递给setTimeout
方法的功能并没有保留相同的上下文,因为Javascript's lexical scoping。
您可以使用Function.prototype.bind
覆盖该行为,例如:
setTimeout(function() { this.removeMe=true; }.bind(this), timeToStay*1000);
答案 1 :(得分:0)
这应该有效:
import java.util.Scanner;
import java.util.Random;
public class Resttest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random rn = new Random();
//Game variables
int round = 0;
int score = 0;
int distance_target = 0;
double gravity_constant = 9.8;
double angle;
double speed;
boolean on = false;
//introduction
System.out.println("********************************\nWelcome to the Zombie Defenders! \nYou are one of the last remaining defenders of human kind, armed with nothing but your trusty catapult! \nAre you ready to show those zombies what you're made of? \n(If you prefer a zombie apocalypse, Enter q to quit and any other letter to continue): \n Oh noes! They're coming get ready! ");
distance_target = rn.nextInt(100); //generates a random number to the the maximum distance of 100
do{
String w = scanner.nextLine();
if(w.equals("q")){
System.exit(0);
}else{
round += 1;
System.out.println("Round " + round);
System.out.println("The target is " + distance_target + " meters away");
System.out.print("Set the angle(degrees)!: ");
angle = scanner.nextDouble();
System.out.print("Set the speed(m/s)!: ");
speed = scanner.nextDouble();
//sets variables into equation and calculates the distance
double distance = ((speed*speed)*(Math.sin(2*angle)))/gravity_constant;
if(distance - distance_target >= 20 || distance - distance_target <= -20){//miss and went long
System.out.println(distance);
System.out.println("Current Score: " + score);
System.out.println("Ouch, that's a miss... went way too far... Were you trying to hit the sky?\n You were " + (distance - distance_target) + " meters off");
score -= 1;
System.out.println("(Input q to quit)");
}else if(distance-distance_target <= -20){
System.out.println(distance);
System.out.println("Current Score: " + score);
System.out.println("Ouch, that's a miss... went way too short... Who in their right mind thought it was a good idea to let you manage the catapult?\n You were " + (distance - distance_target) + " meters off");
score -= 1;
System.out.println("(Input q to quit)");
}else if(distance - distance_target >= 5){// close and went long
System.out.println(distance);
System.out.println("Current Score: " + score);
System.out.println("Close but no cigar :/ went right over their heads.\n You were " + (distance - distance_target) + " meters off");
System.out.println("(Input q to quit)");
}else if(distance-distance_target <= -5){
System.out.println(distance);
System.out.println("Current Score: " + score);
System.out.println("Close but no cigar :/ went down right in front of them. At least you gave them a good scare.\n You were " + (distance - distance_target) + " meters off");
System.out.println("(Input q to quit)");
}else if (distance - distance_target <= 5 || distance - distance_target >= -5){// hit
System.out.println(distance);
System.out.println("Current Score: " + score);
System.out.println("You got those suckers! Nice shot!");
System.out.println("(Input q to quit)");
break;
}
}
}while(on != true);
}