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

2015/2/15

Abstract Worlds

RealFighter64 RealFighter64

2015/2/15

#
I am trying to create an abstract world in Greenfoot. I have created this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import greenfoot.*;
import java.util.*;
 
public abstract class Scene extends World
{
    int[][] map;
    ArrayList<Actor> tileset;
     
     
    public Scene(int width, int height) {
        super(width, height, 64);
        map = new int[height][width];
        tileset = new ArrayList<Actor>();
        this.addTiles();
        this.createMap();
        this.prepare();
    }
     
    public void prepare() {
        for(int i = 0; i < tileset.size(); i++) {
            for(int y = 0; y < map.length; y++) {
                for(int x = 0; x < map[y].length; x++) {
                    if(map[y][x] == i) {
                        this.addObject(tileset.get(i), x, y);
                    }
                }
            }
        }
    }
     
    public void addTile(Actor tile) {
        tileset.add(tile);
    }
     
    public void setMapTile(int x, int y, int tile) {
        map[y][x] = tile;
    }
     
    public abstract void addTiles();
     
    public abstract void createMap();
}
But whenever I compile this code I get an infinite loop. I thought this was just because it was abstract, so I made a new class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;
 
/**
 * Write a description of class Level1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level1 extends Scene
{
    public Level1() {
        super(8, 8);
    }
     
    @Override
    public void addTiles() {
        this.addTile(new Block());
    }
     
    public void createMap() {
        this.setMapTile(0, 0, 0);
    }
}
However, I'm still getting the same problem. Does anyone know why I'm getting an infinite loop? Is there any way to not try and load in the Scene class graphically? Thanks in advance, RealFighter64 Edit: I forgot, I also made two classes for abstract tiling, in case it matters.: SpecialTile:
1
2
3
4
5
6
7
8
9
10
11
12
import greenfoot.*;
 
/**
 * Write a description of class SpecialTile here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public abstract class SpecialTile extends Actor
{
    public abstract void action(String action);
}
Block
1
2
3
4
5
6
import greenfoot.*;
 
public class Block extends Actor
{
     
}
The image is a custom made one.
davmac davmac

2015/2/15

#
But whenever I compile this code I get an infinite loop.
That's not really possible. Compilation itself won't cause an infinite loop. Greenfoot does try to instantiate a suitable world class after you compile it, but because the class is abstract (and because its constructor requires parameters), it's not suitable for automatic instantiation. I'm guessing that it's another world class in your project that is causing the problem.
RealFighter64 RealFighter64

2015/2/15

#
Thank you for the reply Davmac, Those two classes are the only world classes in the entire project. I'm not sure whether there is a problem with the loop in prepare(), or something else in Level1. However, the code still said that it made an infinite loop when Level1 didn't exist. If Scene couldn't be instantiated, then what was causing the infinite loop?
danpost danpost

2015/2/15

#
With all four classes given as above and only those four, the project compiles and runs normally without error (or infinite loop). Without the Level1 class, it should still compile fine; however, no initial world can be created because the class is abstract. Try closing out of greenfoot and re-opening it. If the issue persists, you may want to supply what version you are using and your operating system. I did get something strange after some fiddling. After testing with all four classes, I changed what Level1 extended to Actor and commented out the entire class code; then, tried to compile it (as an attempt to remove the Level1 class from the Scene class without destroying the class or the code within it). I got the following terminal error:
java.lang.ClassCastException: Level1 cannot be cast to greenfoot.World at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
This appears to be a greenfoot bug in that Level1 was not totally cleared as being a subclass of World (or of the abstract class Scene) when compilation was initiated (or when it found that the class was relocated to extend a different class). I am still using the Windows USB 2.3.0 version.
RealFighter64 RealFighter64

2015/2/15

#
Thank you Danpost, However my problem is still persisting even after a restart. I've also tried re-deleting Level1 and still no luck. Are you saying that the infinite loop is a problem with greenfoot and not my code? If so, then is there any way that I can 'fix' Greenfoot to make it work well again? For my version and OS, I'm using Greenfoot 2.4.1 and I'm on Windows 8.1 64x.
danpost danpost

2015/2/15

#
I am not saying, nor am I going to say, why an 'infinite loop' is being detected with your set-up. All I can say, as far as your issue, is what I stated in the first part of my last post. Please note the way I phrased the first sentence. Infinite loop detection was added to greenfoot recently and it is possible that some bugs exist in its coding. You may have to revert back to 2.4.0 (or to the version before infinite loop detection was added, 2.3.0).
RealFighter64 RealFighter64

2015/2/15

#
Thank you very much for your reply. I am going to try reverting back to 2.3.0 (if I can) and see how that turns out. Thanks! Edit: I've tried it, and although the infinite loop warning is gone (as expected) the screen now shows nothing. Anyway, I'm just going to work on something else for the time being. (Yes, I did re-insert the Level1 class)
danpost danpost

2015/2/15

#
Start a new scenario and create the four classes. Then, copy/paste the codes you supplied above, as is, into those classes replacing the code generated from creating the classes. You can double-click on the posted code above to select the inserted code and then proceed to copy it to your clipboard.
RealFighter64 RealFighter64

2015/2/15

#
I'm getting illegal character \160 from the posted code. Something in the formatting, I think...
danpost danpost

2015/2/15

#
I had copy/pasted directly from here to a new scenario's classes without changing anything with no problems.
RealFighter64 RealFighter64

2015/2/16

#
That's weird. I'm not sure why that's happening to me and not you.
RealFighter64 RealFighter64

2015/2/16

#
I just tried what you said, replacing any \160s from the code with spaces, and it worked perfectly! Thank you! I have no idea why it was happening in the first place!
danpost danpost

2015/2/16

#
RealFighter64 wrote...
I just tried what you said, replacing any \160s from the code with spaces, and it worked perfectly!
I was going to suggest doing that, but I was not sure if it would work (I had noticed that 160 is 2 to the 7th power, or 128, more than 32, where 32 is the character number for a space).
You need to login to post a reply.