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.
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.
@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.
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;
------------------------------
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!
@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.
@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).
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. :)
@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.
@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").
@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
@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.
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.
@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).
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.
Is there a way to make some actors move quicker than the player to make a perspective effect? Like change the scrolling speed of some trees to make them move slower to make them feel closer?
@Kizetsu, start a discussion thread on your issue. Provide your attempted codes (complete classes) and an error trace from that particular code. Help can then be provided.
2017/2/17
2017/2/17
2017/2/17
2017/2/20
2017/2/20
2017/2/20
2017/2/20
2017/2/21
2017/2/21
2017/2/21
2017/3/29
2017/3/29
2017/3/29
2017/3/29
2017/3/30
2017/3/31
2017/3/31
2017/3/31
2017/3/31
2017/3/31
2017/12/8
2017/12/8
2018/1/28
2018/1/28
2020/10/29
2023/5/14
2023/5/14
2023/12/6
2023/12/6
2023/12/7