This site requires JavaScript, please enable it in your browser!
Greenfoot back
SushiLlama
SushiLlama wrote ...

2019/9/30

Replace Greenfoot.delay() with a clock/timer to prevent freezes

1
2
SushiLlama SushiLlama

2019/10/3

#
I think i got it right... But please confirm that....
import greenfoot.*;  
import java.util.*;
public class BombNew extends Actor
{
    private int direction;
    private Player summoner;
    private Player opponent;
    private int timer;
    private double speed = 100;
    public BombNew(int dir, Player player) {
        direction = dir;
        summoner = player;
    } 
    public void act() {
        //Movement
        speed *= 0.98;
        setRotation(direction);
        move((int)speed);
        Wall wall = (Wall) getOneIntersectingObject(Wall.class);
        if(wall != null) {
            //switch direction 180 degrees
            direction = (direction+180)%360;
            speed *= 0.75;
            move((int)speed);
        }
        setRotation(0);
        //Opponent Intersection
        java.util.List<Player> pSeen = getIntersectingObjects(Player.class);
        for (Object obj : getObjectsInRange(60, Player.class)) if (obj != summoner) opponent = (Player)obj;
        if (opponent != null) activateBomb(); 
        //Exploding
        activateBomb();
    }
    public void activateBomb() {
        Star star = new Star();
        getWorld().addObject(star, this.getX(), this.getY());
        //subtract one heart
        Greenfoot.playSound("LoseLife.mp3");
        HealthGUI opponentHealthGUI = opponent.getHealthGUI();
        opponentHealthGUI.setLife(opponentHealthGUI.getLife()-1);
        //respawn the opponent
        getWorld().removeObject(opponent);
        getWorld().addObject(opponent, Greenfoot.getRandomNumber(900+1),Greenfoot.getRandomNumber(600+1));        
        /**
        *   TODO: get some delay in to show the exploding bomb (bomb+star) before removing both
        */
        Greenfoot.playSound("BombExplode.wav");
        getWorld().removeObject(this);
        summoner.setIsBombThrown(false);
    }
}
But now i get a NullPointerException like before the adjustment in the line:
HealthGUI opponentHealthGUI = opponent.getHealthGUI();
in activateBomb()... Here is my HealthGUI code if helpful:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 
public class HealthGUI extends Actor
{
    private int life = 3;
    GreenfootImage playerTwoThreeHeart = new GreenfootImage("PlayerTwoThreeHeart.png");
    GreenfootImage playerTwoTwoHeart = new GreenfootImage("PlayerTwoTwoHeart.png");
    GreenfootImage playerTwoOneHeart = new GreenfootImage("PlayerTwoOneHeart.png");
    //TODO: Still misses playerOne images - implement them in the future!
    public int getLife() {
        return this.life;
    }
    public void setLife(int life) {
        this.life = life;
    }
    public void act(){
        switch(life ) { 
            case 3:
                setImage(playerTwoThreeHeart);
                break;
            case 2:
                setImage(playerTwoTwoHeart);
                break;
            case 1:
                setImage(playerTwoOneHeart);
                break;
            default:
                GameWorld guiWorld = (GameWorld) getWorld();
                guiWorld.removeAll();
                Greenfoot.playSound("Lose.wav");
                Greenfoot.stop();  
        }
    }
}
danpost danpost

2019/10/3

#
In BombNew class, remove lines 31 and 32.
SushiLlama SushiLlama

2019/10/3

#
danpost wrote...
In BombNew class, remove lines 31 and 32.
Now the error is gone but the bomb somehow doesnt Explorer. Well it does explode... but only if a Player runs close to it. My bomb turned in a proximity mine. I think we left the case out when the bomb doesnt touch the enemy nor kills the enemy when blowing up which prevents it from blowing up somehow...
danpost danpost

2019/10/3

#
How long do you want it to go before exploding when never in range of opponent?
SushiLlama SushiLlama

2019/10/3

#
So youre looking for a Time x the bomb should move, looking if it hits the opponent, and otherwise just detonate Well my world is 900x600 so i would suggest something like 100 Pixels movement in total... Since i didnt use a Timer before i would suggest making the time of movement before exploding adjustable by variables. The Problem now is that it waits for a player to come in the radius but it should just detonate without killing anybody. If you could show me how to fix that i could just Experiment with values for the movement duration.
SushiLlama SushiLlama

2019/10/3

#
How long do you want it to go before exploding when never in range of opponent? I would say like close to one second. At my (pretty high) gamespeed this would be roughly 1000 ticks i guess.
danpost danpost

2019/10/3

#
In BombNew class, change line 30 to:
if (opponent != null || speed < 0.00000002) activateBomb();
Adjust as needed.
SushiLlama SushiLlama

2019/10/3

#
Wow thank you! And there wont be any delay? The opponent and I can move even while we are dropping bombs? Thats awesome! Ill try to improve my game further. For now i got no more questions. Ill come back if that changes. But for now i really want to thank you for your kind help! I really apreciate it! <3
You need to login to post a reply.
1
2