Weights라는 객체를 만드는 코드가 있습니다. 이제는 WeightSmall, WeightMedium 및 WeightLarge라는 하위가 있습니다. 각 하위에는 onScreen이라는 자체 정적 변수가 있습니다. 이 변수는 WeightSmall, WeightMedium 또는 WeightLarge 중 하나가 추가 될 때 증가해야하지만 배열에 추가되는 것이 아니라 create 메소드를 호출하면 반환됩니다. Weight 객체 배열이 있습니다. 부모 클래스의 배열에 요소가있는 하위 클래스 유형에 액세스 할 수있는 방법이 있습니까? 여기 해당 객체를 반환하기 전에 객체의 정적 변수를 증가 시키십시오.
만들고 무게에 대한 코드입니다 : 일어날 필요가 무엇public Weight createWeight() {
decider = Math.random() * 1;
// creates rocks randomly with the lowest chance for l, and the highest chance for m
if (decider <= 0.33) {
// small weight
return new WeightSmall(BitmapFactory.decodeResource(getResources(), R.drawable.weight_s), new Random().nextInt(screenWidth), -10);
} else if (decider <= 0.5 && decider > 0.33) {
// large weight
return new WeightLarge(BitmapFactory.decodeResource(getResources(), R.drawable.weight_l), new Random().nextInt(screenWidth), -10);
} else {
// medium weight
return new WeightMedium(BitmapFactory.decodeResource(getResources(), R.drawable.weight_m), new Random().nextInt(screenWidth), -10);
}
}
이 WeightSmall는 그것보다 작은 있는지 확인 WeightSmalls 화면 변수를 확인해야합니다, 말할 수를 위해, 그것은 경우의 3.을 가정 해 봅시다 무게를 반환하십시오. 그러나 그것은 WeightSmall의 onScreen 변수에 액세스하는 방법을 생각할 수 없습니다. 한 번 이상 만들어야하고 ArrayList에 구현하려고했지만 업데이트 메서드에서 복잡성이 발생합니다. 선명도
public void render(Canvas canvas) {
if (canvas != null) {
canvas.drawColor(Color.WHITE);
player.draw(canvas);
Weight[] weightArray = weights.toArray(new Weight[0]);
for (Weight weight : weightArray) {
weight.draw(canvas);
}
}
}
// updates the weight's position on the screen and checks collision with the player
public void update() {
Weight[] weightArray = weights.toArray(new Weight[0]);
for (Weight weight : weightArray) {
weight.update();
if (weight.getBounds().intersect(player.getBounds())) {
player.setTouched(false);
Intent gameOverIntent = new Intent(this.getContext(), GameOverActivity.class);
this.getContext().startActivity(gameOverIntent);
((Activity) getContext()).finish();
}
}
}
// count down timer spawning weights in every tick
public void timer() {
if (start == true) {
if (weightSpawnTimer != null) {
weightSpawnTimer.cancel();
weightSpawnTimer = null;
}
weightSpawnTimer = new CountDownTimer(30000, 800) {
public void onTick(long millisUntilFinished) {
weights.add(createWeight());
}
public void onFinish() {
weightSpawnTimer.start();
}
}.start();
}
}
편집 : 다음은 클래스에 대한 코드의 나머지 부분 (즉 문제)입니다 나는 체중의 화면 변수의 서브 클래스가 < = 3 인 경우 확인하십시오 onTick 방법에 일어날 필요가있는 무엇 , 만약 그렇다면 아무것도하지 않으면 새로운 가중치를 만듭니다. 일단 가중치가 오프 스크린되면,이 서브 클래스의 새로운 가중치가 생성 될 수 있도록이 변수를 감소시킵니다.
class WeightSmall {
public WeightSmall(...) {
// increment static
}
}
에 대한 것은 각 클래스 인스턴스가 생성되는 경우에 자신의 수를 증가 할 책임이하자 무엇
화면 오른쪽에있는 각 하위 클래스의 수를 계산하려고합니까? – Trenin
그렇다면 각 Weight * 하위 클래스에 정적 정수를 선언하면됩니다. Weight 슈퍼 클래스에서,'getNumberOnScreen' 메소드를 선언하십시오. 이것은 특정 서브 클래스에 대한 값을 리턴하기 위해 서브 클래스에 의해 오버 라이드됩니다. – Trenin
예. 맞습니다. 좋아요. 당신이 말한 것을 구현했지만, 여기에서이 메소드를 호출하여 각 유형의 가중치를 확인하려면 어떻게해야합니까?다음 작업을 수행 할 수 있습니다 같이 : 편집 공공 무효 onTick을 (긴 millisUntilFinished) { \t \t \t \t \t 무게 [] weightArray = weights.toArray (새 무게 [0]); (체중 무게 : weightArray) 용 \t \t \t \t \t \t { \t \t \t \t \t 경우 (weight.getOnScreen() <= 3) { \t \t \t \t \t \t \t weights.add (createWeight()); \t \t \t \t \t \t} \t \t \t \t \t} \t \t \t \t} 을하지만이 무게의 서브 클래스에 따라 올바른 화면에 변수 추가를 선택할 것인가? 또한 개체가 완료되면 어떻게이 변수를 감소시킬 수 있습니까? –