0
$\begingroup$

Okay. I've been working on this 2D Platform game in the Blender Game Engine lately. I've added some basic motion functions (in Python code), and now I'm getting to animation. I'm using the Blender playAnimation() function, and it works. But, I've found with logic bricks that the animation type that works best in my game is the flipper type. playAnimation() has no flipper mode.

Here's my script so far: (without playAnimation() function in it)

import bge
import math

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    keyboard = bge.logic.keyboard

    xyz = own.localOrientation.to_euler()
    xyz[2] = math.radians(180)

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events
       [bge.events.RIGHTARROWKEY]:
       own.applyMovement((0, 0.08, 0), True)
       own.worldOrientation = (0, 0, 0)

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events
       [bge.events.LEFTARROWKEY]:
       own.applyMovement((0, 0.08, 0), True)
       own.localOrientation = xyz.to_matrix()

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:
        own.applyMovement = ((0, 0, 0.08), True)

main()

So, how could I allow flipper animations in this script? Thanks!!

P.S. This code is for a public game project (or several). While I'll really appreciate any code or tips offered, I may or may not be able to give credit for it.

$\endgroup$

1 Answer 1

1
$\begingroup$

The flipper mode belongs to the action armature. This makes the actuator play the animation forward on activation and backward on deactivation.

The actuator uses the internal animation system to play the action in response the the current status (activated or deactivated) and the animation status (playing, reaching end).

The internal animation system can only act according to the animation status.

The simplest solution is to activate/deactivate the action actuator in flipper mode.

E.g.:

import bge
import math

controller = bge.logic.getCurrentController()
owner = controller.owner

keyboard = bge.logic.keyboard

xyz = owner.localOrientation.to_euler()
xyz[2] = math.radians(180)

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RIGHTARROWKEY]:
    owner.applyMovement((0, 0.08, 0), True)
    owner.worldOrientation = (0, 0, 0)
    controller.activate("turn right")
    controller.deactivate("turn left")
    controller.deactivate("jump")

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.LEFTARROWKEY]:
    owner.applyMovement((0, 0.08, 0), True)
    owner.localOrientation = xyz.to_matrix()
    controller.activate("turn left")
    controller.deactivate("turn right")
    controller.deactivate("jump")

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.SPACEKEY]:
    owner.applyMovement = ((0, 0, 0.08), True)
    controller.activate("jump")
    controller.deactivate("turn right")
    controller.deactivate("turn left")
$\endgroup$
14
  • $\begingroup$ Do I replace the words in quotations with the name of my animation? I have a single animation named "Run" How, would this look for such an animation? $\endgroup$ Oct 12, 2016 at 1:34
  • $\begingroup$ activate() and deactivate() expect a connected actuator or the name of a connected actuator. In the sample "jump", "turn left" and "turn right" are the name of actuators. $\endgroup$
    – Monster
    Oct 13, 2016 at 5:01
  • $\begingroup$ In general I would separate physical motion (moving, turning) from animation (walk cycle). While the first one alters the position/orientation within the scene the second one makes it look nice. Both operations should run synchronous to appear as a single task. $\endgroup$
    – Monster
    Oct 13, 2016 at 5:08
  • $\begingroup$ And what would be the logic brick setup for the "Run" actuator in my case? I tried connecting it to the python controller, and then leaving it all to itself, and neither worked. $\endgroup$ Oct 14, 2016 at 1:50
  • $\begingroup$ When should the "run" animation be played? [playing it is controller.activate("run") - assuming you have an actuator with that name] $\endgroup$
    – Monster
    Oct 14, 2016 at 6:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .