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

2017/2/11

Greenfoot crashes when program is run with certain actors

M0N57R0517Y M0N57R0517Y

2017/2/11

#
Hey, I'm having some trouble with a program and I would appreciate some help. I will upload and link my code whenever I figure it out, but first I will just explain. I am making a simple adventure game, and I made one actor whose sole job is to showText() for the purpose of advancing story. Other actors are movers. However, whenever I run the program with this story actor, called "info", on the map, greenfoot crashes. This has only been happening for some of the time I have had this actor, not all of it, and all the code compiles properly. Has anyone seen this before, or have thoughts on how it might be fixed? Thanks!
danpost danpost

2017/2/11

#
At least start by showing what code you have for this actor that seems to be causing the problem.
M0N57R0517Y M0N57R0517Y

2017/2/12

#
http://www.greenfoot.org/scenarios/19018 Here is the code, sorry. I'm only just figuring this forum out.
danpost danpost

2017/2/12

#
M0N57R0517Y wrote...
http://www.greenfoot.org/scenarios/19018 Here is the code, sorry. I'm only just figuring this forum out.
In your Info class, you are programming the entire dialog in one act cycle. You must track which step in the dialog you are at and execute the appropriate code for that step in a specific act cycle. The steps for a single set of options might be: (step 1) show first option and start countdown timer; go to step 2 (step 2) decrement timer; if zero, show second option, re-start timer and go to step 3 (step 3) decrement timer; if zero, show third option, re-start timer and go to step 4 (step 4) decrement timer; if zero, prompt for response and go to step 5 (step 5) if response made, show result, re-start timer and go to step 6 (step 6) decrement timer; if zero -- then if extra result, show extra result, re-start timer and go to step 7, else go to step 8 (step 7) decrement timer; if zero, go to step 8 (step 8) start next sequence, if any The act method should only execute a single step per cycle (or frame) of the scenario. When I say 'go to #' in the sequence above, all you will do is set a new value to a field that tracks what step is currently being taken.
M0N57R0517Y M0N57R0517Y

2017/2/12

#
Here's the new code in the class info:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Info here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Info extends Actor
{
    /**
     * Act - do whatever the Info wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void Info(){         int Info = 0;         int timer = 0;}
    public void pause (){
        for (int i = 0; i <100; i++)
        {if(Greenfoot.isKeyDown("enter") == true){i = 1000;};};
    }
     
    public void act(){
    //setImage("Info");
    //declaring shit blank
    int Scene = 0;
    String BackgroundInfo = null;
    String Option1 = null;  String Result1 = null;
    String Option2 = null;  String Result2 = null;
    String Option3 = null;  String Result3 = null;
    String Decide = "1.     2.      3.      (enter)";
    
        // SCENE ZERO -- Code Monkey: Get Up, Get Coffee
         if (Scene == 0){
 
          
         //declaring shit correctly for each scene
         BackgroundInfo = "It's 6 O'Clock!";
         Option1 = "1. Get up, get coffee"; Result1 = "Good idea! You'll be all ready for work!";
         Option2 = "2. Stay in bed"; Result2 = "Hm... You'll be late to work!";
         Option3 = "3. Go to job"; Result3 = "In your pajamas? That's not a good plan.";
          
        //running information with blanks filled
 
        int repeats = 0;
        if (timer == 0) {
         
        if (Info == 5) {getWorld().showText("To see options again, press enter", 200, 415);}
         
        else if (Info == 4) {getWorld().showText("Choose with keys!", 130, 415); Info++; timer = 40;}
         
        else if (Info == 3) {getWorld().showText(Option3, 100, 415); Info++; timer = 40;}
         
        else if (Info == 2) {getWorld().showText(Option2, 100, 415); Info++; timer = 40;}
         
        else if (Info == 1) {getWorld().showText(Option1, 100, 415); Info++; timer = 40;}
         
        else if (Info == 0) {getWorld().showText(BackgroundInfo, 100, 415); Info++; timer = 40;}
    }   else {timer --;};
        
          
         //results block
         /* getWorld().showText(Decide, 100, 415);
         if (Greenfoot.isKeyDown("1")){getWorld().showText(Result1, 100, 415); Info++;}
         else if (Greenfoot.isKeyDown("2")){getWorld().showText(Result2, 100, 415); Info++;}
         else if (Greenfoot.isKeyDown("3")){getWorld().showText(Result3, 100, 415); Info++;}
         else if (Greenfoot.isKeyDown("enter") && repeats == 0){Info = 1; repeats++;}
         Scene++; */
        };
        };
 
    }
It should work, but I wanted to make sure that Info and Timer didn't get overwritten every run cycle, so I put them in the constructor. However, I am now getting errors saying int Info and Int Timer were never defined. How should I fix that?
Nosson1459 Nosson1459

2017/2/12

#
You have to do 'int info' and 'int timer' as line 15 and 16 (the variables should start with a lowercase letter). (I think, if it says "never defined".)
danpost danpost

2017/2/12

#
Any variables that need to be maintained through multiple act cycles must be declared outside of any methods. That means lines 24 through 29 and what you have inside the 'Info' method (line 15). You can remove the 'pause' method as the timer will take care of that part. Instead of 'Info' as the name of the field that determines what needs done, I would use something more meaningful -- like 'step'. You can declare constants (outside of any method) to help in what step is what. For example:
1
2
3
4
5
6
7
8
9
10
11
private static final int
    INIT_SCENE = 0,
    BACKGROUND = 1;
    OPTION1 = 2,
    OPTION2 = 3,
    OPTION3 = 4,
    REVIEW_PROMPT = 5,
    CHOOSE_PROMPT = 6,
    VALID_KEY_PROMPT = 7,
    RESULT1 = 8,
    RESULT2 = 9;
With this, you can set the steps easily. For example, if showing the second option:
1
2
3
4
5
6
7
8
if (step == OPTION2)
{
    if (--timer > 0) return;
    getWorld().showText(Option3, 100, 415);
    timer = 120;
    step = OPTION3;
    return;
}
Using 'switch' cases would make things a bit easier too:
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
switch (step)
{
    case INIT_SCENE:
    switch (Scene)
    {
        case 0:
        BackgrounInfo = "It's 6 O'Clock!";
        Option1 = "1. Get up, get coffee";
        Result1 = "Good idea! You'll be all ready for work!";
        Option2 = "2. Stay in bed";
        Result2 = "Hm... You'll be late to work!";
        Option3 = "3. Go to job";
        Result3 = "In your pajamas? That's not a good plan.";
        Result4 = null;
        break;
         
        case 1:
        // similar code
    }
    getWorld().showText(BackgroundInfo, 100, 415);
    timer = 120;
    step = BACKGROUND;
}
return;
 
case BACKGROUND:
if (--timer > 0) return;
getWorld().showText(Option1, 100, 415);
timer = 120;
step = OPTION1;
return;
 
case OPTION1:
// etc.
Also, with the timer, you do not need to set such a slow scenario speed (put it back up near the middle -- you can adjust the initial timer value for each step as needed).
You need to login to post a reply.