不能在JavaScript中创建一个对象的两个实例
Can't create two instances of one object in JavaScript
我创建了这段代码来渲染一些动画。这似乎只适用于一个动画。然而,如果我尝试创建两个或多个对象的实例,一切都会混淆,没有任何渲染了。
var animate = function(id,sprite){
var sprites = sprite;
var imageID = id;
var arize = document.getElementById(imageID);
var spriteID;
var animationSprites;
console.log("before switch: sprite: " + sprite + "imageID: " + imageID + "element: " + arize + "animationSprites: " + animationSprites)
switch(sprites) {
case "appear":
animationSprites = appear;
spriteID = "appear";
break;
case "die":
animationSprites = die;
spriteID = "die";
break;
case "slap":
animationSprites = slap;
spriteID = "slap";
break;
case "walk":
animationSprites = walk;
spriteID = "walk";
break;
default:
animationSprites = idle;
spriteID = "idle";
}
console.log(sprites.length)
console.log("after switch: sprite: " + sprite + "imageID: " + imageID + "element: " + arize + "animationSprites: " + animationSprites)
this.animateThis = function() {
arize.src = animationSprites[i];
i++;
setTimeout(function() {
if (i < animationSprites.length) {
console.log(i)
showings();
if (i== animationSprites.length -1) {
i=0;
}
}
},50);
}
function showings() {
var showZombieTwo = new animate("imgs", "slap");
showZombieTwo.animateThis();
//var showZombieOne = new animate("imgs1", "idle");
//showZombieOne.animateThis();
}
如何同时渲染多个动画?
showing
函数位于animate
函数内部,这意味着动画单个精灵将动画所有精灵。当然,当有一个,但只有一个时,这是有效的。
我重写了一些代码,使它容易从一个动画切换到另一个,因为我认为这是最初的意图。
你还能说出spriteID
变量是什么吗?我在代码中没有看到任何地方使用它。
// assumed variables
var i = 0,
appear = [/* array of src strings */],
die = [/* array of src strings */],
slap = [/* array of src strings */],
walk = [/* array of src strings */];
var animate = function(imageID, sprite) {
var that = this;
var sprites = sprite;
var arize = document.getElementById(imageID);
var spriteID; // What is the purpose of this variable?
var animationSprites;
console.log("before switch: sprite: " + sprite + "imageID: " + imageID + "element: " + arize + "animationSprites: " + animationSprites);
this.currentAnimationFrame = 0;
this.setCurrentAnimation = function(animationName) {
// reset animation
that.currentAnimationFrame = 0;
switch (animationName) {
case "appear":
animationSprites = appear;
spriteID = "appear";
break;
case "die":
animationSprites = die;
spriteID = "die";
break;
case "slap":
animationSprites = slap;
spriteID = "slap";
break;
case "walk":
animationSprites = walk;
spriteID = "walk";
break;
default:
animationSprites = idle;
spriteID = "idle";
}
return spriteID;
}
this.animateThis = function() {
arize.src = animationSprites[that.currentAnimationFrame];
that.currentAnimationFrame++;
setTimeout(function() {
if (that.currentAnimationFrame >= animationSprites.length) {
that.currentAnimationFrame = 0;
}
that.animateThis();
}, 50);
}
// setting the init animation
this.setCurrentAnimation(sprite);
}
var showZombieTwo = new animate("imgs", "slap");
showZombieTwo.animateThis();
// braaaain
showZombieTwo.setCurrentAnimation("walk");
// finally...
showZombieTwo.setCurrentAnimation("die");
相关文章:
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- 如何通过osascript处理同一应用程序的两个实例
- 在两台不同的服务器上运行同一node.js应用程序的两个实例
- 一个用户脚本的两个实例如何在帧之间进行通信
- Colorbox插件正在加载其自身的两个实例
- javascript中的Ruby插值在两个实例中的一个实例中不起作用
- Javascript — 相同元素的两个实例
- 两个实例包含相同的值
- 不能使用ezinearticles作者小部件的两个实例
- Widget的两个实例只有一个实例在工作
- 删除角色的最后两个实例
- 一个指令的两个实例正在使用同一个模板实例
- 如何删除数组中重复对象的两个实例
- 当同一个模板的两个实例在应用程序的多个地方使用时,Ember的奇怪行为
- 为什么这个属性在一个实例中打印,而不是在类似的实例中打印,即使它在两个实例中都是可枚举的
- AngularJS指令的两个实例,更改为2会影响第一个实例
- Express(4.0)自定义中间件可以在router的两个实例上运行,尽管它只声明了一个实例
- 不能在JavaScript中创建一个对象的两个实例
- 为什么我不能得到我的视图的两个实例来渲染?
- 获取url中字符最后两个实例之间的子字符串