Search code examples
javascriptcreatejs

How to decrease the scaleX using createJS as it reach a limit?


I am using createJS and have used the scaleX property to increase the scaling size of an object like so :

this.main_mc.fish_mc.scaleX = this.main_mc.fish_mc.scaleY += .1;

I have already attached this to a click handler.

I was thinking how do I write a condition that once it reach more than 1.5 scale level, then it starts to decrease by .1. And then another condition that once it is lesser than 0.5 scale level, then it increases by .1 again.

Thanks for helping!

Cheers!


Solution

  • If that's all that you want to do, then it's pretty straight-forward, although I can see why it's a bit tricky. You basically want to use a variable to determine the 'direction' of the scaling:

    var scaling = 1;
    
    function clickListener(e)
    {
        if (this.main_mc.fish_mc.scaleX <= 0.5){
            scaling = 1;
        } else if (this.main_mc.fish_mc.scaleX >= 1.5){
            scaling = -1;
        }
        this.main_mc.fish_mc.scaleX = this.main_mc.fish_mc.scaleY += 0.1 * scaling;
    }