Hi Claudio,
thanks for the advice.
The actors label is actually visible, but as you said is off screen. My
problem with that is that the label moves with the actors layer when it's
position gets updated and I'd like to have the output static on the corner
of the window.
I'll try to publish an event as you suggested.
Below is the entire code of the script. If you let the sun exit the frame
to the lower left, you can see the label of the actor class coming into
frame.
import cocos
import cocos.euclid as eu
import cocos.particle_systems as ps
from cocos.director import director
def truncate(vector, m):
magnitude = abs(vector)
if magnitude > m:
vector *= m / magnitude
return vector
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
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
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
if __name__ == '__main__':
cocos.director.director.init(caption='Steering Behaviors')
scene = cocos.scene.Scene(MainLayer())
cocos.director.director.run(scene)
On Monday, September 28, 2020 at 3:14:59 AM UTC+1 Claudio Canepa wrote:
> You have two different labels
> - one in the HUD, which has a text set at the HUD init and nexer updated
> - one in the actor, updated which I suspect is not visible because out
> of screen
>
> If you want to update the HUD label, you can make the actor's update
> publish a 'distance_updated' event, the HUD listen to that event and
> updating the label's text. Read about user defined events in the pyglet doc
>
> If you want to make the actor's label visible, use a small x,y in
> _create_text(self, x, y) ; start with 0, 0 and do smal changes to find the
> desired offset.
>
>
> On Sun, Sep 27, 2020 at 9:42 PM Christoph Roth <[email protected]>
> wrote:
>
>> 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
>>
>> <https://groups.google.com/d/msgid/cocos-discuss/698bb178-95f6-4830-b4b0-4b4e8f30abc2n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
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/8a7071fa-61b1-4346-909b-0b69bb3a5a83n%40googlegroups.com.