Hello,

Interesting example! :) You should schedule your loop_hook with the schedule 
<http://python.cocos2d.org/doc/api/cocos.cocosnode.html#cocos.cocosnode.CocosNode.schedule>
 method. 
As it's schedule, you should not loop with a while in it, but just look in 
a non-blocking way if there is a message. If not, just exit and let cocos 
run until your function is scheduled again.

To change a label text, you need to access the the text attribute in 
label.element.text. The cocos label is just a wrapper around the pyglet 
label which is found under the element attribute. 

Here is a working example where I use the Queue to wait until cocos is 
ready to run before starting the label text changes.
For Windows users, you should really use the if __name__ == '__main__': guard, 
otherwise the code won't run.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from multiprocessing import Queue, Process
from queue import Empty

import time


def start_cocos(input_queue):
    import cocos

    class HelloWorld(cocos.layer.Layer):
        def __init__(self):
            super().__init__()

            self.label = cocos.text.Label(
                'Hello, world',
                font_name='Times New Roman',
                font_size=32,
                anchor_x='center', anchor_y='center'
            )
            self.label.position = 320, 240
            self.add(self.label)
            self.schedule(self.check_queue)

        def check_queue(self, dt):
            try:
                new_label_text = input_queue.get(block=False)
                self.label.element.text = new_label_text
            except Empty:
                pass

    cocos.director.director.init()
    hello_layer = HelloWorld()
    main_scene = cocos.scene.Scene(hello_layer)
    input_queue.put("Ready!")
    cocos.director.director.run(main_scene)


if __name__ == '__main__':
    output_queue = Queue()
    process = Process(target=start_cocos, kwargs=dict(
        input_queue=output_queue,
    ))
    process.start()
    print("Process starts")
    ready = output_queue.get(block=True)
    print("Cocos is running")
    time.sleep(2)
    print("Change label name")
    output_queue.put('New text for hello world')
    time.sleep(2)
    print("Change label name")
    output_queue.put('New text again for hello world')
    time.sleep(10)
    print("End")

Regards,
Dan


Le dimanche 20 novembre 2016 17:54:26 UTC+1, Bastien Sevajol a écrit :
>
> Hello,
> I'm discovering cocos2d for python, my project have to create a cocos2d 
> application inside a process and communicate with it with a Queue. I search 
> a way to read this Queue in event loop.
> Some code to explain:
>
> from multiprocessing import Queue
> from multiprocessing import Process
> from queue import Empty
>
> import time
>
>
> def start_cocos(input_queue: Queue):
>     import cocos
>
>     class HelloWorld(cocos.layer.Layer):
>         def __init__(self):
>             super().__init__()
>
>             label = cocos.text.Label(
>                 'Hello, world',
>                 font_name='Times New Roman',
>                 font_size=32,
>                 anchor_x='center', anchor_y='center'
>             )
>             label.position = 320, 240
>             self.add(label)
>
>     cocos.director.director.init()
>     hello_layer = HelloWorld()
>     main_scene = cocos.scene.Scene(hello_layer)
>
>     def loop_hook(director_or_scene_or_whatever):
>         while True:
>             try:
>                 new_label_text = input_queue.get(block=False, timeout=None
> )
>                 # ? Change label text ?
>             except Empty:
>                 return  # Finished to read Queue
>
>     loop_hook(None)
>
>     # ? Give to director loop_hook ?
>     cocos.director.director.run(main_scene)
>
>
> output_queue = Queue()
> process = Process(target=start_cocos, kwargs=dict(
>     input_queue=output_queue,
> ))
> process.start()
>
> time.sleep(2)
> output_queue.put('New text for hello world')
> time.sleep(2)
> output_queue.put('New text again for hello world')
> time.sleep(10)
>
> In this example code, i search to give to director or something else the 
> loop_hook function. This loop_hook function should be executed a each loop 
> iteration. And inside it's loop_hook, i should be able to modify label text 
> value.
> Is it possible ?
>
> Regards,
> Bastien.
>

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to