-1
我有這個類叫做「敵人」。它裏面有一個位圖和一些方法來動畫這個位圖的移動。如何用局部變量與隨機值相乘類?
所以我想要做的就是把這個類乘以X次。我用「for」循環和一個數組來做到這一點,但是我發現類中的變量最終都是相同的,儘管它們的值是隨機的(Math.random())。
如果我在類之外聲明變量,但這不是我想要的,因爲我希望類是獨立的。我想要同一班,乘以X次,獨立移動。
非常感謝。
public class Enemy {
float q,w,e,r = 0;
public void execute(){
draw();
move();
}
public void draw(){
Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.base);
if(q == 0 && w == 0 || q > e-5 && q < e+5 || w > e-5 && w < e+5){
e = 100 + (float) (Math.random()*200);
r = 100 + (float) (Math.random()*200);
}
canvas.drawBitmap(bitmap, q, w, null);
}
public void move() {
if(q<e && w<r) {
new Thread(); {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
EplusPlus();
}}}
if(q<e && w>r) {
new Thread(); {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
EplusMinus();
}}}
if(q>e && w<r) {
new Thread(); {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
EminusPlus();
}}}
if(q>e && w>r) {
new Thread(); {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
EminusMinus();
}}}
}
我調用類是這樣的:
public void run() {
while(isRunning) {
if(!ourHolder.getSurface().isValid())
continue;
canvas = ourHolder.lockCanvas();
canvas.drawRGB(30, 30, 200);
Enemy[] enemy = new Enemy[3];
for(int i = 0; i<enemy.length; i++){
enemy[i].execute();
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
向我們顯示您的代碼。 – Kayaman 2014-08-30 22:53:34
我不能確定沒有看到你的代碼,但它聽起來像你的類有靜態變量,當你需要的是非靜態變量。 – Eran 2014-08-30 22:54:22
隨機隨機性如何?你可以投入更多的數學,並使用時間函數,這使得每個實例都有所不同。 – RST 2014-08-30 22:56:44