# Learning about "functions" and "if"

**URL:** <https://forum.kirupa.com/t/learning-about-functions-and-if/263715>\
**Category:** flash\
**Created:** [June 18, 2008, 3:47pm UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715 "2008-06-18T15:47:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vxd](https://avatars.discourse-cdn.com/v4/letter/v/848f3c/32.png) [@vxd](https://forum.kirupa.com/u/vxd)\
**Post date:** [June 18, 2008, 3:47pm UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715/1 "2008-06-18T15:47:09Z")

</div>

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”

```auto
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

---

<div class="post-metadata">

**Author:** ![artriste](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@artriste](https://forum.kirupa.com/u/artriste)\
**Post date:** [June 18, 2008, 5:17pm UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715/2 "2008-06-18T17:17:42Z")

</div>

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.

```auto

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.

```auto

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");

}

```

---

<div class="post-metadata">

**Author:** ![ajcates](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ajcates/32/2435_2.png) [@ajcates](https://forum.kirupa.com/u/ajcates)\
**Post date:** [June 18, 2008, 5:51pm UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715/3 "2008-06-18T17:51:02Z")

</div>

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

```auto

square.onRelease = moveCircle();

```

---

<div class="post-metadata">

**Author:** ![vxd](https://avatars.discourse-cdn.com/v4/letter/v/848f3c/32.png) [@vxd](https://forum.kirupa.com/u/vxd)\
**Post date:** [June 19, 2008, 12:46am UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715/4 "2008-06-19T00:46:18Z")

</div>

Perfect thanks guys.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 19, 2008, 1:57am UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715/5 "2008-06-19T01:57:10Z")

</div>

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

```auto

square.onRelease = moveCircle();

```

[/QUOTE]

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

```auto
square.onRelease = moveCircle;

```

---

<div class="post-metadata">

**Author:** ![vxd](https://avatars.discourse-cdn.com/v4/letter/v/848f3c/32.png) [@vxd](https://forum.kirupa.com/u/vxd)\
**Post date:** [June 19, 2008, 7:17am UTC](https://forum.kirupa.com/t/learning-about-functions-and-if/263715/6 "2008-06-19T07:17:27Z")

</div>

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

Thanks
