[英]eval() function not working with global scope
I am using CreateJS to animate an object. 我正在使用CreateJS为对象设置动画。
I want to move the circle
shape to a position that changes in a for loop. 我想将
circle
移动到for循环中更改的位置。
var x = {
time: 200,
x: 0,
y: 0
};
I am going to change this variable after every time the shape animates in a for loop. 每当形状在for循环中设置动画后,我将更改此变量。
for(var i = 0; i < 4; i++) {
createjs.Tween.get(circle)
.to({x: hits.x, y: hits.y}, hits.time);
hits.x += 200;
hits.y += 200;
}
I have the increment value set to 4, but I am going to change it. 我将增量值设置为4,但是我将对其进行更改。 This is why I am not directly using the
.to
function every time I am animating it. 这就是为什么我每次给动画设置动画时都没有直接使用
.to
函数的原因。 I do not want to hardcode every loop for every single increment. 我不想为每个循环的每个增量进行硬编码。 I need to make a single function that can take in any increment value and animate it accordingly.
我需要制作一个可以接受任何增量值并相应地为其设置动画的函数。
I tried this out, but then realized that the animation doesn't take place if another animation is in progress. 我尝试了一下,但随后意识到如果正在进行另一个动画,则动画不会发生。
I then used an eval function to add all the values to a string, and then doing it as a whole, as the .to()
function is chainable. 然后,我使用eval函数将所有值添加到字符串中,然后将其作为一个整体进行处理,因为
.to()
函数是可链接的。
var bounceString = "createjs.Tween.get(circle)";
for(var i = 0; i < 4; i ++) {
bounceString += ".to({x: hits.x, y: hits.y}, hits.time)";
hits.x += 200;
hits.y += 200;
}
bounceString += ";";
eval(bounceString);
By using the eval function with the string, I can use any increment value and animate the shape accordingly without having to hardcode it. 通过对字符串使用eval函数,我可以使用任何增量值并相应地对形状进行动画处理,而不必对其进行硬编码。 However, there is a problem.
但是,有一个问题。
I have no clue why, but it says that the hits
variable is undefined. 我不知道为什么,但是它说
hits
变量是不确定的。 I also tried to define that variable in the bounceString
, but it did not work out. 我也尝试在
bounceString
定义该变量,但没有成功。 I also tried making it a global variable (as the whole code is in a function). 我还尝试将其设置为全局变量(因为整个代码都在一个函数中)。
The error that shows up when I try to define hits
in bounceString
is this- 当我尝试在
bounceString
定义hits
时显示的错误是:
This happens only when I am trying to define the variable in the string itself. 仅当我尝试在字符串本身中定义变量时,才会发生这种情况。
I know that the eval
function is dangerous, but this is the only way. 我知道
eval
函数很危险,但这是唯一的方法。 Is there any other way I can do this? 我还有其他方法可以做到吗?
I have no idea what the API for createjs
is. 我不知道
createjs
的API是什么。 However, I would expect that storing what you get from createjs.Tween.get()
and then doing the .to()
would be far better than anything eval()
does for you. 但是,我希望存储您从
createjs.Tween.get()
得到的createjs.Tween.get()
,然后执行createjs.Tween.get()
.to()
会比eval()
为您做的任何事情都要好得多。
Consider the following code: 考虑以下代码:
// Store the tween
var c = createjs.Tween.get(circle);
// Create your keyframes
var keyframes = [],
keyframe;
for (keyframe = 0; keyframe < 4; keyframe += 1) {
keyframes.push({
x: 200 * keyframe,
y: 200 * keyframe
});
}
// Do your iterations on tween
keyframes.reduce(function (prev, curr, i, arr) {
return prev.to({
x: arr[i].x,
y: arr[i].y
});
}, c.to({
x: keyframes[0].x,
y: keyframes[0].y
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.