官术网_书友最值得收藏!

Making animations fit to intervals

Intervals are a very powerful feature of Panda3D, which you can see in the recipe Controlling actions using intervals. In this recipe you will go one step beyond and see how to efficiently control animations when used in conjunction with intervals.

Getting ready

If you haven't already, please read how to use intervals in Controlling actions using intervals and follow the steps of that recipe.

How to do it...

This recipe consists of the following tasks:

  1. Open Application.py.
  2. Delete the line self.panda.loop("walk").
  3. Find the following part of the code:
    self.pandaWalk = Sequence(self.walkIval1, self.turnIval1, self.colorIval, self.walkIval2, self.turnIval2,
    self.colorIval)
    self.pandaWalk.loop()
  4. Replace it with the following code:
    self.pandaAnim = ActorInterval(self.panda, "walk", loop = 1, duration = 5)
    self.pandaMove = Sequence(self.walkIval1, self.turnIval1, self.colorIval, self.walkIval2, self.turnIval2, self.colorIval)
    self.pandaWalk = Parallel(self.pandaAnim, self.pandaMove)
    self.pandaWalk.loop()

How it works...

The panda is moving just like before, but now an ActorInterval we create in the highlighted line of the code controls animation playback. We set the animation to loop and to play over the duration of five seconds. Finally, the animation-controlling ActorInterval and the Sequence of transformations are made part of a Parallel that plays the two intervals at the same time.

There's more...

The actor interval constructor can take a range of optional parameters besides loop and duration that were already presented in the sample code. Let's take a look at what else you can do with ActorInterval:

Instead of duration, you can use startTime and endTime to define more precisely when to start and stop playing the animation.

The playrate parameter lets you set the animation playback speed. Also note that if playrate is a negative value, the animation is played backwards.

You can specify a sub range of the animation to be played by setting startFrame and endFrame. Also, if you want to loop an animation range, set constrainedLoop to one instead of loop.

In the following line of code, all these options have been applied to our sample code to loop the first second of the walk animation at a very low rate:

self.pandaAnim = ActorInterval(self.panda, "walk", startTime = 0, endTime = 1, playrate = 0.1, constrainedLoop = 1, duration = 5)
主站蜘蛛池模板: 都江堰市| 五台县| 兴国县| 阳山县| 滁州市| 叶城县| 永和县| 达孜县| 襄城县| 红桥区| 理塘县| 东阳市| 大悟县| 股票| 大悟县| 德钦县| 正镶白旗| 大关县| 多伦县| 年辖:市辖区| 丰顺县| 马山县| 栾川县| 广安市| 改则县| 泸定县| 射洪县| 蒲城县| 柞水县| 平安县| 且末县| 英吉沙县| 武山县| 阳江市| 淮南市| 泰宁县| 南开区| 民乐县| 东源县| 鸡西市| 新绛县|