简体   繁体   中英

Slick2D ClassCastException

I've been working on a little game in Slick2D Here is the 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. I did the same thing with a Polygon in a different class, and it works fine.

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:

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.

Ellipse and Polygon are extended from the Shape . So declare like Shape shape instead of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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