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

2019/7/19

My images are larger than they are supposed to be. Also, using multidimensional arrays and for-loops to put actors into the world.

1
2
3
444Jam444 444Jam444

2019/7/19

#
Images I have some custom made images (made in photoshop) that are 25x25px each (except for some). I'm making a Bomberman game, where each cell is 25x25px (hence the small image sizes). The bomb (which I manually animated in photoshop as a GIF) used to be larger than 25x25px (because I hadn't decided on the cell size at the time), and so did the explosion (which is 2 separate graphics as subclasses to a explosion class, for hit-detection reasons). I have since resized both images to their correct sizes, but the problem is that, though the bomb is the correct size outside of greenfoot, when it is actually used inside greenfoot, it is too big. I tried resizing it using code in greenfoot and (although I don't remember exactly what happened) it ran into an error. I doubt it's because I only resized one image of the bomb in the GIF in photoshop because I resized the layers collectively, but it is a possibility. TL:DR: The bomb (which is a custom photoshop GIF) is 25x25px, but when in greenfoot, it becomes larger. Resizing using code caused an error. Arrays I don't have much experience with Java arrays. I have had experience with dictionaries/lists/arrays in other coding languages such as python, so I'm not entirely new to the concept. However, when I started this Bomberman project, it was recommended to me that I use an array to spawn the blocks and enemies etc. I think it would be easier to use a multidimensional array. But, my grid consists of 36x24 cells (each cell is 25x25px), which is a lot of dimensions in one (or more) array(s). I also am unsure how I would go about using the array to implement the actual actors into the world. With this, I actually haven't started coding this part yet, as I am unsure of how to do it... so... no source code yet unfortunately... but this is what I am planning: (in World) - initialise a multidimensional array of strings {{"player", "space", "", "", "", "", etc.}, {"space", "", "", etc.}, {"", "", "unbreakable block", etc.}, etc.} - each dimension of the array should be the y-coord storing a string. Each position equals an x-value eg. { {x1, x2, x3, x4, etc.}, {x1, etc.}, etc.} - the Unbreakable blocks, the player, and 2+ blank spots are in set positions. Everything else should be randomly generated in the array - anything in the array should then be inserted into the world at the corresponding x/y-coords, preferably using a for-loop, but act() works too. Before I decided to use a multidimensional array, I tried using a for-loop to add in unbreakable blocks at x/y-coords that were 3 cells apart. The for-loop ended up spawning the first block, before it gave up (multiple times): sigh... it went something like:]
int x = 2;
int y = 2;
for (i=0; i < 100; i++){
    if (x > 35){
        y = y + 3;
        x = 2; //I think I forgot this line in my old code
    }
    if (y > 24){
        i = 100;
    }
    addObject(sblock, (x*25+(25/2), (y*25+(25/2)); //I had already defined sblock as the unbreakable block actor
    x = x + 3;
}
I'm sorry this is so long but I just need suggestions these things, and I'd rather get suggestions all in one go. Kinda short on time is all. Any and all help is appreciated, thank you.
444Jam444 444Jam444

2019/7/19

#
Another problem: I am having difficulty increasing the explosion radius of the bombs. I need it so that when the player touches an ExplodeRadius powerup, the player class calls a method in the world that increases "explodeRadius" by 1. The "Explosion" class then (in its act method) loops through a bunch of if-statements until it finds the graphic that corresponds to explodeRadius, and sets itself to said graphic. My problem is that I don't know how to make an actor call methods from the world.
//player
ExplodeRadius explrad = (ExplodeRadius) getOneIntersectingObject(ExplodeRadius.class);
if (explrad != null){
            World myWorld = new MyWorld(); //I am aware this line is probably wrong. I was experimenting to find a solution
            myWorld.increaseExplodeRadius(); //Error here
            //getWorld().addObject(new ExplodeRadiusText, getX(), getY());
            removeTouching(ExplodeRadius.class);
        }

//world
public int explodeRadius = 1;

public int getExplodeRadius(){
        return explodeRadius;
    }
    static void increaseExplodeRadius(){
        explodeRadius++;
    }

//I have not coded the explosion class yet, but here is the code for it anyway
public class BombExplode extends Player
{
    //Setting a timer so the explosion disappears after 0.5 secs
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    
    
    public void act() 
    {
        //if exploderadius == num
        //set image to graphic (num)
        //etc
        //exploderadius = (World) getExplodeRadius etc
        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;

        if (durationSecs >= 0.000001){
            getWorld().removeObject(this);
        }
        
    }
}
444Jam444 444Jam444

2019/7/19

#
I am unlikely to reply for the next 36 hours or more. I have an event on. My apologies. Edit: I have also just noticed that parts of my original comment have completely disappeared... I don't know what happened. There is an entire sentence in square brackets that has just vanished. Excuse any grammatical errors, because certain words have also disappeared...
danpost danpost

2019/7/19

#
IMAGES You will need to show your Explosion class codes (best to show in entirety). Same with Bomb class. ARRAYS I am not totally sure an array is necessary. A (proper) for-loop can add the invincible blocks; the non-random stuff can be added directly and then the random stuff can be either looped or directly put in at random locations, depending on the count of each item. WORLD I would think your grid dimensions, when divided by your block size (25), should be an odd number. Along both the horizontal and the vertical, the ends should be similar (both walls or both paths). With both being an even number, there is no odd to pair with one of them. So, let us assume a grid of 35x23 which would be a world of 775x575. Then, depending on what is on the outer edge, you would have:
 // paths along edges
for (int y=0; y<35; y++)
{
    for (int x=0; x<23; x++)
    {
        if (x%2 == 1 && y%2 == 1)
        {
            addObject(new InvincibleBlock(), 12+25*x, 12+25*y);
        }
    }
}

// or walls along edges
for (int y=0; y<35; y++)
{
    for (int x=0; x<23; x++)
    {
        if (x == 0 || x == 22 || y == 0 || y == 34 || x%2 == 0 && y%2 == 0)
        {
            addObject(new InvincibleBlock(), 12+25*x, 12+25*y);
        }
    }
}
In your code:
444Jam444 wrote...
int x = 2;
int y = 2;
for (i=0; i < 100; i++){
    if (x > 35){
        y = y + 3;
        x = 2; //I think I forgot this line in my old code
    }
    if (y > 24){
        i = 100;
    }
    addObject(sblock, (x*25+(25/2), (y*25+(25/2)); //I had already defined sblock as the unbreakable block actor
    x = x + 3;
}
The variable, sblock, is never assigned another actor. So, you only ever add one object into the world. EXPLOSION RADIUS I would think this would be a player state, not a world state, which is then passed to any bomb placed by the player. Also, a single explosion should occur at each grid point within that radius. In other words, a bomb would then, when exploding, add explosions to its four surrounding cells, and, if radius is bigger, to the next set of cells more removed from where the bomb was placed. Hit detection should then not be a problem. SQUARE BRACKETS These are special characters used for tags (see list of some of the tags below the reply box) and care should be taken in using them in discussions.
444Jam444 444Jam444

2019/7/22

#
danpost wrote...
IMAGES You will need to show your Explosion class codes (best to show in entirety). Same with Bomb class.
Note: no changes have been made to the imported "GifImage" class.
//Bomb (image size is 25x25, but image appears larger in greenfoot) [GIF]
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bomb extends Player
{
    //Setting the image so the gif plays when the game is run
    GifImage myGif = new GifImage("bomb flash.gif");

    //Setting a timer so the explosion triggers after 5 secs (when the gif ends)
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {

        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;

        if (durationSecs >= 5){
            getWorld().addObject(new BombExplodeG1(), getX(), getY());
            getWorld().addObject(new BombExplodeG2(), getX(), getY());
            getWorld().removeObject(this);
        }
        else{
            playGif();
        }
    }
    public void playGif(){
        setImage(myGif.getCurrentImage());
    }
}

//Explosion (appropriately sized, no problems)
//The image is a 1x1px transparent image. 
//The actual image comes in 2 separate subclasses to stop incorrect image-based hit detection
//not a GIF
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class BombExplode extends Player
{
    //Setting a timer so the explosion disappears after 0.5 secs
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    
    
    public void act() 
    {

        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;

        if (durationSecs >= 0.000001){
            getWorld().removeObject(this);
        }
        
    }
}

//Explosion Graphic 1 ("Explosion G1")
//appropriately sized, no problems, not a GIF
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BombExplodeG1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BombExplodeG1 extends BombExplode
{
    //Setting a timer so the explosion triggers after 5 secs (when the gif ends)
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;
        
        if (durationSecs >= 0.05){
            
            getWorld().removeObject(this);
        }
        
    }
}

//Explosion graphic 2
// identical code to graphic 1
//not a GIF
//appropriately sized, no problems
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BombExplodeG2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BombExplodeG2 extends BombExplode
{
    //Setting a timer so the explosion triggers after 5 secs (when the gif ends)
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;
        
        if (durationSecs >= 0.000001){
            
            getWorld().removeObject(this);
        }
    }
}
Because the graphics for the explosion are subclasses of the explosion itself, anything that touches the graphics registers as touching the explosion (which is a good thing - allows for proper hit detection. Otherwise, the player would get hit for standing next to, but not touching, the explosion)
danpost wrote...
ARRAYS I am not totally sure an array is necessary. A (proper) for-loop can add the invincible blocks; the non-random stuff can be added directly and then the random stuff can be either looped or directly put in at random locations, depending on the count of each item.
This is actually a school project, and I have just found out that a 1-dimensional array is a requirement, unfortunately. How I'm supposed to fit a grid of such a large size into a single 1D array is beyond me.
danpost wrote...
WORLD I would think your grid dimensions, when divided by your block size (25), should be an odd number. Along both the horizontal and the vertical, the ends should be similar (both walls or both paths). With both being an even number, there is no odd to pair with one of them. So, let us assume a grid of 35x23 which would be a world of 775x575. Then, depending on what is on the outer edge, you would have:
 // paths along edges
for (int y=0; y<35; y++)
{
    for (int x=0; x<23; x++)
    {
        if (x%2 == 1 && y%2 == 1)
        {
            addObject(new InvincibleBlock(), 12+25*x, 12+25*y);
        }
    }
}

// or walls along edges
for (int y=0; y<35; y++)
{
    for (int x=0; x<23; x++)
    {
        if (x == 0 || x == 22 || y == 0 || y == 34 || x%2 == 0 && y%2 == 0)
        {
            addObject(new InvincibleBlock(), 12+25*x, 12+25*y);
        }
    }
}
The (probably minor) problem with changing my grid size is that I have already made the graphic for the world... I can make minor changes, but I'm not too fussed about where the unbreakable blocks are, just as long as they are in a grid and not causing problems (which they aren't, apart from not spawning them all). Also, a for-loop inside a for-loop always causes greenfoot to crash on me.
danpost wrote...
The variable, sblock, is never assigned another actor. So, you only ever add one object into the world.
So... do you mean "SolidBlock sblock = new SolidBlock();"? If so, then I should move that into the for loop? Or do I need to do something else? I'll have to test this while I wait for a response.
danpost wrote...
EXPLOSION RADIUS I would think this would be a player state, not a world state, which is then passed to any bomb placed by the player. Also, a single explosion should occur at each grid point within that radius. In other words, a bomb would then, when exploding, add explosions to its four surrounding cells, and, if radius is bigger, to the next set of cells more removed from where the bomb was placed. Hit detection should then not be a problem.
I'm not having a problem with hit detection. I probably should have explained it better, sorry. Also, I will be doing this with other classes, so this part will be modified and used elsewhere in my program as well. My problem is: - Powerups are temporary, so they cannot store and change values before the values are reset when the next powerup appears. Instead, I need another class that isn't temporary to store the values using their own variables or methods, such as variables or methods in the player or world class. - A potential fix for this is inheritance, but how does a subclass inherit a variable from a superclass? Example of what I mean by this problem: - When a breakable block is broken, it has a 2/25 chance of spawning a powerup. If it spawns a powerup, the chance resets to 2/25. If not, the chance becomes 2/24 (25 - 1, etc):
public int powerupChance = 25;
    private int randChance = Greenfoot.getRandomNumber(powerupChance);
    /**
     * Act - do whatever the BreakBlock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        BombExplode explosion = (BombExplode) getOneIntersectingObject(BombExplode.class);
        if (explosion != null){
            if (randChance >= (powerupChance-1)){
                getWorld().addObject(new Powerups(), getX(), getY());
                powerupChance = 25;
            }
            //else{
                //powerupChance--;
                //}
            getWorld().removeObject(this);
        }
    }   
Problem with this is (unless I'm wrong, because I haven't tested it) that powerupChance will always be 25 because each block has their own powerupChance (unless because its a public variable, this isn't a problem?) Thanks for helping.
444Jam444 444Jam444

2019/7/22

#
I just remembered that constructors are a thing. I can try messing around with constructors to fix my problem.
444Jam444 444Jam444

2019/7/22

#
444Jam444 wrote...
So... do you mean "SolidBlock sblock = new SolidBlock();"? If so, then I should move that into the for loop? Or do I need to do something else? I'll have to test this while I wait for a response.
This worked. Thanks!
danpost danpost

2019/7/22

#
A Bomb object is not a Player object; so, the Bomb class should not extend the Player class. Likewise with the BombExplode class. I have often used 1-D String arrays to build worlds with, assigning a specific character to each type actor. In fact, it more resembles the layout when you look at the array. That makes it easier to build. The powerupChance field is not the problem. It is the randChance field that is problematic. Its value never changes (is not random once set).
444Jam444 444Jam444

2019/7/23

#
danpost wrote...
I have often used 1-D String arrays to build worlds with, assigning a specific character to each type actor. In fact, it more resembles the layout when you look at the array. That makes it easier to build.
How would I go about doing this?
danpost wrote...
The powerupChance field is not the problem. It is the randChance field that is problematic. Its value never changes (is not random once set).
I've set each variable to static, will that help? I've added some more code along with that which might work, but some testing is needed.
444Jam444 444Jam444

2019/7/23

#
Update on the Bomb GIF: If the bomb is manually inserted into the world (before running), the image is fine. As soon as it runs, the GIF becomes enlarged. The GIF is fine: every image within the GIF is appropriately sized within photoshop, so there is something wrong with code/greenfoot/imported GIF class.
danpost danpost

2019/7/23

#
444Jam444 wrote...
<< Quote Omitted >> How would I go about doing this?
The Maze class of my Glowing Walls Maze Demo scenario exemplifies the idea.
I've set each variable to static, will that help?
No. If anything, it will make things worse.
danpost danpost

2019/7/23

#
If you want to know when the bomb gif has ended, then save the initial image and add a boolean to track its state of use. That way you can catch the moment when the gif cycle begins to repeat:
public class Bomb extends Actor
{
    GifImage myGif = new GifImage("bomb flash.gif");
    GreenfootImage initialImage;
    boolean firstImage = true;
    
    public Bomb()
    {
        initialImage = myGif.getImages().get(0;
    }
    
    public void act()
    {
        setImage(myGif.getCurrentImage());
        if (firstImage != (getImage() == initialImage)) // is there a change in use of first image
        {
            firstImage = !firstImage;
            if (firstImage) // is change to use the first image (again)
            {
                getWorld().addObject(new BombExplodeG1(), getX(), getY());
                getWorld().addObject(new BombExplodeG2(), getX(), getY());
                getWorld().removeObject(this);
            }
        }
    }
}
If the bomb adds explosion objects after its gif has completed, I do not see a need for timers in the explosion classes.
444Jam444 444Jam444

2019/7/24

#
danpost wrote...
If you want to know when the bomb gif has ended, then save the initial image and add a boolean to track its state of use. That way you can catch the moment when the gif cycle begins to repeat:
This is not a problem I am having. The GIF I have made plays once (both inside and outside of greenfoot), and lasts exactly 5 seconds, which is what I have set the timer to. Here is a demonstration of the variable-related problems I am having:
public class BreakBlock extends Block
{
    static int powerupChance = 25;
    
    //public BreakBlock(int pChance){
    //    powerupChance = pChance;
    //}
    
    static int randChance = Greenfoot.getRandomNumber(powerupChance);
    
    /**
     * Act - do whatever the BreakBlock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        BombExplode explosion = (BombExplode) getOneIntersectingObject(BombExplode.class);
        if (explosion != null){
            if (randChance >= (powerupChance-1)){
                Powerups pUp = new Powerups();
                pUp.spawnPowerup(getX(), getY());
                powerupChance = 25;
            }
            else{
                powerupChance--;
                System.out.println("BLOCK DESTROYED. VARIABLES:");
                System.out.println("randChance: "+randChance);
                System.out.println("pChance: "+powerupChance);
            }
            
            getWorld().removeObject(this);
        }
    }    
}
Outputs: BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 24 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 23 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 22 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 21 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 20 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 19 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 18 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 17 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 16 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 15 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 14 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 13 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 12 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 11 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 10 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 9 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 8 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 7 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 6 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 24 (powerupChance resets here, which means a powerup should have spawned... but no powerup spawned) BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 23 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 22 BLOCK DESTROYED. VARIABLES: randChance: 5 pChance: 21 NOTE: This is very weird... before, the "pChance" was not changing. I have not made any changes, other than this line:
System.out.println("BLOCK DESTROYED. VARIABLES:");
I have no idea why this has changed the output for my code... I am very confused... Previously outputted: randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 randChance: 23 pChance: 24 And also I see what you mean now by "randChance is only randomised once".
danpost wrote...
444Jam444 wrote...
I've set each variable to static, will that help?
No. If anything, it will make things worse.
Making the variables static has actually (mostly) fixed my problem, but now weird things are happening (mainly, how adding in a "println" fixed my variables not changing for some reason, as I showed above) Edit: I may as well show all relevant code, sorry for not showing earlier.
public class MyWorld extends World
{
    Player player = new Player();
    SolidBlock sblock = new SolidBlock();
    
    public int powerupChance = 25;
    
    static int explodeRadius = 1;
    
    //score = ???
    //public void updateScore(newScore){
    //score = newScore
    //update score on screen or something idk, I will look up how to do this
    //}
    
    public int x = 1;
    public int y = 1;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        Player player = new Player();
        SolidBlock sblock = new SolidBlock();

        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(player,(25/2),(25/2));

        for (int i = 0; i < 100; i++){
            if (x > 35){
                y = y + 3;
                x = 1;
            }
            if (y > 22){
                i = 100;                
            }
            else{
                SolidBlock sblock = new SolidBlock();
                addObject(sblock, (x*25+(25/2)), (y*25+(25/2)));
                x = x + 3;
            }
        } 

    }
    
    //variable1 = getnumberofobjects(breakblocks)
    //act{
    //variable2 = getnumberofobjects(breakblocks)
    //if variable2 < variable1
    //do randChance stuff (from breakblock class - just cut and paste)
    //addObject(new powerup)
    //variable1--
    //}
    /** ^^THIS WILL NOT WORK - MISSING COORDS^^ */
    
    
    
    public static void main(String[] args){
        String[] grid = {};
    }
    
    public int getPowerChance(){
        return powerupChance;
    }
    public void increasePowerChance(){
        powerupChance--;
    }
    public void resetPowerChance(){
        powerupChance = 25;
    }
    
    public int getExplodeRadius(){
        return explodeRadius;
    }
    static void increaseExplodeRadius(){
        explodeRadius++;
    }
}
public class Player extends Actor
{
    
    public Player(){
        
    }
    public int delayCount = 0;
    public int invincibility = 0;
    
    public int bombCount;
    
    //powerup counter
    public int lives = 3;
    public int maxBomb = 1;
    public int explodeRadius = 1;
    public int moveSpeed = 45;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (lives > 0){
            if (invincibility == 0){
                checkKeypress();
                if (delayCount > 0){
                    delayCount = moveDelay(delayCount);
                }
            }
            checkCollision();
        }
        else{
            getWorld().removeObject(this);
        }
    }    
    public void checkKeypress()
    {
        bombCount = (getWorld().getObjects(Bomb.class)).size();
        if (Greenfoot.isKeyDown("space") && bombCount < maxBomb){
            if (isTouching(Bomb.class) == false){
                getWorld().addObject(new Bomb(), getX(), getY());
            }
        }
        if (delayCount == 0){
            
            if (Greenfoot.isKeyDown("left") == false){
                if (Greenfoot.isKeyDown("right") == false){
                    if (Greenfoot.isKeyDown("up"))
                    {
                        Block block = (Block) getOneObjectAtOffset(0, -25, Block.class);
                        //setImage();
                        
                        if (block == null)
                        {
                            setLocation((getX()),(getY()-25));
                            delayCount = delayCount + moveSpeed;
                            
                        }
                        if (isAtEdge()){
                            setLocation((getX()),(getY()+(25/2)));
                        }
                    }
                    
                    if (Greenfoot.isKeyDown("down"))
                    {
                        Block block = (Block) getOneObjectAtOffset(0, 25, Block.class);
                        //setImage();
                        
                        if (block == null)
                        {
                            setLocation((getX()),(getY()+25));
                            delayCount = delayCount + moveSpeed;
                            
                        }
                        if (isAtEdge()){
                            setLocation((getX()),(getY()-(25/2)));
                        }
                    } 
                }
            }
                        
            if (Greenfoot.isKeyDown("up") == false){
                if (Greenfoot.isKeyDown("down") == false){
                    if (Greenfoot.isKeyDown("left"))
                    {
                        Block block = (Block) getOneObjectAtOffset(-25, 0, Block.class);
                        if (block == null)
                        {
                            setLocation((getX()-25),(getY()));
                            delayCount = delayCount + moveSpeed;
                            
                        }
                        if (isAtEdge()){
                            setLocation((getX()+(25/2)),(getY()));
                        }
                    }
                    if (Greenfoot.isKeyDown("right"))
                    {   
                        Block block = (Block) getOneObjectAtOffset(25, 0, Block.class);
                        if (block == null)
                        {
                            setLocation((getX()+25),(getY()));
                            delayCount = delayCount + moveSpeed;
                            
                        }
                        
                        if (getX() >= 900){
                            setLocation((getX()-25),(getY()));
                        }
                    }
                }
            }
            
        }
        else{
            delayCount = moveDelay(delayCount);
        }
    }
    public static int moveDelay(int delay)
    {
        delay--;
        return delay;
    }
    public static int invincibilityDelay(int invincibility)
    {
        invincibility--;
        return invincibility;
    }
    
    public void checkCollision(){
        BombExplode explosion = (BombExplode) getOneIntersectingObject(BombExplode.class);
        if (explosion != null){
            if (invincibility == 0){
                 
                lives = lives - 1;
                invincibility = 30;
                delayCount = 20;
                delayCount = moveDelay(delayCount);
            }
            else{
                invincibility = invincibilityDelay(invincibility);
                delayCount = moveDelay(delayCount);
            }
        }
        Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
        if (enemy != null){
            if (invincibility == 0){
                 
                lives = lives - 1;
                invincibility = 30;
                
            }
            else{
                invincibility = invincibilityDelay(invincibility);
            }
        }
        
        BombUp bombup = (BombUp) getOneIntersectingObject(BombUp.class);
        ExplodeRadius explrad = (ExplodeRadius) getOneIntersectingObject(ExplodeRadius.class);
        OneUp oneup = (OneUp) getOneIntersectingObject(OneUp.class);
        Skate skate = (Skate) getOneIntersectingObject(Skate.class);
        
        if (bombup != null){
            maxBomb++;
            
            removeTouching(BombUp.class);
        }
        if (explrad != null){
            MyWorld myWorld = new MyWorld();
            myWorld.increaseExplodeRadius(); //Error here
            
            removeTouching(BombUp.class);
        }
        if (oneup != null){
            lives++;
            
            removeTouching(OneUp.class);
        }
        if (skate != null){
            moveSpeed = moveSpeed - 5;
            
            removeTouching(Skate.class);
        }
        
        
    }
}
public class BreakBlock extends Block
{
    public int powerupChance;
    
