Skip to main content
Inspiring
October 5, 2024
Answered

how to set a condition to apply to multiole frames

  • October 5, 2024
  • 1 reply
  • 389 views

Hi guys any help is greatly appreciated, what I'm trying to do is this ->

 

On button click, if the timeline is on frame 6, 12, 18 or 24, goto frame 54

If the timeline is on any other frame besides those ones, goto frame 73

 

I took a few guesses and tried to find the answer but no luck.

 

this.bulbut2.addEventListener("click", playjump.bind(this));
function playjump()
{
if (this.currentFrame == 6,12,18,24)
{
this.gotoAndPlay(54)
}
else ()
{
this.gotoAndPlay(73)
}
}


// I even ecrewed up the simple else condition  😞

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    There are a few different ways you can do this. For example:

    // Logical OR (||) operator
    function playjump()
    {
    	if (this.currentFrame === 6 || this.currentFrame === 12 || this.currentFrame === 18 || this.currentFrame === 24)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Switch statement
    function playjump()
    {
    	switch(this.currentFrame)
    	{
    		case 6:
    		case 12:
    		case 18:
    		case 24:
    			this.gotoAndPlay(54);
    			break;
    		default:
    			this.gotoAndPlay(73);
    	}
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Else if
    function playjump()
    {
    	if (this.currentFrame === 6)
    		this.gotoAndPlay(54);
    	else if (this.currentFrame === 12)
    		this.gotoAndPlay(54);
    	else if (this.currentFrame === 18)
    		this.gotoAndPlay(54);
    	else if (this.currentFrame === 24)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Object literal
    var targetFrames = { 6, 12, 18, 24 };
    
    function playjump()
    {
    	if (targetFrames[this.currentFrame] !== undefined)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Object literal and ternary operator
    var targetFrames = { 6, 12, 18, 24 };
    
    function playjump()
    {
    	this.gotoAndPlay(targetFrames[this.currentFrame] !== undefined ? 54 : 73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Array
    var targetFrames = [ 6, 12, 18, 24 ];
    
    function playjump()
    {
    	
    	if (targetFrames.indexOf(this.currentFrame) > -1)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Array and ternary operator
    var targetFrames = [ 6, 12, 18, 24 ];
    
    function playjump()
    {
    	this.gotoAndPlay(targetFrames.indexOf(this.currentFrame) > -1 ? 54 : 73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));


    I hope these help.

    Regards,
    JC

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    October 5, 2024

    Hi.

     

    There are a few different ways you can do this. For example:

    // Logical OR (||) operator
    function playjump()
    {
    	if (this.currentFrame === 6 || this.currentFrame === 12 || this.currentFrame === 18 || this.currentFrame === 24)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Switch statement
    function playjump()
    {
    	switch(this.currentFrame)
    	{
    		case 6:
    		case 12:
    		case 18:
    		case 24:
    			this.gotoAndPlay(54);
    			break;
    		default:
    			this.gotoAndPlay(73);
    	}
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Else if
    function playjump()
    {
    	if (this.currentFrame === 6)
    		this.gotoAndPlay(54);
    	else if (this.currentFrame === 12)
    		this.gotoAndPlay(54);
    	else if (this.currentFrame === 18)
    		this.gotoAndPlay(54);
    	else if (this.currentFrame === 24)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Object literal
    var targetFrames = { 6, 12, 18, 24 };
    
    function playjump()
    {
    	if (targetFrames[this.currentFrame] !== undefined)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Object literal and ternary operator
    var targetFrames = { 6, 12, 18, 24 };
    
    function playjump()
    {
    	this.gotoAndPlay(targetFrames[this.currentFrame] !== undefined ? 54 : 73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Array
    var targetFrames = [ 6, 12, 18, 24 ];
    
    function playjump()
    {
    	
    	if (targetFrames.indexOf(this.currentFrame) > -1)
    		this.gotoAndPlay(54);
    	else
    		this.gotoAndPlay(73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));
    
    
    // Array and ternary operator
    var targetFrames = [ 6, 12, 18, 24 ];
    
    function playjump()
    {
    	this.gotoAndPlay(targetFrames.indexOf(this.currentFrame) > -1 ? 54 : 73);
    }
    
    this.bulbut2.addEventListener("click", playjump.bind(this));


    I hope these help.

    Regards,
    JC

    Inspiring
    October 5, 2024

    THANK YOU SO MUCH!!