0

I have a script to reset game pieces, but I cannot get the variable to call the other variable to get the end value.

Here are the variables that contain original position, I need the value of these in my function:

red_1_x = $('#red_1').css('left');
red_1_y = $('#red_1').css('top');

Here is my function that calls the animation function:

$('.piece').mouseover(function(e) {
    var id = $(this).attr('id');
    resetGroundShoe(id);
});

$(this).attr('id') is for example: red_1

And here is the animation function:

function resetPiece(id){
    if(!id){
        alert('no id');
    }
    else{
        x = id+'_x';
        y = id+'_y';
        gotoX = x;
        gotoY = y;
        $('#'+id).animate({left: x}, 250, function(){
            $('#'+id).animate({left: y}, 250);
        });
    }
}

For some reason gotoX and gotoY are not the numeric value of red_1_x and red_1_y

1 Answer 1

1

Assuming those are global variables you can use window[x] and window[y] to access the globals with the names stored in x/y.

However, using globals is pretty bad style and it really sounds like you should pass those values as arguments!

Sign up to request clarification or add additional context in comments.

1 Comment

Or obj[x] in the general case, window[x] being a special case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.