    //public BreakBlock(int pChance){
    //    powerupChance = pChance;
    //}
    MyWorld myWorld = new MyWorld();
    private int randChance;
    
    /**
     * Act - do whatever the BreakBlock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        powerupChance = myWorld.getPowerChance();
        BombExplode explosion = (BombExplode) getOneIntersectingObject(BombExplode.class);
        if (explosion != null){
            randChance = Greenfoot.getRandomNumber(powerupChance);
            if (randChance >= (powerupChance-1)){
                Powerups pUp = new Powerups();
                pUp.spawnPowerup(getX(), getY());
                myWorld.resetPowerChance();
            }
            else{
                myWorld.increasePowerChance();
                System.out.println("BLOCK DESTROYED. VARIABLES:");
                System.out.println("randChance: "+randChance);
                System.out.println("pChance: "+powerupChance);
            }
            
            getWorld().removeObject(this);
        }
    }    
}
public class Bomb extends Player
{
    //Setting the image so the gif plays when the game is run
    GifImage myGif = new GifImage("bomb flash.gif");

    //Setting a timer so the explosion triggers after 5 secs (when the gif ends)
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {

        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;

        if (durationSecs >= 5){
            getWorld().addObject(new BombExplodeG1(), getX(), getY());
            getWorld().addObject(new BombExplodeG2(), getX(), getY());
            getWorld().removeObject(this);
        }
        else{
            playGif();
        }
    }
    public void playGif(){
        setImage(myGif.getCurrentImage());
        
    }
}
public class BombExplode extends Player
{
    //Setting a timer so the explosion disappears after 0.5 secs
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    
    
    public void act() 
    {

        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;

        if (durationSecs >= 0.000001){
            getWorld().removeObject(this);
        }
        
    }
}
public class BombExplode extends Player
{
    //Setting a timer so the explosion disappears after 0.5 secs
    long startTime = System.currentTimeMillis();

    private int durationMillis;
    private int durationSecs;
    
    
    public void act() 
    {

        //Timer
        long currentTime = System.currentTimeMillis();
        durationMillis = (int)(currentTime - startTime);
        durationSecs = durationMillis / 1000;

        if (durationSecs >= 0.000001){
            getWorld().removeObject(this);
        }
        
    }
}
public class Powerups extends Actor
{
    static int bombUpChance = 30;
    static int explodeRadiusChance = 55;
    static int oneUpChance = 65;
    static int skateChance = 100;
    static int randChance;
    /**
     * Act - do whatever the Powerups wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    static void spawnPowerup(int X, int Y){
        
        randChance = Greenfoot.getRandomNumber(100);
        if (randChance >= 0 && randChance < bombUpChance){
            BombUp bUp = new BombUp(X, Y);
            bombUpChance = bombUpChance + 6;
            explodeRadiusChance = explodeRadiusChance - 1;
            oneUpChance = oneUpChance - 2;
            skateChance = skateChance - 3;
        }
        else if (randChance >= bombUpChance && randChance < explodeRadiusChance){
            ExplodeRadius eR = new ExplodeRadius(X, Y);
            bombUpChance = bombUpChance - 1;
            explodeRadiusChance = explodeRadiusChance + 6;
            oneUpChance = oneUpChance - 2;
            skateChance = skateChance - 3;
        }
        else if (randChance >= explodeRadiusChance && randChance < oneUpChance){
            OneUp oUp = new OneUp(X, Y);
            bombUpChance = bombUpChance - 1;
            explodeRadiusChance = explodeRadiusChance - 2;
            oneUpChance = oneUpChance + 6;
            skateChance = skateChance - 3;
        }
        else if (randChance >= oneUpChance && randChance < skateChance){
            Skate sk8 = new Skate(X, Y);
            bombUpChance = bombUpChance - 1;
            explodeRadiusChance = explodeRadiusChance - 2;
            oneUpChance = oneUpChance - 3;
            skateChance = skateChance + 6;
        }
    }
}
Also, when the player gets hit by an explosion, they become unable to move or act for some reason.
444Jam444 444Jam444

2019/7/25

#
Update: + The player can now move after being hit. + The bomb is appropriately sized. > I've been told to use a 2D array. - Still having problems with powerupChance.
danpost danpost

2019/7/25

#
You create objects in each part of the if-else-else... in your spawnPowerup method of the Powerups class; but, nothing is done with them.
There are more replies on the next page.
1
2
3