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

2022/6/15

Scenario won't work on website but does in the app

aleifericsson aleifericsson

2022/6/15

#
My scenario here: https://www.greenfoot.org/scenarios/29765 does not work on the website but functions perfectly fine in the app (it should work when you download it, at least it did for me). I suspect it is something to do with images not loading in properly or maybe to do with the image stretching methods on the spring, but I'm not really sure.
danpost danpost

2022/6/15

#
aleifericsson wrote...
My scenario here: ... does not work on the website but functions perfectly fine in the app
Your lines 28 and 29 in the MyWorld class declare static object fields. Rather than assigning their objects on those lines, try assigning them from the constructor of the class.
aleifericsson aleifericsson

2022/6/16

#
I've tried this and it still doesn't work. My computer science teacher thinks that it might be becuase I'm using external libraries but removing those also does not solve the issue.
danpost danpost

2022/6/16

#
aleifericsson wrote...
I've tried this and it still doesn't work. My computer science teacher thinks that it might be becuase I'm using external libraries but removing those also does not solve the issue.
Sorry. I got confused with a different issue. Your scenario compiles and initializes on the site; so, it wouldn't be the static field assignments. It must be in the running codes somewhere; but, who knows where?!
Spock47 Spock47

2022/6/16

#
Static variables in general shouldn't store a state. E.g., it makes problems when classes get loaded at a different time than the program's main execution (which is probably what causes the problems when executing Greenfoot on the webpage). So, try the following: 1. Change all your static variables in MyWorld class to not-static (lines 17 to 29), e.g.
   public double extension = 0;
2. The compiler will show you all positions where you use these variables. In all these places, replace "MyWorld." by "((MyWorld)getWorld())." 3. In class Spring, remove the third line from the constructor (line 24) and add the following method to the class:
    @Override
    public void addedToWorld(final World world) {
        target_y = (((MyWorld)getWorld()).extension+2)/4;
    }
danpost danpost

2022/6/16

#
Spock47 wrote...
Static variables in general shouldn't store a state. E.g., it makes problems when classes get loaded prior to the program's main execution (which is probably what causes the problems when executing Greenfoot on the webpage).
Like I stated above, the program compiles and initializes. So, there is no problems with the static fields.
Spock47 Spock47

2022/6/16

#
danpost wrote...
Like I stated above, the program compiles and initializes. So, there is no problems with the static fields.
Our posts were written in parallel. However, the worst case for the described attempt would be that the coding quality of the project is improved - which is a good thing especially for a project that already is of good quality. Ok, the web console says that the following error happens:
java.util.UnknownFormatConversionException: Unknown format conversion: f
I see that you use String.format in the project, e.g.
String.format("%.2f",gravity)
However, I have no knowledge whether this can be a problem. @danpost: Do you know about problems with String.format or anything that can throw a UnknownFormatConversionException when executing here on the webpage?
danpost danpost

2022/6/16

#
Spock47 wrote...
@danpost: Do you know about problems with String.format or anything that can throw a UnknownFormatConversionException when executing here on the webpage?
I am not aware of anything. However, I am not up to speed on javascript. Obviously there is a problem either in the conversion itself (from Java to JavaScript) or in the supporting of this conversion (or something relating to it). Best is probably to convert everything long-hand, rather than using String conversions/formatting.
RcCookie RcCookie

2022/6/17

#
I wouldn’t be surprised if some part of String.format doesn’t work online. After all, other core methods are missing, like String.repeat and PrintWriter.println(boolean). Or, it may work differently online than offline. Generally, don’t trust the JavaScript translation.
aleifericsson aleifericsson

2022/6/17

#
I have now changed every String.format() into String.valueOf() and now it works! Thank you so much for the help, guys!
You need to login to post a reply.