2013-10-23 87 views
1

我一直工作在一个小游戏在Slick2D 这里的JavaDoc:http://slick.ninjacave.com/javadoc/overview-summary.htmlSlick2D ClassCastException异常

的代码块这是给我的问题:

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(); 
    } 

这是错误:

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

什么让我失望是.. shape.transform()返回一个抽象的Shape类,它是为了被铸造成一个特定的形状。我在一个不同的类中使用Polygon做了同样的事情,并且它工作正常。

如果任何人有这样的经验,它的大加赞赏,谷歌并没有帮助我太多

编辑* 哦,对不起,我忘了,包括this.shape的创建:

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

回答

1

this.shape.transform()方法返回多边形造成的问题,但是您正在转换Ellipse。

EllipsePolygonShape延伸。所以申报Shape shape而不是Ellipse shape

现在您可以直接指定而不需要投射。

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

如果需要,那么你可以输入它。

+0

那么,这比预期容易,谢谢 – EyeOfTheHawks

+0

@EyeOfTheHawks高兴地听到它。不用谢。 – Prabhakaran