Log In  

Heya, I'm trying to add a scrolling level to my game but finding difficulty implementing it. Got some code (see below) from a kind fellow user but when I put it within the question referring to the fourth level function ( 'if _FOUR() then' in the window 0 code), nothing happened. Is there anything I'm missing?

code:
cam.x = cam.x + time()
camera(cam.x,cam.y)

Cart #hpofidafi-0 | 2020-05-04 | Code ▽ | Embed ▽ | No License

P#77317 2020-05-28 13:44 ( Edited 2020-05-28 13:44)

1

So ... I can't get to the fourth level.

But at the moment, your code doesn't (as far as I can see) contain the line cam.x = cam.x + time()

You can test that line, or variations, in the first level (to save having to get to the fourth level) by putting them in directly after (or instead of) cam.x=x-40

If you had a variable to track level you could use an if-then-else to switch between different camera positioning. For example, one possibility would be:

if (level==4) then
 cam.x=time()*8 --or as you said: cam.x = cam.x + time()
else
 cam.x=x-40
end

Presumably if _four() is also performing a test for the level; I prefer the simple if(level==4) because I can change the 4 in it to a 1 easily, for testing different things.

I'm not sure I would use time() quite so directly though, but if it works, sure. The main problem I forsee is that because time() returns the number of elapsed seconds since the cartridge started, by the time you reach the fourth level quite a few seconds will already have elapsed. This means you will be adding some unknown number onto your cam.x even at the first frame of the fourth level, which probably isn't a good thing to do. Instead, if you're going to use time() for that, you need to do something like get the number of seconds already elapsed at the start of the fourth level, then in each frame deduct that from time() to give you your cam.x position.

Something like:

if (atlevelstart) timeatstartoflevel=time() atlevelstart=false

cam.x=time()-timeatstartoflevel

Note please, that's untested code, and you would need to determine for yourself how to set the boolean atlevelstart to true.

And you could combine the two:

if (level==4) then
 if (atlevelstart) timeatstartoflevel=time() atlevelstart=false

 cam.x=time()-timeatstartoflevel
else
 cam.x=x-40
end

With the same note about untested code etc as before. (Using if (level==4) like this, if you are just going to have the one level automatically scroll, you could just set atlevelstart to true in the _init() function.)

That's not the only problem you might encounter. If you encounter other problems using time(), try using a framecounter variable instead and increase it by 1 every _update(), then (for example) use cam.x=framecounter\30 (this presumes 30 frames per second, which _update() is meant to be called at, presuming normal operation) to get a 1 pixel per second adjustment.

In case a concise example in code would help; the following cartridge uses the up and down arrow keys to change which variable affects cam.x; although 1) as it is showing all three methods at once, the above comments may be clearer, and 2) it doesn't have your original cam.x modification, just the other possibilities I've mentioned, because they work slightly better:

Cart #sewidawona-0 | 2020-05-28 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

P#77324 2020-05-28 15:42 ( Edited 2020-05-30 12:25)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 21:48:28 | 0.011s | Q:20