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

Report as inappropriate.

kevv
kevv presents ...

2012/10/28

Lab 4 - Parallax scrolling

Use the arrow keys to move the spaceship, space button for a little boost.
All comments are welcome, especially suggestions on how to make the code look more correct.

3318 views / 10 in the last 7 days

3 votes | 0 in the last 7 days

Tags: with-source

open in greenfoot
Your browser does not support the canvas tag.
A new version of this scenario was uploaded on Sun Oct 28 21:29:35 UTC 2012 Arrow keys for movement, space for boost.
A new version of this scenario was uploaded on Sun Oct 28 21:29:53 UTC 2012 Arrow keys for movement, space for boost.
A new version of this scenario was uploaded on Sun Oct 28 21:38:29 UTC 2012 Arrow keys for movement, space for boost.
A new version of this scenario was uploaded on Sun Nov 04 12:40:41 UTC 2012 published source code
danpostdanpost

2012/11/4

In the Space class, I saw two things that could be done to make the code look better. (1) line 8, the int 's' is not used anywhere, so it can be removed (2) lines 7, 9 and 10: these are only used in the 'createStars' method and should be local to it. Move line 7 to the beginning of the 'createStars' method (you may have to assign zero to it) and remove 9 and 10 (there are already local variables for these, so those declared in lines 9 and 10 are not being used). In the Star class: the constructor uses 'if (size == 2)...if(size == 3)...if(size == 4)'. Better would be a 'switch' statement. Also 'size' has a range from one to five; you are not doing anything for when it is one or five. Actually those with sizes of one and five are invisible (black on a black background) since the default drawing color for them was never changed. In the Spaceship class: You are calling the 'hyper' method every act cycle (which is OK), but the method is updating the image each time it is called, whether the image needs changed or not. You can add a boolean field to the class, 'private boolean spaceDown;' and change the 'if's in the method to: if (!spaceDown && Greenfoot.isKeyDown("space")) and if (spaceDown && !Greenfoot.isKeyDown("space")) With that, the image will only be updated when there is a change in state of the "space" key.
danpostdanpost

2012/11/4

I forgot to mention, in the first 'if' block in the 'hyper' method, set 'spaceDown' to 'true'; and in the other one, set it back to 'false'.
kevvkevv

2012/11/5

Thank you very much danpost! I'll get to fixing it soon.

Want to leave a comment? You must first log in.

Who likes this?

SSS solokeh KevinMR97