Question

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);
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top