[英]Slick2D ClassCastException
我一直在使用Slick2D開發一個小游戲。JavaDoc如下: http ://slick.ninjacave.com/javadoc/overview-summary.html
這段代碼給了我一些問題:
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);
此問題是由this.shape.transform( )
方法返回Polygon引起的,但是您正在轉換Ellipse。
Ellipse
和Polygon
從Shape
擴展。 因此,聲明為Shape shape
而不是Ellipse shape
。
現在,您可以直接分配而無需強制轉換。
this.shape = this.shape.transform(Transform.createTranslateTransform((float)(speed*Math.sin(angle)),(float)(speed*Math.cos(angle)*-1)));
如果需要,可以鍵入強制轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.