# Looped action on button counting up (or down)

**URL:** <https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296>\
**Category:** flash\
**Created:** [May 14, 2008, 8:15am UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296 "2008-05-14T08:15:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegrrraue](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@thegrrraue](https://forum.kirupa.com/u/thegrrraue)\
**Post date:** [May 14, 2008, 8:15am UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296/1 "2008-05-14T08:15:00Z")

</div>

I’m kind of embarrassed I couldn’t figure this one out, I couldn’t find anything on either of the steps needed to pull this off, so I must be calling them by the wrong name or something.

What I’m doing is making a key listener so that when I hold down to the right, a number (in a text box) counts up, when I hold down left, it counts down. The range for the number is 0-255.

I assume I have to make a for loop that checks at a certain interval whether right or left is being pressed, then some sort of if statement seeing if it’s over 255 or under 0, but for some reason, I can’t figure out how to actually make any of that code.

---

<div class="post-metadata">

**Author:** ![thesean](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@thesean](https://forum.kirupa.com/u/thesean)\
**Post date:** [May 14, 2008, 9:02pm UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296/2 "2008-05-14T21:02:08Z")

</div>

So, do you have the code for the text box counting up and down working? If so, just add an if clause inside that action:

```php

if (Number(_root.myTF.text) > 255) {
    trace("number greater than 255");
} else if (Number(_root.myTF.text) < 0) {
    trace("number less than 0");
}

```

You could also check that condition somewhere else, perhaps with a setInterval function, or whenever a certain button is pressed, etc.

Does that help?

-thesean

---

<div class="post-metadata">

**Author:** ![thegrrraue](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@thegrrraue](https://forum.kirupa.com/u/thegrrraue)\
**Post date:** [May 14, 2008, 11:57pm UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296/3 "2008-05-14T23:57:53Z")

</div>

Yes, that definitely helps. I do not have the counting thing working, though, none of it. The problem is that I have a pretty good idea of what I have to do but no idea how to write the code I need.

So now we have the range limiter, just have to figure out how to write the hold button down and have it count up!

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 15, 2008, 12:06am UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296/4 "2008-05-15T00:06:34Z")

</div>

Here you go:

```auto
count = 0;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.RIGHT) && count < 255) {
count++;
}
if (Key.isDown(Key.LEFT) && count > 0) {
count--;
}
myTextField.text = count;
};
Key.addListener(keyListener);

```

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 15, 2008, 12:10am UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296/5 "2008-05-15T00:10:36Z")

</div>

To display it in a dynamic textfield, change

```auto
trace(count);

```

to

```auto
myTextField.text = count;

```

---

<div class="post-metadata">

**Author:** ![thegrrraue](https://avatars.discourse-cdn.com/v4/letter/t/b4bc9f/32.png) [@thegrrraue](https://forum.kirupa.com/u/thegrrraue)\
**Post date:** [May 15, 2008, 7:39am UTC](https://forum.kirupa.com/t/looped-action-on-button-counting-up-or-down/260296/6 "2008-05-15T07:39:55Z")

</div>

Dude, seriously thank you so much. This not the first time you’ve saved me! Works like a charm!
