简体   繁体   English

如何从补间函数中调用数组以优化代码

[英]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. 我有一个handleKeyDown函数,并在按下时执行补间函数。 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. 我想要的是将数组和forEach函数放在补间函数中,而不要放在if条件中。 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)

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM