Reputation: 1
So I am a new programmer and I have a project due, so I was working on developing a basic game, the problem is once the player dies its supposed to open up a new activity for the ending screen so that i can take them back to the menu whilst also recording their records, however once the player dies no matter what I do it just crashes the App.
This is the code(its messy ik but i've been trying to solve this for a while so i got desperate):
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// Draw Tilemap
tilemap.draw(canvas, gameDisplay);
// Draw game objects
player.draw(canvas, gameDisplay);
for (Enemy enemy : enemyList) {
enemy.draw(canvas, gameDisplay);
}
for (Hex hex : hexList) {
hex.draw(canvas, gameDisplay);
}
// Draw game panels
joystick.draw(canvas);
performance.draw(canvas);
// Draw Game over if the player is dead
//this is where i need help
if (player.getHealthPoints() <= 0) {
Intent intent = new Intent(this.getContext(), EndScreen.class);
intent.putExtra("score", gameLoop.getCurrentKillCount()); // add any necessary data
//startActivity(intent);
playerDead = true;
playerHasFallen();
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//gameOver.draw(canvas);
//mainActivity.gameOver();
//endScreen.gameIsOver();
}
}
public void update() {
// Stop updating the game if the player is dead
if (player.getHealthPoints() <= 0) {
playerDead = true;
//pause();
return;
}
// Update game state
joystick.update();
player.update();
// Spawn enemy
if(Enemy.readyToSpawn()) {
enemyList.add(new Enemy(getContext(), player));
}
// Update states of all enemies
for (Enemy enemy : enemyList) {
enemy.update();
}
// Update states of all spells
while (numberOfSpellsToCast > 0) {
hexList.add(new Hex(getContext(), player));
numberOfSpellsToCast --;
}
for (Hex hex : hexList) {
hex.update();
}
// Iterate through enemyList and check if any Enemies have collided with the player
Iterator<Enemy> iteratorEnemy = enemyList.iterator();
while (iteratorEnemy.hasNext()) {
Circle enemy = iteratorEnemy.next();
if (Circle.isColliding(enemy, player)) {
// Remove enemy if it collides with the player
iteratorEnemy.remove();
player.setHealthPoints(player.getHealthPoints() - monsterTypeADmg);
//if (playerHasFallen() == true) {break;}
continue;
}
Iterator<Hex> iteratorHex = hexList.iterator();
while (iteratorHex.hasNext()) {
Circle hex = iteratorHex.next();
// remove hexes if it collides with something
if (Circle.isColliding(hex, enemy)) {
iteratorHex.remove();
iteratorEnemy.remove();
gameLoop.increaseKillCount();
increaseMultiplier();
break;
}
}
}
gameDisplay.update();
}
//public void playerDead() {
// endScreen.gameIsOver();
//}
public boolean playerHasFallen(){
if (playerDead == true){ return true;}
return false;
}
public boolean isPlayerAlive(){
if (playerDead == false) {return true;}
return false;
}
public int increaseMultiplier(){
if(gameLoop.killCount >= difficulty+10) {
difficulty++;
}
//if (gameLoop.increaseKillCount() )
//monsterTypeADmg++;
return difficulty;
}
public int getMultiplier(){
return difficulty;
}
@Override
public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
super.setOnCreateContextMenuListener(l);
}
public void pause() {
gameLoop.stopLoop();
gameWindow.setEndScreen();
}
}
I've tried various different methods to activate a different activity I've tried,
if (playerHealth <= 0) {
Intent intent = new Intent(this, EndingActivity.class);
intent.putExtra("score", playerScore); // add any necessary data
startActivity(intent);
}
Intent intent = new Intent(this, EndScreenActivity.class);
startActivity(intent);
public void openEndScreen(View view) {
Intent endScreen = new Intent(context,EndScreen.class);
context.startActivity(endScreen);
}
I've tried calling it from other classes i've tried using different code but no matter what i did the most i got was a black screen (after the canvas turned transparent)
(my bad here is the logcat; FATAL EXCEPTION: Thread-2 Process: com.example.lostc, PID: 3626 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135) at android.content.ComponentName.(ComponentName.java:130) at android.content.Intent.(Intent.java:5688) at com.example.lostc.GameOver.returnToMenu(GameOver.java:24) at com.example.lostc.Game.draw(Game.java:225) at com.example.lostc.GameLoop.run(GameLoop.java:75))
Upvotes: 0
Views: 40
Reputation: 926
it is hard to give a concrete answer without seeing a stack trace, but have you made sure to add EndScreen
Activity to the AndroidManifest.xml
as an <activity>
tag?
Upvotes: 0