Search code examples
javascriptif-statementbackground-colorgsap

If statement put in wrong place


In order to make some test on TweenMax I'm doing a simple "catch-the-color" game however it looks like I do some error in my code as everything is fine but a single if statement that should end the game. I guess that the problem is not with the color changer itself but with the place where I put the if statement. I'm pretty new to programming so there are high chances I made some silly mistake.

You can take a look at the entire page here: https://codepen.io/IvanRoselli/pen/vbqjoo

And here's the Javascript I used

var colorCircle = document.getElementById('circle');
var startBtn = document.getElementById('start-btn');
var catchBtn = document.getElementById('catch-btn');
var mainTitle = document.getElementById('main-title');
var colorChange = new TimelineMax({repeat: -1});
var circleBackgroundColor = document.getElementById('circle').style.backgroundColor;
var colorCircle = document.getElementById('circle');
var startBtn = document.getElementById('start-btn');
var catchBtn = document.getElementById('catch-btn');
var mainTitle = document.getElementById('main-title');
var colorChange = new TimelineMax({repeat: -1});
var circleBackgroundColor = 
document.getElementById('circle').style.backgroundColor;

function playPause(){
    colorChange.paused(!colorChange.paused()); 
}

function getIt(){
     $('#catch-btn').click(function(){
        if(circleBackgroundColor === 'blue'){
            mainTitle.innerHTML = "YOU WON!";
            return;
        } else {
            mainTitle.innerHTML = "Try again pressing CATCH"; 
            playPause();
        }
    })
}

function startGame(){
    getIt();
    $('#start-btn').css("display", "none");
    $('#catch-btn').css("display", "inline");
    $(colorCircle).css("cursor", "pointer");
    colorChange.from(colorCircle, .5, {backgroundColor: 'orange', ease: SteppedEase.config(1)});
    colorChange.to(colorCircle, .5, {backgroundColor: 'red', ease: SteppedEase.config(1)});
    colorChange.to(colorCircle, .5, {backgroundColor: 'pink', ease: SteppedEase.config(1)});
    colorChange.to(colorCircle, .5, {backgroundColor: 'yellow', ease: SteppedEase.config(1)});
    colorChange.to(colorCircle, .5, {backgroundColor: 'blue', ease: SteppedEase.config(1)});
    colorChange.to(colorCircle, .5, {backgroundColor: 'green', ease: SteppedEase.config(1)});
    colorChange.to(colorCircle, .5, {backgroundColor: 'cyan', ease: SteppedEase.config(1)});


};


$('#start-btn').click(function() {
    startGame();
});

Basically the H1 should change between "You Won" if you catch the blue or "Try Again" for other colors.

Thanks in advance for your help and advice.


Solution

  • I have made some changes and its working fine for me.
    https://codepen.io/anon/pen/NoZBPz
    

    Problem is its giving rgb value and app is also not stopping on won also changed the if condition.

    if((document.getElementById('circle').style.backgroundColor) === 'rgb(0, 0, 255)'){
                mainTitle.innerHTML = "YOU WON!";
              playPause();
                return;
            } else {
                mainTitle.innerHTML = "Try again pressing CATCH"; 
                playPause();
            }