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

2015/2/17

how to use a worlds started() and stopped()

fejfo fejfo

2015/2/17

#
I work on a project and if the execution started I want it to load a save en when it stops I want it to save. the saving and loading works good enuf but my world doesn't execute stated or stopped
1
2
3
4
5
6
7
public void started() {
        System.out.println("started");
 }
     
    public void stopped() {
        System.out.println("stoped");
}
this code is in my world annywanne know how to use this ?
danpost danpost

2015/2/17

#
These methods, 'started' and 'stopped', execute when the project is started or stopped while the active world is an instance of the class the methods are located in. That means (1) the project is stopped or paused and either (a) the 'Run' button is clicked; (b) a 'Greenfoot.start()' command is executed; or (c) 'Ctrl-R' is pressed on the keyboard; while the active world is an instance of the class the 'started' method is in; or (2) the project is running and either (a) the 'Pause' button is clicked, (b) a 'Greenfoot.stop()' command is executed; or (c) 'Ctrl-Shift-R' is pressed on the keyboard; while the active world is an instance of the class the 'stopped' method is in. Normally, you would not call these methods within your code; but, they are executed automatically upon those circumstances. This does not mean you cannot execute these methods by calling them in your code. Doing so, however, will not cause the project to start or stop.
fejfo fejfo

2015/2/17

#
my methods suddenly started wording when I put somme code into the act() method of some actors . and Do you know what the greenfoot webside does whit java.io beacuase thats what I use to save and load the game
danpost danpost

2015/2/17

#
You will be able to load the game on the site, but forget about saving it there due to security reasons. The only way to save info on the site is through the limited amount of UserInfo storage available.
fejfo fejfo

2015/2/18

#
can I use object serialisation to save to the greenfoot site ?
fejfo fejfo

2015/2/18

#
this is the code (the methods) I currently use to save
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.io.*;
import java.util.*;
import greenfoot.*;
 
public class IoHelp implements Serializable
{
    String main,save,Class,object,image,objectX,objectY,objectRotation;
 
    public IoHelp() {
        makeSaveFolders();
    }  
 
    public void makeSaveFolders() {
        main = "data/";
        save = main + "save/";
        Class = main + "class/";
        object = main + "object/";
        image = main + "image/";
        objectX = object + "x/";
        objectY = object + "y/";
        objectRotation = object + "rotation/";
        makeFolder(main);
        makeFolder(save);
        makeFolder(Class);
        makeFolder(object);
        makeFolder(image);
        makeFolder(objectX);
        makeFolder(objectY);
        makeFolder(objectRotation);
    }
 
    public void makeFolder(String folderName) {
        File folder = new File( folderName );           
        folder.mkdir();
        //folder.close();
    }
 
    public void saveFile( ArrayList<String> lines, String fileName )
    {
        try
        {
            File file = new File( fileName );
            //file.mkdir();
            file.createNewFile();
 
            PrintWriter out = new PrintWriter(file);
            for(String text: lines) {
                out.println(text);
            }
            out.close();
            //System.out.println("Done writing to " + fileName); //For testing
        }
        catch( IOException e )
        {
            System.out.println("IoHelp: saveFile Error: " + e);
            e.printStackTrace( );
            //out.close();
        }
    }
 
    public ArrayList<String> loadFile(String fileName) {       
        ArrayList<String> list = new ArrayList<String>();
        try {
            File file = new File( fileName );
            Scanner in = new Scanner(file);
            try {
                while(true) {
                    list.add(in.nextLine());
                }               
            } catch(NoSuchElementException e) {
                in.close();
            }
        } catch(IOException ex) {
            System.out.println("IoHelp: loadFile ERROR: " + ex + "making file");
            try {
                File file = new File(fileName);
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("IoHelp: saveFile Error: " + e);
                e.printStackTrace( );
            }
        }
 
        return list;
    }
 
    //Greenfoot only
    public void saveActor(java.util.ArrayList<greenfoot.Actor> objects,String fileName) {
        //saving object
        try {           
            FileOutputStream file = new FileOutputStream(object+fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(Actor o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            System.out.println("IoHelp: saveActor ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
        //saving lockation
        //make arrays with all the lockations and rotation
        ArrayList<String> x = new ArrayList();
        ArrayList<String> y = new ArrayList();
        ArrayList<String> ro = new ArrayList();
        for(Actor o: objects) {
            if(o.getWorld() != null) {
                x.add(Integer.toString(o.getX()));
                y.add(Integer.toString(o.getY()));
                ro.add(Integer.toString(o.getRotation()));
            } else {
                x.add("not in world");
                y.add("not in world");
                ro.add(Integer.toString(0));
            }
        
        //save lockations
        saveFile(x,objectX+fileName);
        saveFile(y,objectY+fileName);
        saveFile(ro,objectRotation+fileName);
    }
 
    public void saveWorld(ArrayList<World> objects,String fileName) {
        try {
            FileOutputStream file = new FileOutputStream(fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(World o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            System.out.println("IoHelp: saveWorld ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
    }
 
    public ArrayList<Actor> loadActor(String fileName,World caller) {
        ArrayList<Actor> list = new ArrayList<Actor>();
        try {
            FileInputStream file = new FileInputStream(object + fileName);
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((Actor) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        System.out.println("IoHelp: load Actor class ERROR: " + cnfex);
                    }
                }
            } catch(java.io.EOFException e) {
                //ignore this
            }    
            in.close();
        } catch(IOException ex) {
            if(ex.toString() == "java.io.EOFException") {
                //ingnore it
            }else {
                System.out.println("IoHelp: load Actor file ERROR: " + ex);
                try {
                    File file = new File(object + fileName);
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println("IoHelp: saveFile Error: " + e);
                    e.printStackTrace( );
                
            }
        }
        //got the actors adding them to the world if they were in the world
        ArrayList<String> textX = loadFile(objectX + fileName);
        ArrayList<String> textY = loadFile(objectY + fileName);
        ArrayList<String> textRo = loadFile(objectRotation + fileName);
        for(int i = 0; i < list.size(); i++) {
            Actor o = list.get(i);
            try {
                int x = Integer.parseInt(textX.get(i));
                int y = Integer.parseInt(textY.get(i));
                int ro = Integer.parseInt(textRo.get(i));
                caller.addObject(o,x,y);
                o.setRotation(ro);
            } catch(NumberFormatException NFEx) {
                System.out.println("object: " + " wasn't in world");
            }
        }
 
        return list;       
    }
 
    public ArrayList<World> loadWorld(String fileName) {
        ArrayList<World> list = new ArrayList<World>();
        try {
            FileInputStream file = new FileInputStream(fileName);
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((World) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        System.out.println("IoHelp: load World class ERROR: " + cnfex);
                    }
                }
            } catch(EOFException e) {
                //ignore this
            }
            in.close();
        } catch(IOException ex) {
            System.out.println("IoHelp: load World file ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //in.close();
        }
        return list;
    }
}
danpost danpost

2015/2/18

#
Again, you can not create or delete any files or folders, nor can you write to any files on the site due to security restraints.
You need to login to post a reply.