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

2013/6/10

Scenario freezes when I change levels?

1
2
Entity1037 Entity1037

2013/6/10

#
I implemented a cheat that changes the level to a blank room in my Megaman EX scenario, and it was working perfectly. But I changed the layout of the room a bit and now it freezes the game whenever I use the cheat. I've tried making the layout blank again along with a multitude of other things, but nothing seems to work. I sometimes get an out of memory error too, but I have no idea how I would fix that. Can anyone help me on this one?
Entity1037 Entity1037

2013/6/10

#
The scenario is here: http://www.greenfoot.org/scenarios/8355
Could you post some code? It seems that you may have accidentally called a new something infinite times, or you have a giant image/sound file, or something like that.
After looking at your game, it seems that you have some code (i'm guessing from the player) that says something like below:
if (Greenfoot.isKeyDown("t"))
    Greenfoot.setWorld(new TestWorld());
BAD IDEA! You should have an instance variable that is called whenever T is called. Like this:
//Instance Variables
private TestWorld() cheat = new TestWorld();

//Constructors/Methods/Other code

//Method with isKeyDown("t")
if (Greenfoot.isKeyDown("t"))
    Greenfoot.setWorld(cheat);
That way you don't run out of space making new instances of the TestWorld.
Entity1037 Entity1037

2013/6/11

#
What you posted doesn't work. Here's what I was trying before:
if (Greenfoot.isKeyDown("t")){
    getWorld().removeObject(this);
    Greenfoot.setWorld(new TestRoom());
}
Any other ideas?
What do you mean it didn't work? If you have an instance variable of another world, it should work... What do you add to your test world? A new Player? If so, you should make either a new Player object that can't teleport, and is only used in the TestRoom, or make player have the following line in the if statement:
if (Greenfoot.isKeyDown("t") && !(getWorld() instanceof TestWorld)
...
Hope that helps.
Entity1037 Entity1037

2013/6/11

#
That didn't work either...
After looking at your source code, I have found your source of error. Look at line 38 of your TestRoom class:
for (int i = 2; i < 8; ) // OH CRAP! You forgot to add i++ or some other factor that would increase i to end the for-loop! 
{
    ...
}
I'm pretty sure that's your problem. Infinite loops are nasty in Greenfoot if placed in *just* the wrong place.
I have just tested this on your scenario, and I can move around in that nice little box you made when I click "t". You should also fix the Player spawning, it's sorta glitchy for some reason.
Entity1037 Entity1037

2013/6/11

#
Thank you! I'm a little embarrassed that I didn't catch this simple error a little sooner, but at least everything is fixed! :)
Well, now you should know to always end a loop! An important part of programming is learning from your mistakes.
Entity1037 Entity1037

2013/6/11

#
Indeed it is. And btw, I HATE YOUR PROFILE PIC.
danpost danpost

2013/6/11

#
I am quite surprised that you are not experiencing much problem with the way your classes are arranged. When sub-classing a class, you are (should be) saying that anything created from the sub-class is generally an instance of the super-class. What I am saying is, if 'X' is a Megaman, and you sub-class it with 'shot1' or 'XExplosion', then instances of 'shot1' and instances of 'XExplosion' are also Megaman objects (having basically the same behaviours and characteristics), which, in your case, are not. So, you could have a class called 'Vehicle', sub-class it with 'SpaceCraft', 'AirCraft', 'LandCraft', and 'WaterCraft'. Then, 'AirCraft' can be sub-classed with 'Airplane', 'Helicoptor', 'Glider', etc. You should be getting the idea. A 'Glider' IS an 'AirCraft' and a 'Vehicle' and an 'Actor' and an 'Object' (all super-classes of it).
>:D
@danpost I agree. I decided not to say anything about it since that wasn't his current problem. But it's not really a smart idea to create subclasses just because two objects use each other. You make a subclass when you want it to do everything the superclass does, or edit one thing, or have it all and add more, but not because they will be used by it's superclass.
There are more replies on the next page.
1
2