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.

DarkBASIC Discussion / Changing sprites

Author
Message
Red general
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: United Kingdom
Posted: 6th Dec 2002 17:56
Does anyone know how to change sprites?
I made a walking sprite which would go forwards, backwards, up and down. This sprite is animated to make it look like it is running. It runs on a background.
Here's the code:




Now what I want to do is have it so that it starts off with 1 sprite (say scrating his head). On left key it should change to another. On right key it should change to another etc. however it should go back to the orrigenal when no keys are pressed.

How do I do these things? Please help
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 6th Dec 2002 18:06
Try this:

Rem ++++++++++++++++++++++++++++++++++++++++++++
Rem +++++++ +++++++
Rem +++++++ Sprite Mania +++++++
Rem +++++++ +++++++
Rem ++++++++++++++++++++++++++++++++++++++++++++

rem Load character into hidden bitmap
Load Bitmap "field02.bmp"
LOAD BITMAP "runner.bmp",1

rem Grab images for character animation
FOR y=0 to 1
FOR x=0 TO 6
GET IMAGE 1+x+(y*7),(x*89),(y*120),(x*89)+89,(y*120)+120
NEXT x
NEXT y

rem Delete character bitmap
DELETE BITMAP 1


Rem Set Variables
x=230
y=200

rem Activate manual syncronization
SYNC ON

rem Begin Loop
DO

rem Control position with cursors
movehor=0
if leftkey()=1 then x=x-9 : movehor=-1
if rightkey()=1 then x=x+9 : movehor=1
if upkey()=1 then y=y-9
if downkey()=1 then y=y+9


Rem Animate runner and wrap
if movehor<>0 then image=image+1 : IF image>12 THEN image=2
if movehor=0 then image=1

rem Update sprite
sprite 1,x,y,image

rem Refresh screen now
SYNC : SLEEP 20

rem End Loop
LOOP

Now I added a variable called 'movehor' - which is 0 when standing still, or -1/1 when moving, it's set to 0 by default, and changes when you press left or right. This is usefull for conditional control too, like you might have a different jump for when he's standing still, that sorta thing - it's always a good idea to keep track of what your character is doing.

HTH


Van-B
Red general
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: United Kingdom
Posted: 6th Dec 2002 18:42
Eh? It doesn't change the bitmap, and now the bitmap can only shuffle its feet - it can't complete the animation. This needs a little work, and I still need help

Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 7th Dec 2002 06:25
if you want to do this just add a variable like Van has...
the variable set in motion the state - if scancode()=0 then the state=0
if state=0
gosub _anim_idle
else
if upkey()=1 then blah blah

pretty simple really (^_^)

Anata aru kowagaru no watashi!
Red general
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: United Kingdom
Posted: 8th Dec 2002 17:50
Still stuck, they can now change, but they now don't move.
Here's the code

Rem +++++++++++++++++++++++++++++++++++++++++++++++++
Rem +++++++ +++++
Rem +++++++ Sprite Mania By David Harper +++++
Rem +++++++ +++++
Rem +++++++++++++++++++++++++++++++++++++++++++++++++

rem Load background
Load Bitmap "field02.bmp"


rem Activate manual syncronization
SYNC ON


rem Begin Loop
DO

Rem variable for sprite
movehov=0

rem Control position with cursors
if leftkey()=1 then x=x-9 : movehov=-1
if rightkey()=1 then x=x+9 : movehov=1
if upkey()=1 then y=y-9
if downkey()=1 then y=y+9

rem load sprites
if movehov=-1 then Load Bitmap "runner3.bmp",1

If movehov=1 then LOAD BITMAP "runner.bmp",1

If movehov=0 then Load bitmap "runner2.bmp",1


rem Grab images for character animation
FOR y=0 to 1
FOR x=0 TO 6
GET IMAGE 1+x+(y*7),(x*89),(y*120),(x*89)+89,(y*120)+120
NEXT x
NEXT y

rem Delete character bitmap
DELETE BITMAP 1

Rem Set Variables
x=230
y=200


Rem Animate runner
image=image+1 : IF image>12 THEN image=2


Rem wrap round screen
IF x>640 THEN x=-40
IF y>480 THEN y=-80

rem Update sprite
sprite 1,x,y,image

rem Refresh screen now
SYNC : SLEEP 20

rem End Loop
LOOP


If you can help me show the whole code so I can understand. please.

cyrano
21
Years of Service
User Offline
Joined: 7th Dec 2002
Location:
Posted: 8th Dec 2002 18:14
I think I see part of your problem...

You are changing x and y variables everytime the player presses the arrow keys which is correct, but then you reset them back:

Rem Set Variables
x=230
y=200

This does no good to change the moving variables and then reset them again before the sprite has had a chance to display.

Move this code, put it right before the DO loop. See if that helps.

Rem Set Variables
x=230
y=200

cyrano
21
Years of Service
User Offline
Joined: 7th Dec 2002
Location:
Posted: 8th Dec 2002 18:30
I just noticed something else..

FOR y=0 to 1
FOR x=0 TO 6
GET IMAGE 1+x+(y*7),(x*89),(y*120),(x*89)+89,(y*120)+120
NEXT x
NEXT y

You are also using x and y in this for-next loop...
You are using x and y to get the images and then to display the sprites position.. you can't do both at the same time.

Try using different variables... Try this code..

Rem +++++++++++++++++++++++++++++++++++++++++++++++++
Rem +++++++ +++++
Rem +++++++ Sprite Mania By David Harper +++++
Rem +++++++ +++++
Rem +++++++++++++++++++++++++++++++++++++++++++++++++

rem Load background
Load Bitmap "field02.bmp"


rem Activate manual syncronization
SYNC ON


rem Begin Loop

Rem Set Variables
x=230
y=200

DO

Rem variable for sprite
movehov=0

rem Control position with cursors
if leftkey()=1 then x=x-9 : movehov=-1
if rightkey()=1 then x=x+9 : movehov=1
if upkey()=1 then y=y-9
if downkey()=1 then y=y+9

rem load sprites
if movehov=-1 then Load Bitmap "runner3.bmp",1

If movehov=1 then LOAD BITMAP "runner.bmp",1

If movehov=0 then Load bitmap "runner2.bmp",1


rem Grab images for character animation
FOR yy=0 to 1
FOR xx=0 TO 6
GET IMAGE 1+xx+(yy*7),(xx*89),(yy*120),(xx*89)+89,(yy*120)+120
NEXT xx
NEXT yy

rem Delete character bitmap
DELETE BITMAP 1


Rem Animate runner
image=image+1 : IF image>12 THEN image=2


Rem wrap round screen
IF x>640 THEN x=-40
IF y>480 THEN y=-80

rem Update sprite
sprite 1,x,y,image

rem Refresh screen now
SYNC : SLEEP 20

rem End Loop
LOOP

Red general
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: United Kingdom
Posted: 13th Dec 2002 23:58
This works much better, but I know when stil there is no animation, I have tried if scannercode()=0 then movehov=0 but I get errors on working.

Login to post a reply

Server time is: 2024-04-25 04:34:08
Your offset time is: 2024-04-25 04:34:08