# Generate new object (class) on button (key) hit

**URL:** <https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681>\
**Category:** Processing.py\
**Created:** [June 13, 2021, 6:20pm UTC](https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681 "2021-06-13T18:20:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nktlst](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nktlst](https://discourse.processing.org/u/nktlst)\
**Post date:** [June 13, 2021, 6:20pm UTC](https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681/1 "2021-06-13T18:20:59Z")

</div>

I created class with animated circle and then I want to create same objects every time I hit keyboard button… Problem that when I hit button here is always same object and animation disappear when button release…  
On buttons’s hit I need new objects, so if I hit button many times here is appear many animated objects and not disappear.

How I can achieve this?

My code example

```auto
class Circleclass:
    def __init__ (self,x,y,r,start=0):
        self.radius = r
        self.x = x
        self.y = y
        self.start = 0
    
    def __bool__ (self):
        return self.start == 1

    def ani(self):
        if (self. __bool__ ):
            op = 0
            while (op < 10 * frameCount/30):
                op = op + 1
            strokeWeight(op)
            stroke(0,op)
            circle(self.x,self.y,self.radius)
    
def setup():
    size(500,500)
    
def draw():
    background(255)

def keyPressed():
    if key == 'r' or key == 'R':
        i = Circleclass(200,100,100)
        bool(i)
        i.ani()

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 13, 2021, 8:36pm UTC](https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681/2 "2021-06-13T20:36:26Z")

</div>

You can store each circle you instantiate in a list. Then, you loop through that list to draw the circles each frame:

```python
...

circles = []

def draw():
    background(255)
    for c in circles:
        c.ani()

def keyPressed():
    global circles
    if key == 'r' or key == 'R':
        i = Circleclass(random(width), random(height), 100)
        circles.append(i)

```

Note that I randomized each new circle’s x-y coordinate (so they don’t all appear in the same place).

---

<div class="post-metadata">

**Author:** ![nktlst](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nktlst](https://discourse.processing.org/u/nktlst)\
**Post date:** [June 14, 2021, 10:31am UTC](https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681/3 "2021-06-14T10:31:34Z")

</div>

Thnx @tabreturn, its working! But, animation doesnt start from beginning, it continues… every new circle start from previous animation in class… how can I reset animation on every new object (and not affect animation from old ones)?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 14, 2021, 10:54am UTC](https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681/4 "2021-06-14T10:54:45Z")

</div>

You’re using the `frameCount` to control the width of every circle. Instead, use an instance-specific value, something like –

```python
        ...
        self.op = 0
    
    def ani(self):
        self.op += 1
        strokeWeight(self.op)
        stroke(self.op)
        circle(self.x, self.y, self.radius)

...

```

---

<div class="post-metadata">

**Author:** ![nktlst](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nktlst](https://discourse.processing.org/u/nktlst)\
**Post date:** [June 14, 2021, 11:03am UTC](https://discourse.processing.org/t/generate-new-object-class-on-button-key-hit/30681/5 "2021-06-14T11:03:07Z")

</div>

Beautiful! Thank you for clarifications, @tabreturn! 😀
