Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Move sprite with scrolling background?

Author
Message
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 12th Apr 2021 01:15 Edited at: 12th Apr 2021 01:20
In my experimenting I have managed to get a background to scroll horizontally using the code below as found here on the forum. Thanks to the author of this



However I am wanting to place objects (sprites) on the background that will scroll with it e.g. buildings etc along the edge of a scrolling road

I have tried several variations of the following (LevelOne_testcar being the sprite I wish to synchronise with the scrolling game.bg background)


thanks
Bob
We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 12th Apr 2021 02:42 Edited at: 12th Apr 2021 02:45
Firstly, setspriteoffset() does not position the sprite, it sets the sprites offset position.
The SetSpriteUVOffset() value is in a range 0 to 1 where 0 is the left size of the sprite and 1 is the righ side of the sprite.
So if you are moving the background UV texture by 0.04 then the sprite pixel movement should be .04 * GetSpriteWidth(game.bg).
Something like

Make sure you are using float values when doing these calculations
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 12th Apr 2021 02:45 Edited at: 12th Apr 2021 02:45
This is one method I have tried and it does work... sort of... the static car sprite does scroll with the background, but the movement looks erratic

We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 12th Apr 2021 02:48 Edited at: 12th Apr 2021 02:51
bg1x should be a float. All values relating to a sprite's position, velocity, gravity etc should be floats
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 12th Apr 2021 03:41 Edited at: 12th Apr 2021 03:58
Thanks blink0k. I tried your code as it is and the car sprite shot up off the screen so I changed the SetSpritePositionByOffset Y to the actual position of the car and also the carstep# to -0.04 and it worked. I added code to check on the left position of the car and reset it to the right to have the scroll continuous. Thanks again I believe the initial scroll code was yours too.



edit:
Since removed these two lines as they were redundant

setspritex(LevelOne_testCar, bg1x)
bg1x=bg1x-20
We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 12th Apr 2021 03:43
Quote: "bg1x should be a float. All values relating to a sprite's position, velocity, gravity etc should be floats"


Yep it is. I have made that mistake several times
We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 12th Apr 2021 04:38 Edited at: 12th Apr 2021 04:41
1. You are using GetSpriteY() to get the Y position of the sprite (Rather than GetSpriteYByOffset())


My advice would be to have a look at SetViewOffset(). Then you can layout your background with all the decals and then use SetViewOffset() to move the view around
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 12th Apr 2021 11:08 Edited at: 12th Apr 2021 11:10
Quote: "You are using GetSpriteY() to get the Y position of the sprite (Rather than GetSpriteYByOffset())"

@ blink0k No. In the end I had to use a typed in Y number. in this case 570. I don't know why but both GetSpriteY() and GetSpriteYByOffset() sent the sprite upwards yet the number didn't.

Quote: "My advice would be to have a look at SetViewOffset()"

I'll take a look at that. My next mission is to have the cars (4 of them) jostling for position in the center of the screen. At the moment they move to the right then reappear at the left after they reach the right hand side of the screen. I'm using a random movement to allow the cars to jostle, but that doesn't work so well when the cars are given an anchor point to jostle from. I'm looking at the 2D Physics to see if that will do what I want.
We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 13th Apr 2021 10:43 Edited at: 14th Apr 2021 02:23
Using sprite physics I've been able to simulate (sort of) the cars jostling for position. At the moment the they are basically equal re physics attributes. Movement for each car is governed by random and also timers. TODO: At the moment the play is not staying central to the screen and the cars are edging toward s the right hand side of the screen. Also play back of the video is x2.


(is the video showing? If not can someone point me in the direction of how to do this, Thanks)

Mod note: When you use the youtube tags you only need to specify the video name, not the whole URL. Edit this post and you will see the correct syntax
We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
Colonel_Klink
17
Years of Service
User Offline
Joined: 20th Dec 2006
Location: Within the dark recesses of my mind
Posted: 17th Apr 2021 05:50 Edited at: 17th Apr 2021 06:09
Updated video simulates around 30 laps. Still got a lot of work to do

We are now fully operational - that is, we are in a constant flap instead of having isolated confused situations.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 18th Apr 2021 08:30
Looking really nice. Very well done

Login to post a reply

Server time is: 2024-03-28 22:22:23
Your offset time is: 2024-03-28 22:22:23