嗨,我正试着在游戏中数我的分数。我刚开始使用javascript,并使用CreateJS。我的问题是,我不知道如何才能同时使用Ticker和Click事件。它不起作用..。
function init(){
var stage = new createjs.Stage("myCanvas");
stage.mouseEventsEnabled = true;
createjs.Ticker.interval = 1500;
createjs.Ticker.addEventListener("tick", handleTick);
var statPoint = new createjs.Text("Punkte:", "bold 20px Arial", "#000000");
statPoint.x = 750;
var currentPoints = new createjs.Text("0", "20px Arial", "#000000");
currentPoints.x= 850;
var victim = new createjs.Bitmap("Opfer.png");
victim.scaleX = 0.4;
victim.scaleY = 0.4;
stage.addChild(statPoint);
stage.addChild(currentPoints);
stage.addChild(victim);
victim.addEventListener("click", handleClick);
function handleTick(event){
victim.x = 850*Math.random();
victim.y = 550*Math.random();
stage.update();
}
function handleClick(event){
currentPoints.text = parseInt(currentPoints.text + 1);
}
}
发布于 2015-11-04 00:22:18
您可能正在处理范围问题。您已经在init方法中使用var 定义了您的victim和currentPoints变量,因此它只在那里可用。这意味着您的handleTick和handleClick方法不能访问这些变量。您的控制台中可能存在未定义的错误。
将变量更改为在init方法外部声明,就可以在处理程序方法中访问它们。
var currentPoints, victim;
function init() {
// Other code
currentPoints = values;
victim = value;
}https://stackoverflow.com/questions/33455263
复制相似问题