A
AlexL1980
Neues Mitglied
- 0
Hallo!
Ich versuche mich gerade an einem Spiel und bin am verzweifeln:
Ich habe eine Klasse spiel erstellt, welche alle Einstellungen des Spiels enthält.
Ich versuche mich gerade an einem Spiel und bin am verzweifeln:
Ich habe eine Klasse spiel erstellt, welche alle Einstellungen des Spiels enthält.
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] spiel [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]...[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]gesundheit[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]int geld;[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] lebenminus() {[/SIZE][/LEFT]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0][COLOR=#000000]ge[/COLOR]sundheit[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]--;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]gesundheit[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]==0) {[/SIZE][/LEFT]
[LEFT][SIZE=2]Intent intent = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent(spiel.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], Verloren.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]); [/SIZE]
[SIZE=2]startActivity(intent);[/SIZE][/LEFT]
[LEFT][SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[LEFT][SIZE=2][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] spiel() {[/SIZE][/SIZE][/LEFT]
[SIZE=2][LEFT][SIZE=2]geld=300;[/SIZE][/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]gesundheit[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=1;[/SIZE]
}
}[/LEFT]
[/SIZE]
Um darauf zugreifen zu können habe ich in der Activity wo man auf einen Button "Neues Spiel" klickt folgendes gemacht´:
Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Start [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] spiel [/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]s[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2];[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[LEFT][SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]start[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
[SIZE=2]Button spielstarten1 = (Button) findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]spielstarten[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]spielstarten1.setOnClickListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] OnClickListener() { [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onClick(View v) { [/SIZE]
[I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]s[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]=[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] spiel();[/SIZE]
[SIZE=2]Intent intent = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent(Start.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], Hauptscreen.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]); [/SIZE][/LEFT]
[SIZE=2][/SIZE]
[/LEFT]
[SIZE=2]startActivity(intent);[/SIZE]
[LEFT]
[LEFT][SIZE=2]finish();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]});[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/LEFT]
Somit kann ich von jeder Activity aus über Start.s.gesundheit den Gesundheitswert abfragen.
Mein Problem ist nun: Wenn der Spieler stirbt (Gesundheit=0) soll ein Verloren-Bildschirm angezeigt werden. Ich nutze hierzu die Funktion lebenminus der Spiel-Klasse. Das Programm soll einen Gesundheitspunkt abziehen und wenn Gesundheit=0=tot die Verloren-Activity anzeigen. Dies funktioniert nicht und ich komme nicht dahinter warum das so ist.
Sobald das Intent erstellt wird stürzt das Spiel ab.
Gruß,
Alex
Zuletzt bearbeitet: