Learning about "functions" and "if"

Hey guys just trying to learn about functions and if statements.

I’m just trying to do something really basic. I have a circle and a square on the stage.
when you click on the square the circle will go to “circle._x = 300” when the circle is at 300 I want to make it when you click on the square the circle will go “circle._x = 45”

moveCircle = function () {
    
    circle._x = 300;
    if (circle._x >= 300) {
        circle._x = 45;
    }
}

square.onRelease = function() {
    moveCircle();
    trace(circle._x + "is the position of the circle");
}

thanks

I think you just need to remove circle._x = 300; from the moveCircle function.

The way you have it, when you click on square, the first thing it does is move it to 300 no matter what, every time you click the square… which is not what you are looking for.
If you intended to place the cicrle at 300 x at the beginning of the movie, then copy the line above all functions.



moveCircle = function () {
    
       if (circle._x >= 300) {
        circle._x = 45;
    }
}

square.onRelease = function() {
    moveCircle();
    trace(circle._x + "is the position of the circle");
}

For the second case, you will need to go a little further and use the Else statement.



moveCircle = function () {
    
       if (circle._x >= 300) {
        circle._x = 45;
       } else if (circle._x == 45) {
        circle._x = 300;
       }
}

square.onRelease = function() {
    moveCircle();
    trace(circle._x + "is the position of the circle");

}

To shorten up your code a bit, and maybe give you a better understanding on functions.


square.onRelease = moveCircle();

Perfect thanks guys.

[QUOTE=ajcates;2345077]To shorten up your code a bit, and maybe give you a better understanding on functions.


square.onRelease = moveCircle();

[/QUOTE]

If you’re going to be giving advice, it probably should be right:

square.onRelease = moveCircle;

So thats why it kept calling the function without me pressing on the square.

Thanks