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

Comments for Scrolling Tutorial

Return to Scrolling Tutorial

A new version of this scenario was uploaded on 2016-12-23 05:13:06 UTC Fixed no-image limited scrolling code.
A new version of this scenario was uploaded on 2016-12-23 05:35:28 UTC
tkieseltkiesel

2017/2/17

In the scroll() method, the following two lines with loops may take a long time to execute if the 'camera' is scrolled very far from world center: while (imageX < 0) imageX += 100*wide; while (imageY < 0) imageY += 100*high; --- Consider updating this to if (imageX < 0) { imageX %= 100*wide; imageX += 100*wide; } if (imageY < 0) { imageY %= 100*high; imageY += 100*high; } -------------------- This replaces a looped add with a single mod and a single add.
tkieseltkiesel

2017/2/17

Or... given that it looks like you may have added the multiplication by 100 to reduce the number of loops.... if (imageX < 0) { imageX %= wide; imageX += wide; } if (imageY < 0) { imageY %= high; imageY += high; } Should get you there without the loop and without the x100 workaround to try and reduce looping.
danpostdanpost

2017/2/17

@tkiesel, the multiplication by 100 was to reduce the number of possible loops (probably to only one, anyway). The addition of this value is necessary before the mod when the imageX or imageY value is negative. Mod is not uniform going from a negative value to a positive one; so the value must be made positive before the mod.
tkieseltkiesel

2017/2/20

Yes indeed. The change I posted makes the value positive before the mod. They always land on the same value. So instead of: ------------------------------ // find a similar positive value for scroll positions while (imageX < 0) imageX += 100*wide; while (imageY < 0) imageY += 100*high; // get new starting positions for drawing 'scrollImage' imageX = imageX%wide; imageY = imageY%high; ------------------------------ You can have: ------------------------------ // find a similar positive value for scroll positions if (imageX < 0) { imageX %= wide; imageX += wide; } if (imageY < 0) { imageY %= high; imageY += high; } // get new starting positions for drawing 'scrollImage' imageX = imageX%wide; imageY = imageY%high; ------------------------------
tkieseltkiesel

2017/2/20

Also added if ( dsx == 0 && dsy == 0 ) return; to the beginning of scroll(int dsx, int dsy) so that no work is done if the requested scroll is zero. I love this code. Most compact way of implementing this that I've seen. Very well done! What license is this code under? I have some students interested in using it!
danpostdanpost

2017/2/20

@tkiesel, I think the calling statement should check that condition for scrolling. Calling scroll from the world constructor is useful in creating the initial background image sometimes and adding your line there would conflict with that.
danpostdanpost

2017/2/20

@tkiesel, the code was created completely by myself and is presented as freeware.
danpostdanpost

2017/2/21

@tkiesel, as far as adding the line (again), I was incorrect when I said "calling scroll from the world constructor is useful in crating the initial background image sometimes". This is actually being done by both the Scroller class constructors -- calling scroll with both parameter values of zero (see lines 46 and 77). So, not "sometimes"; but, "all the time". Adding your line would prevent these calls from doing their functions (setting the initial background image of the world).
tkieseltkiesel

2017/2/21

Awesome tip! By 'freeware' do you mean something like the Unlicense? http://unlicense.org/ I have to be a bit of a stickler about this, because I'm modelling proper digital citizenship for my kids. :)
danpostdanpost

2017/2/21

@tkiesel, by 'freeware', I mean the code is not under any license of any kind and is free to use at will. I have found one bug in the class which I will need to address (hopefully today). It is in the unlimited, no-background image scrolling part of the scroll method where the scrolling offsets fail to be updated.
A new version of this scenario was uploaded on 2017-02-21 19:49:23 UTC Added shake codes. Updated Scroller class codes.
Nosson1459Nosson1459

2017/3/29

How can I make the background image scroll slower than the main object's speed (regarding to your sample platform scenario which has gone "missing").
danpostdanpost

2017/3/29

@Nosson, start a discussion thread on this. Be more detailed in your description. Also, if you could describe the platform scenario of which you mentioned (I do not believe that anything is "missing").
danpostdanpost

2017/3/29

@Nosson1459, the 'selectable actor follow scrolling code' example shows how to govern the scrolling speed while following an actor.
danpostdanpost

2017/3/29

@tkiesel, you suggested this: // find a similar positive value for scroll positions if (imageX < 0) { imageX %= wide; imageX += wide; } if (imageY < 0) { imageY %= high; imageY += high; } // get new starting positions for drawing 'scrollImage' imageX = imageX%wide; imageY = imageY%high; which does indeed work (I, for some reason, was under the impression that all mod returns were positive -- apparently, on negative values, when there is a remainder, the value returned is negative; hence, the confusion). Even better, then, would be this: // find a similar near-zero value for scroll positions imageX = imageX%wide; imageY = imageY%high; // adjust for negative values if (imageX < 0) imageX += wide if (imageY < 0) imageY += high;
A new version of this scenario was uploaded on 2017-03-30 03:34:52 UTC
danpostdanpost

2017/3/30

@Nosson1459, I had to modify the codes you were interested in. Please note the changes in the 'selectable actor follow scrolling code' scroll methods.
Nosson1459Nosson1459

2017/3/31

I have the scrolling following an actor, I just want to know how to make the scrolling image scroll a different speed than all the scrolling objects.
danpostdanpost

2017/3/31

@Nosson1459, I guess you could create a second Scroller object for the same world and not include a background image so that you can scroll the actors only with it and have the first one scroll both.
Nosson1459Nosson1459

2017/3/31

How will that help the actor that we are following is moving the same speed. I just want to change the code so that everything else moves the opposite amount of the main actor which stays in the middle of the frame, the image will only move when the actor is moving but not the same amount of pixels each act cycle as everything else.
danpostdanpost

2017/3/31

@Nosson1459, I asked before, and I will ask once more, to please start a discussion thread on this issue (where codes can be show easier and the scenario comments are left for general comments on the specific scenario).
Nosson1459Nosson1459

2017/3/31

@danpost you asked before and then answered before also so I figured if you're willing to answer then why not ask (again).
A new version of this scenario was uploaded on 2017-10-16 00:25:35 UTC
A new version of this scenario was uploaded on 2017-10-16 01:08:03 UTC
A new version of this scenario was uploaded on 2017-10-16 01:13:07 UTC
A new version of this scenario was uploaded on 2017-11-13 01:07:25 UTC
VenbhaVenbha

2017/12/8

Amazingly Superb
VenbhaVenbha

2017/12/8

You were telling how to do it as well as doing it right then!
Paul12345Paul12345

2018/1/28

I can't seem to figure out how to set the background of the whole world to an image and not just the first viewpoint and then stretch it (on the limited scrolling). Like how it was done in your old Scrolling SuperWorld.
danpostdanpost

2018/1/28

@Paul12345 Please start a discussion thread on the issue.