簡體   English   中英

使對象數組成為Java

[英]Making an array of objects Java

我正在創建一個平台游戲,我有一個名為Platform的類,該類將x位置和y位置,寬度,高度和顏色放置在該平台內。

platform = new Platform(100, 250, 100, 10, Color.BLUE);

像這樣。

我可以這樣稱呼變量:

g.fillRect(platform.x, platform.y, platform.width, platform.height);

當我想獲得平台的顏色時,請執行以下操作:

g.setColor(platform.color);

我也有一個工作原理非常相似的播放器。

我有一個碰撞方法:

int LXOff = (platform.x - player.width); // boundaries
int LYOff = (platform.y - player.height); // boundaries
int RXOff = (platform.x + platform.width); // boundaries
int RYOff = (platform.y + platform.height); // boundaries

int LXOff2 = (platform2.x - player.width);
int LYOff2 = (platform2.y - player.height);
int RXOff2 = (platform2.x + platform2.width);
int RYOff2 = (platform2.y + platform2.height);
if(x <= 5 || y <= 29) // numbers are specific to the perfect border of the screen
{
    return true;
}
if(x >= 495 - player.width || y >= 495 - player.height) // numbers are specific to the perfect border of the screen
{
    return true;
}
if(x >= LXOff && y >= LYOff && x <= RXOff && y <= RYOff)
{
    return true;
}
if(x >= LXOff2 && y >= LYOff2 && x <= RXOff2 && y <= RYOff2)
{
    return true;
} 
return false;

我的問題是我需要創建一組額外的變量,這些變量代表第二個平台使用的邊界。 我想知道,如果將來要使用大量平台,該如何使用數組或類似Platforms的東西簡化它,然后可以使用一些循環在數組中繪制所有平台。

我相信您將使用的平台將創建一個新的Platform對象。 創建一個映射,以創建平台名稱作為鍵,平台對象作為值的圖。 必要時通過平台名稱檢索Platform對象

Platform android = new Platform(....);
Platform windows = new Platform(....);
Map<String, Platform> map = new HashMap<String, Platform>();
map.put("android", android);
map.put("windows", windows);

//for retrieval
map.get("android");

如果邊界是可變的,我建議為每個平台創建一個具有通用邏輯的抽象類和一個平台特定的類。

Platform[] platforms = new Platform[N]

其中N是數組的大小。

如果在創建數組時不沒有數組大小,則最好使用

List<Platform>  platforms = new ArrayList<Platform>() 

如果我理解您的問題,您可以繪制多個平台,但是您還想繪制一些邊界? OO方式將是為兩者定義一個通用接口。

一種選擇是使用getX(), getWidth(), getColor()等來定義一個接口,也許是RectangleLike ……然后循環遍歷RectangleLike的一個大數組(注意:最好使用List)並調用適當的碼。

另外,IMO更好地使用一種方法drawYourself(Graphics g)定義一個接口,可能是CanDrawIntoGraphics 循環調用drawYourself().的列表drawYourself().

我得到了這個解決方案,您必須發送一個平台的數組列表

List <Platform> platformList =new ArrayList<Platform> ();
platformList.add(platform1);
platformList.add(platform2);

發送這個platformList到你那里的方法

foreach(Platform platform:platformList){
    int LXOff = (platform.x - player.width);
    int LYOff = (platform.y - player.height);
    int RXOff = (platform.x + platform.width);
    int RYOff = (platform.y + platform.height);

    if(x >= LXOff && y >= LYOff && x <= RXOff && y <= RYOff)
    {
        return true;
    } 
}

正如Java文檔指出的那樣

集合 (有時稱為容器)只是將多個元素組合為一個單元的對象。 集合用於存儲,檢索,操作和傳達聚合數據。 通常,它們代表形成自然組的數據項,例如撲克手(紙牌集合),郵件文件夾(字母集合)或電話目錄(名稱到電話號碼的映射)。

因此,為了維護平台數量以備將來使用,您可以制作一個Platform而不是Array的集合,因為Collection具有動態擴展和收縮的能力。

來到編碼側,要么使用“ 地圖”,要么使用“平台列表 ”,如果要刪除重復項,請使用“ 設置集”界面

假設您有2個平台,則可以使用此平台

List<Platform> platformList=Arrays.asList(platform1,platform2);

要遍歷任何collection的實現,請使用Iterator接口

對於地圖,您可以查看@ dc.sashwat發布的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM