Hello,
Thanks for all the comments. As Paul suggested, here is the part of my code
where I was trying to make some sequence of actions.
def attack_ship(self, attacker, defender):
"Attacker attacks the defender"
destroyed = attacker.attack(defender) #This returns True if the
defender is destroyed by the attack
ox, oy = self.battle_grid.from_pixel_to_grid(attacker.position) #
Origin in grid coord
m, n = self.battle_grid.from_pixel_to_grid(defender.position) #
Defender position in grid coord
def _remove_ship(ship):
explosion = cocos.sprite.Sprite(self.battle_grid.explosion_anim,
position=ship.position) # This is an
animation of an explosion
explosion.push_handlers(self) # So we know when the animation
is finished
self.battle_grid.add(explosion, name="explosion")
ship.player.destroy_ship(ship) # Removes the ship from that
player's fleet
self.battle_grid.remove(ship) # Removes the ship sprite from
the battle grid
def _show_laser(destroyed, defender):
laser = self.battle_grid.get("laser") #It's a derived class
from draw.Canvas
direction = cocos.euclid.Vector2(x=m-ox, y=n-oy).normalize() #
Vector from attacker to defender
laser.pos_from = attacker.position + direction *
grid.CELL_WIDTH/2. # Origin of the laser line
laser.pos_to = defender.position # Destination of the laser line
laser.free() # To refresh the vertex_list
action = Show() + Delay(0.1) + Hide() # Show the laser beam for
0.1 sec
if destroyed:
action = action + CallFunc(_remove_ship, defender)
else:
action = action + CallFunc(self.on_command_finished)
laser.do(action)
# self.battle_grid.rotate_to_bearing will return a RotateTo action
towards the defender ship.
action = self.battle_grid.rotate_to_bearing(m, n, ox, oy) +
CallFunc(_show_laser, destroyed, defender)
attacker.do(action)
def on_animation_end(self):
explosion = self.battle_grid.get("explosion")
explosion.pop_handlers()
self.battle_grid.remove("explosion")
self.on_command_finished() # This tells us that the attack command
animation is done, and the IA or the player can send a new command.
This is my first implementation, very ugly, but made with the sole purpose
to have something working. And now I wanted to simplify this ugly
construction. :)
By the way, bear with me, I'm only a hobbyist programmer. I don't program
for a living.
I appreciate a lot the long answer from Claudio. It's very helpful and I
still need some time to decide which approach would work best for me.
Best regards,
Daniel.
On Saturday, November 2, 2013 8:44:53 AM UTC+1, Daniel Gillet wrote:
>
> Hello,
>
> I would like to chain some actions for multiple targets. Say I want to do
> a little animation with two sprites:
> 1. Sprite1 moves next to Sprite2 in 5 sec.
> 2. Sprite1 says: "Where should I go?" for 3 sec.
> 3. Sprite2 says: "Go to the bottom right corner." for 3 sec.
> 4. Sprite 1 moves to the bottom right corner in 5 sec.
>
> And let's just assume there is an action to say something (not relevant
> for what I'm trying to figure out).
>
> How would be the best way to put that in place? I can sequence the first
> two actions. But how to know when Sprite2 should start saying something? To
> me this feels like actions should emit an event "action_stopped". Is this
> the right way to go?
>
> Another idea is to have a parent action which is aware of the sequence of
> actions and the sequence of targets involved. It would check in each update
> if the current action if finished, and if so, pass to the next action
> applying the correct target. But then again not too sure how to go down
> this route.
>
> I tried sequencing with CallFunc passing a function for the next action (
> Sprite2.do(Say("blahblah", 3)) ), but obviously when Sprite2 starts the
> action #3, Sprite1 starts immediately action #4.
>
> Any suggestion to a good way to implement this would be highly appreciated.
>
> Best regards,
> Daniel.
>
--
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 http://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/groups/opt_out.