简体   繁体   English

Slick2D ClassCastException

[英]Slick2D ClassCastException

I've been working on a little game in Slick2D Here is the JavaDoc: http://slick.ninjacave.com/javadoc/overview-summary.html 我一直在使用Slick2D开发一个小游戏。JavaDoc如下: http ://slick.ninjacave.com/javadoc/overview-summary.html

This chunk of code is giving me issues: 这段代码给了我一些问题:

 public void move(){

        this.shape = (Ellipse)this.shape.transform(Transform.createTranslateTransform((float)(speed*Math.sin(angle)),(float)(speed*Math.cos(angle)*-1)));
        this.posx = this.posx+(float)(speed*Math.sin(angle));
        this.posy = this.posy+(float)(speed*Math.cos(angle)*-1);

         updateShape();
    }

This is the error: 这是错误:

java.lang.ClassCastException: org.newdawn.slick.geom.Polygon cannot be cast to org.newdawn.slick.geom.Ellipse

What's throwing me off is.. shape.transform() returns an abstract Shape class, which is meant to be casted to a specific shape. 使我失望的是.. shape.transform()返回一个抽象的Shape类,该类打算转换为特定的形状。 I did the same thing with a Polygon in a different class, and it works fine. 我在不同的类中对Polygon做过同样的事情,并且效果很好。

If anyone has experience with this, it's much appreciated, google wasn't helping me much 如果有人对此有经验,那就太感谢了,谷歌对我没有多大帮助

edit* Oh, I'm sorry, I forgot to include how this.shape was created: 编辑*哦,很抱歉,我忘了包含this.shape的创建方式:

Ellipse shape;
...
shape = new Ellipse(diameter/2,diameter/2,posx,posy);

This issues caused by this.shape.transform( ) method returning Polygon but you are converting Ellipse. 此问题是由this.shape.transform( )方法返回Polygon引起的,但是您正在转换Ellipse。

Ellipse and Polygon are extended from the Shape . EllipsePolygonShape扩展。 So declare like Shape shape instead of Ellipse shape . 因此,声明为Shape shape而不是Ellipse shape

Now you can assign directly without casting. 现在,您可以直接分配而无需强制转换。

this.shape =  this.shape.transform(Transform.createTranslateTransform((float)(speed*Math.sin(angle)),(float)(speed*Math.cos(angle)*-1)));

if needed then you can type cast it. 如果需要,可以键入强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM