我写了一个类,它以简单的动画名称加载一系列动画,如下所示:
public Animation walking;
walking= new Animation(1/10f,
atlas.findRegion("1"),
atlas.findRegion("2"),
atlas.findRegion("3"),
atlas.findRegion("4"),
atlas.findRegion("5"),
atlas.findRegion("6"),
atlas.findRegion("7"),
atlas.findRegion("8"),
atlas.findRegion("9"),
atlas.findRegion("10"));
walking.setPlayMode(Animation.LOOP);
其余的依此类推。 (所有这些都被写入类的构造函数中,以便在它加载时加载..
然后在游戏类中,我尝试使用 AnimatedSprite() 访问这些动画。这是一个库 dermetfan 写的,它采用动画而不是纹理,并且表现得像 Sprite 。用于将动画传递给 Sprite 的代码如下所示:
animatedSprite = new AnimatedSprite(Animations.walking);
eclipse 认为这是正确的,一切都很好,直到运行时尝试使用动画创建 animatedSprite 时出错。 现在我已经在一个类中使用了这个确切的设置,当动画是包含使用它的 animatedSprite 的类的一部分时,它似乎工作正常。但是,按照我所描述的设置,将动画传递给 AnimatedSprite 类时会出现一些问题。
是的,我正在创建一个 animations=new Animations();在将它传递给 AnimatedSprite 之前,因为 eclipse 将 Animations.walking 视为有效动画。有什么想法吗?
最佳答案
如果您遇到空指针异常,这意味着您在调用它之前没有实例化 walking
。您应该创建一个名为 initAnims()
的方法,该方法位于您的 Animations
类中
新建Animations.class
public Animation walking;
public void initAnims() {
walking= new Animation(1/10f,
atlas.findRegion("1"),
atlas.findRegion("2"),
atlas.findRegion("3"),
atlas.findRegion("4"),
atlas.findRegion("5"),
atlas.findRegion("6"),
atlas.findRegion("7"),
atlas.findRegion("8"),
atlas.findRegion("9"),
atlas.findRegion("10"));
walking.setPlayMode(Animation.LOOP);
//Init other anims here
}
在您的main(String[] args)
方法中,您可以调用initAnims()
,然后开始游戏本身。您也可以在您的屏幕类中(如果有的话)在任何 show()
方法中调用 initAnims()
方法。
关于java - LibGDX 访问另一个类中的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21072691/