iT邦幫忙

0

Day3

昨天把成員變量都設計好之後,發現會有共通設計的變量名稱,像是x,y,width,height等,於是我們在這可以應用繼承的方法,將這些共同會用到的放在一個類裡面,這樣就可以將程式碼簡單一點。

1.設定一個類別:FlyingObject
protected int x;
protected int y;
protected int width;
protected int height;
成員變量設定好之後 要定義兩種FlyingObject

第一種專門給敵人的:airplane,bigairplane,bee 
因為他們出現的時候左右(x)不固定,由上往下下來(y)
public FlyingObject(int width,int height){
	this.width=width;
	this.height=height;
	Random rand =new Random();
	x =rand.nextInt(World.WIDTH-this.width);
	y=-this.height;
}
第二種給Sky,Bullet,Hero
不隨機產生位置,位置由自己設計位置
public FlyingObject(int width,int height,int x,int y){
	this.width=width;
	this.height=height;
	this.x=x;
	this.y=y;
}

2.每個角色都要移動,所以設計一個step()的方法在FlyingObject方法並以abstract表示,讓每個有繼承該類別的都必須要輸入內容。
以下是FlyingObject類的內容

https://ithelp.ithome.com.tw/upload/images/20200923/20125821VwGzmBJB5r.png
3.接著開始更新所有角色的類 每個都大同小異但需要去設計出他們的不同
Airplane:
最簡單的設計好圖片的width,heigth後,設定降下來速度的間隔即可
在step方法上,再將y去加上他設定的速度,圖片就會往下掉了
"在圖片上往下的圖片y會增加,相反Y越小圖片越上面"

https://ithelp.ithome.com.tw/upload/images/20200923/20125821fx3DWPziXB.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言