Hi all,

I am learning Cocos 2D and Python and have a simple question.

I'd like to output the content of a variable on the screen, so that I can 
see how it changes during runtime.

I have an actor class like this:

class Actor(cocos.cocosnode.CocosNode):
    def __init__(self, x, y):
        super(Actor, self).__init__()
        w, h = director.get_window_size()
        self.position = (x, y)
        self.slow_radius = 200
        self.velocity = eu.Vector2(0, 0)
        self.speed = 10
        self.max_force = 5
        self.max_velocity = 200
        self.target = None
        self.seek = True
        self.distance = 10
        self.distance_text = self._create_text(w-60, h-40)
        self.add(ps.Sun())
        self.schedule(self.update)

    def update(self, dt):
        if self.target is None:
            return
        self.distance = self.target - eu.Vector2(self.x, self.y)
        ramp = 1.0
        ramp = min(abs(self.distance) / self.slow_radius, 1.0)
        steering = self.distance * self.speed * ramp - self.velocity
        steering = truncate(steering, self.max_force)
        self.velocity = truncate(self.velocity + steering,
                                self.max_velocity)
        direction = 1 if self.seek else -1
        self.position += self.velocity * dt * direction
        self.distance_text.element.text = 'Distance: %s' % self.distance

    def _create_text(self, x, y):
        text = cocos.text.Label(font_size=10, font_name='Verdana',
                                anchor_x='right', anchor_y='center',
                                color=(255, 255, 255, 255))
        text.position = (x, y)
        self.add(text)
        return text

and I'd like to output the the content of self.distance for example.

I wrote another class called HUD:

class HUD(cocos.layer.Layer):
    def __init__(self, distance):
        super(HUD, self).__init__()
        w, h = director.get_window_size()
        self.distance = distance
        self.distance_text = self._create_text(w-60, h-40)

        self.distance_text.element.text = 'Distance: %s' % self.distance

    def _create_text(self, x, y):
        text = cocos.text.Label(font_size=10, font_name='Verdana',
                                anchor_x='right', anchor_y='center',
                                color=(255, 255, 255, 255))
        text.position = (x, y)
        self.add(text)
        return text

and a Main Layer that calls both classes:

class MainLayer(cocos.layer.Layer):
    is_event_handler = True

    def __init__(self):
        super(MainLayer, self).__init__()
        self.actor = Actor(320, 240)
        self.add(self.actor)
        self.hud = HUD(self.actor.distance)
        self.add(self.hud)

    def on_mouse_motion(self, x, y, dx, dy):
        self.actor.target = eu.Vector2(x, y)

    def on_mouse_press(self, x, y, buttons, mod):
        self.actor.seek = not self.actor.seek

The output on my screen stays at 10 and doesn't update to the values of the 
update function.

Any help on this would be appreciated.

Thanks,
Christoph

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/cocos-discuss/698bb178-95f6-4830-b4b0-4b4e8f30abc2n%40googlegroups.com.

Reply via email to