我试图从同一个班级创建多个动画兔子,但我似乎无法绕过它,也许有人可以提供帮助。目前我的场景中只有一个兔子(它跳到右边)
在这里使用此代码:
private void constructRootBunny(boolean preview)
{
GameObject bunnyRoot = new GameObject();
game.RemoveBunnyObject(bunnyRoot);
bunnyRoot.GetTransform().GetPos().Set(new Vector3f(506+bunny_X,500,506.5+bunny_Z));
bunnyRoot.GetTransform().SetScale(new Vector3f(0.1,0.1,0.1));
bunny_X += 0.005; // bunny 'hops' to the right
if(preview == true) {
String bunnyFrame = bunny_WalkCycle.get(currentFrame); //obj files
Mesh mesh = new Mesh(bunnyFrame );
Material material = new Material (new Texture("bunny_Furr1.png"),
new Texture("diffuse_NRM.jpg"),
new Texture("diffuse_DISP.jpg"));
bunnyRoot.AddComponent(new MeshRenderer(mesh, material));
}
game.AddBunnyObject(bunnyRoot);
currentFrame++;
if(currentFrame== 30) {
currentFrame= 0; //resets animation
}
}
观看视频:https://www.youtube.com/watch?v=yHvCHJp85Tc
我可以很容易地拥有多个兔子,但它们是静态的,我不能让它们动画(甚至不能同时)。我不知道如何将不同的行为模式应用到各自的兔子,这样他们就会最终独立行动(有不同的动画,走到不同的地方等等)..
我可以尝试的任何想法或建议吗?
答案 0 :(得分:1)
您已将动画硬编码到代码中。您需要动态分配动画行为。
您可以使用一组预定义的行为并使用Random Number Generator为兔子分配行为来执行此操作。
作为一个想法,考虑创建一个提供抽象方法的Behavior
类。然后,可以考虑制作一个JumpBehavior
类,其范围为Behavior
,并且要求您指定hopFrequency
,maximumHeight
和movementDirection
。然后,您可以使用不同的参数实例化其中几个JumpBehaviors
,以创建一系列独特的行为。
将这些行为分配给兔子后,您可以按帧更新兔子。
public abstract class Behavior
{
public abstract void initBehavior();
public abstract void updateBunny(GameObject bunny);
}
public class JumpBehavior extends Behavior
{
private long hopFrequency = 1000; //1 hop / 1000 milliseconds.
private double maximumHeight = 20, movementVelocity = 0;
public JumpBehavior(long hopFrequency, double maximumHeight, double movementVelocity)
{
this.hopFrequency = hopFrequency;
this.maximumHeight = maximumHeight;
this.movementVelocity = movementVelocity;
}
private long previousTimeMillis = 0;
private long elapsedTimeMillis = 0;
private long timeSinceHop = 0;
public void initBehavior()
{
//Start the clock.
previousTimeMillis = System.currentTimeMillis();
elapsedTimeMillis = System.currentTimeMillis();
}
public void updateBunny(GameObject bunny)
{
//Implement logic to control bunny here.
//Consider some randomization using a Random Number Generator.
//Keep track of time.
elapsedTimeMillis = System.currentTimeMillis() - previousTimeMills;
//If velocity is negative, then we move left.
bunny.X += movementVelocity * elapsedTimeMillis;
timeSinceHop += elapsedTimeMillis;
if (timeSinceHop >= hopFrequency)
{
//Reset counter, but keep the extra milliseconds passed.
timeSinceHop -= hopFrequency;
//Apply an acceleration to the bunny to start the hop.
//Maybe add logic to make sure it doesn't hop while in the air.
bunny.Velocity.Y += maximumHeight;
}
previousTimeMillis = elapsedTimeMillis;
}
}