当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How can I call the array from the tween function to optimize my code

问题描述:

I have a handleKeyDown function and onpress it executes a tween function. In this code I am tweening multi objects in an array like this :

function handleKeyDown(e) {

if (!e) {

var e = window.event;

}

if (e.keyCode == '1'.charCodeAt(0)) {

var imagearray = [ChickenMale3Container, ChickenMale2Container, ChickenMaleContainer];

imagearray.forEach(function (element) {

rotateanscleElement(element);

});

}

function rotateanscleElement(element) {

createjs.Tween.get(element, {

loop: false

})

.to({

regY: element.height / 2,

regX: element.width / 2,

x: element.x + 138,

y: element.y + 133,

rotation: 0

}, 1)

.to({

scaleX: 0.7,

scaleY: 0.7

}, 1000, createjs.Ease.bounceOut)

.to({

regY: element.height / 2,

regX: element.width / 2,

rotation: 360

}, 1000)

.to({

scaleX: 1,

scaleY: 1

}, 500, createjs.Ease.bounceOut)

.to({

regY: 0,

regX: 0,

x: element.x,

y: element.y,

rotation: 0

}, 1)

}

What I want is to put the array and the forEach function in the tween function and not in the if condition. How can I make it for more code optimization?

网友答案:
if (e.keyCode=='6'.charCodeAt(0)) {

    multirotateanscleElement();
    }

function multirotateanscleElement(element) {
var imagearray = [ChickenMale3Container,ChickenMale2Container,ChickenMaleContainer];
imagearray.forEach(function(element) {
createjs.Tween.get(element, {loop: false})

    .to({regY:element.height/2, regX:element.width/2,x:element.x+138,y:element.y+133,rotation: 0}, 0)
    .to({scaleX: 0.7, scaleY: 0.7},500, createjs.Ease.bounceOut)
    .to({regY:element.height/2, regX:element.width/2,rotation:360}, 500)
    .to({scaleX: 1, scaleY: 1}, 500, createjs.Ease.bounceOut)
    .to({regY:0, regX:0,x:element.x,y:element.y,rotation: 0}, 0)
    .to({regY:element.height/2, regX:element.width/2,x:element.x+138,y:element.y+133,rotation: 360}, 0)
    .to({scaleX: 0.7, scaleY: 0.7},500, createjs.Ease.bounceOut)
    .to({regY:element.height/2, regX:element.width/2,rotation:0}, 500)
    .to({scaleX: 1, scaleY: 1}, 500, createjs.Ease.bounceOut)
    .to({regY:0, regX:0,x:element.x,y:element.y,rotation: 0}, 0)

 });
分享给朋友:
您可能感兴趣的文章:
随机阅读: