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.

2D All the way! / 2D Multiple Sprite Control

Author
Message
Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 21st Dec 2002 04:04
Ok, for my first foray into a 'moving' 2D game (instead of a single player chacter moving from space to space in a static world), I need to know how to change the x and y positions of more than one sprite. Can anyone help?
-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 21st Dec 2002 04:50
for each sprite u want to manage u will need a variable for the x and y positions and any other type of feature u would want to track like hit points etc.


imagine a few arrays to this effect.

firstly we would need to make an array using locations 1 - 10 and leaving location zero spare for the moment.

the E denotes that these 10 sprites will be the Enemies


the array names are long winded so u can clearly see the intention

dim E_sprite_x_loc(10)
dim E_sprite_y_loc(10)


to fill the location 10 only
E_sprite_x_loc(10)=200
E_sprite_y_loc(10)=200

this in effect says to place the value of 200 into both the x and y arrays that store the sprites co ordinates and since we have entered a 10 into the array at this point we are calling on sprite 10

if u wanted to fill all the array locations without writing each line by line u could deploy a for loop

for i = 1 to 10
E_sprite_x_loc(i)=i*20
E_sprite_y_loc(i)=50
next i


this loop makes them all appear in a row but by changing
the code after the equals in both the x and y will change there locations.



to use this on one sprite
u could now use



and it would place the sprite in that location provided u had set the sprite up previously



and to do the same as we did above to save coding lines we make another loop to position them


for i = 1 to 10
sprite i,E_sprite_x_loc(i)=,E_sprite_y_loc(i)=,i
next i


all this assumes that the sprites u are using have the same image number reference and that they are previously set up.

this is a small insight to some of the issue u may face I guess.

indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 21st Dec 2002 04:52
typo

to use this on one sprite
u could now use


forgot this line

sprite 10,E_sprite_x_loc(10)=,E_sprite_y_loc(10)=,10

Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 22nd Dec 2002 01:10
Well, this may sound stupid, but I don;t get how an array works, or how it's better than a series of variables, so is there any way to do that without using an array?

-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 22nd Dec 2002 01:26
yup

sprite1x = 100
sprite1y = 100

sprite2x = 200
sprite2y = 100


etc...

Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 22nd Dec 2002 01:41
So, for the complete code...

load bitmap "Sprite1.bmp",1
get image 1,0,0,50,50
sprite 1,0,0,1

load bitmap "Sprite2.bmp",2
get image 2,0,0,50,50
sprite 2,0,0,2

sprite1x = 100
sprite1y = 100

sprite2x = 200
sprite2y = 200

...should load Sprites 1 and 2, then place Sprite 1 at (100,100) and Sprite 2 at (200,200)? 'Cause that doesn;t work...

-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 22nd Dec 2002 02:13
declare the variables first

then use them in the sprite command in the x and y positions.

something to this effect.


sprite1x = 100
sprite1y = 100

sprite2x = 200
sprite2y = 200

load bitmap "Sprite1.bmp",1
get image 1,0,0,50,50
sprite 1,sprite1x,sprite1y,1

load bitmap "Sprite2.bmp",2
get image 2,0,0,50,50
sprite 2,sprite2x,sprite2y,2

do
sync
loop

Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 22nd Dec 2002 02:28
And to control one of the sprites, how would I modify this?...

sprite1x = 100
sprite1y = 100

sprite2x = 200
sprite2y = 200

load bitmap "Sprite1.bmp",1
get image 1,0,0,50,50
sprite 1,sprite1x,sprite1y,1

load bitmap "Sprite2.bmp",2
get image 2,0,0,50,50
sprite 2,sprite2x,sprite2y,2

if upkey()=1 then sprite1y = sprite1y - 50

if downkey()=1 then sprite1y = sprite1y + 50

if leftkey()=1 then sprite2x = sprite1x - 50

if rightkey()=1 then sprite2x = sprite1x + 50

do
sync
loop

...'cause it doesn't work (in other games like the aforementioned sprites in static worlds one, the control scheme does).

Oh, and sorry to keep bugging you like this.

-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 22nd Dec 2002 02:32
if leftkey()=1 then sprite2x* = sprite1x - 50

if rightkey()=1 then sprite2x* = sprite1x + 50

*And I realize now that the 'sprite2x's should be 'sprite1x's, but that doesn't work either...

-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 22nd Dec 2002 02:59
YES!!!
I finally figured it out!
With the following code, the player can move both sprites, Sprite 1 in all directions, and Sprite 2 up and down. There's also sprite collision, of course. Thanks for your help!

sprite1x = 100
sprite1y = 100

sprite2x = 200
sprite2y = 200

oldsp1x = 100
oldsp1y = 100
oldsp2x = 200
oldsp2y = 200

load bitmap "DLKnight.bmp",1
get image 1,0,0,50,50
sprite 1,sprite1x,sprite1y,1

load bitmap "DLHargon.bmp",2
get image 2,0,0,50,50
sprite 2,sprite2x,sprite2y,2

a:

oldsp1x = sprite1x
oldsp1y = sprite1y
oldsp2x = sprite2x
oldsp2y = sprite2y

if upkey()=1 then sprite1y = sprite1y - 50

if downkey()=1 then sprite1y = sprite1y + 50

if leftkey()=1 then sprite1x = sprite1x - 50

if rightkey()=1 then sprite1x = sprite1x + 50

if returnkey()=1 then sprite2y = sprite2y - 50

if spacekey()=1 then sprite2y = sprite2y + 50

rem draw the sprite at the new position
sprite 1,sprite1x,sprite1y,1
sprite 2,sprite2x,sprite2y,2

rem Check for initial impact
if sprite hit(1,0)>0 then ink 0,rgb(255,255,255) else ink rgb(255,255,255),0

rem Check for collision and block new position
if sprite collision(2,0)>0
sprite1x=oldsp1x
sprite1y=oldsp1y
sprite2x=oldsp2x
sprite2y=oldsp2y
sprite 1,sprite1x,sprite1y,1
sprite 2,sprite2x,sprite2y,2
endif

sync
goto a

-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
Ole Sparky
21
Years of Service
User Offline
Joined: 6th Dec 2002
Location: United States
Posted: 22nd Dec 2002 03:00
Note: 'DLKnight.bmp' and 'DLHargon.bmp' are Sprites 1 and 2, respectively. I just forgot to change them in the posted code snippet.

-Ya know how much I hate 'Stack Overload Error's?-
Thhhhhhhhhhhiiiiiiiisssssssssssss muuuuuuuuccchhh!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 22nd Dec 2002 05:36
hehe your getting the hang of it good luck

Login to post a reply

Server time is: 2024-04-20 09:34:18
Your offset time is: 2024-04-20 09:34:18