forums

TNT Basic Forums > Programming Q&A
sprite movement
< Last Thread     Next Thread >
Author
Thread        Post A Reply

10-08-2003 12:14

Posted by:
scruffkat

Location:
Ireland

Find more posts by scruffkat

I'm trying to make a simple program that requires a method for one or more character(s) to walk around a top-down view area using the arrow keys for movement. There is 8 sprites, 2 for each direction. When an arrow key is pressed an animation should start using the 2 sprites and they move in the pressed direction. When the key is not pressed the sprite should stop the animation and movement and stay where it is. The playable area is 128*128 pixels within a total map area of 160*160 pixels. The sprite should be restricted to the playable area with a possible inclusion of more restricted areas with that box. The sprites themselves have a boxed area of 16*16 pixels. My current piece of code has a lot of glitches and stuff and there has to be a much easier way to do this bit of code. Any help is appreciated.

10-08-2003 18:11

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

right, so let me get this right,
you want a central character that animates and is kept within the confines of the box and stops when you tell it to, ok this is simple stuff, so here goes.

assuming that you have a static image for the sprite when its not moving you should use that sprite all the time when your not actually pressing a key.

when you call the sprite onto the screen, use a variable for the sprites image in the sprite bank i.e.

sprite 0,x,y,spriteid

make sure you put a value on spriteid when you declare it.

now when you go to write your movement routine try to use spriteid to animate your character, for example

if left=true
spriteid=spriteid+0.1
x=x-5
end if

this will animate the sprite moving in the left direction
you will need to make sure that animating sprites are stored sequentially in the sprite bank/graphics tab.
the above routine will get the current value of spriteid and increace it 0.1 every time the screen updates/code loops, when 0.1 turns into a whole number then the image will change on screen

if, say, you wanted the animation to loop between a certian few frames, and the sprites are stored sequentially in the sprite bank/graphics tab, then you could use something like this

if your static sprite is 0 and the movement animations for left are 1 and 2

if left=true
spriteid=spriteid+0.1
x=x-5
if spriteid>=3
spriteid=1
end if
end if

this will loop the animation of moving left so that when you are holding left down only spriteid 1 and 2 are shown.

to display youe static image use something like

if left=false then spriteid=0

this displays 0 when you are not pressing a button.

the same obviously applies to the other directions.

you may need to write a line like

if spriteid>2 then image=1

to make sure that when you change direction the right sprite is displayed.

as for keeping the sprite in a box, try the following and change the x and y values

if shipy>=590 then shipy=590
if shipy<10 then shipy=10
if shipx>=775 then shipx=775
if shipx<26 then shipx=25

this does the job.

i hope this helps, if your still stuck i could email you an old game i never finished that does the same thing.

cheers
charlie


10-09-2003 14:30

Posted by:
scruffkat

Location:
Ireland

Find more posts by scruffkat

Thanks for the advice Charlie. I'm using the in built animate and move commands and my static image is the first of the two sprites used for a direction. So 0,1 are the two images used for the down movement and 0 would be the static image. The following piece of code works perfectly albeit a bit long, I think.

Excuse the bad indentation.

Hide Mouse
Graphics Mode 640,480

int map=128
int mapx1=0
int mapy1=0
int mapx2=10
int mapy2=10
int playerdirection=6
int enemydirection=2 'ignore for now
int enemydirectioncal 'ignore for now
int playercoorx=16
int playercoory=16
int enemycoorx=128 'ignore for now
int enemycoory=16 'ignore for now
int playeranimate1
int playeranimate2
int playeranimate3
int enemyanimate1=2 'ignore for now
int enemyanimate2=3 'ignore for now
int movementcalx
int movementcaly
int movementx
int movementy
int enemymovementx 'ignore for now
int enemymovementy 'ignore for now
int enemymovementcalx 'ignore for now
int enemymovementcaly 'ignore for now

load map map
draw map section mapx1,mapy1 to mapx2,mapy2,0 to 0,0
draw frame
load images 129
sprite 0,playercoorx,playercoory,playerdirection
draw frame

while not mouse button
if down
movementcalx=0
movementy=128
movementcaly=movementy-sprite y(0)
playercoorx=sprite x(0)
playercoory=sprite y(0)
playerdirection=0
playeranimate1=0
playeranimate2=1
sprite 0,playercoorx,playercoory,playerdirection
animate sprite 0,-1,playeranimate1,10 to playeranimate2,10
move sprite 0,movementcalx,movementcaly,1
while down
draw frame
wend

else if left
movementcaly=0
movementx=16
movementcalx=movementx-sprite x(0)
playercoorx=sprite x(0)
playercoory=sprite y(0)
playerdirection=2
playeranimate1=2
playeranimate2=3
sprite 0,playercoorx,playercoory,playerdirection
animate sprite 0,-1,playeranimate1,10 to playeranimate2,10
move sprite 0,movementcalx,movementcaly,1
while left
draw frame
wend

else if up
movementcalx=0
movementy=16
movementcaly=movementy-sprite y(0)
playercoorx=sprite x(0)
playercoory=sprite y(0)
playerdirection=4
playeranimate1=4
playeranimate2=5
sprite 0,playercoorx,playercoory,playerdirection
animate sprite 0,-1,playeranimate1,10 to playeranimate2,10
move sprite 0,movementcalx,movementcaly,1
while up
draw frame
wend

else if right
movementcaly=0
movementx=130
movementcalx=movementx-sprite x(0)
playercoorx=sprite x(0)
playercoory=sprite y(0)
playerdirection=6
playeranimate1=6
playeranimate2=7
sprite 0,playercoorx,playercoory,playerdirection
animate sprite 0,-1,playeranimate1,10 to playeranimate2,10
move sprite 0,movementcalx,movementcaly,1
while right
draw frame
wend
end if

wend
repeat
until mouse button






10-09-2003 14:38

Posted by:
scruffkat

Location:
Ireland

Find more posts by scruffkat

What I want to add to the code is an enemy that walks randomly around the screen but which animates in the correct direction that its facing or moving toward.
It has the same image structure as the player, 8 images, 2 for each direction.
Here what I've come up with so far. What I need help with is the placing of draw frame and the while strucutre for it so that it fits in with the original code. Also some delays within it so that the enemy waits sometimes changing direction would be a help. The ints are already in the original code. Again please excuse the indentation.


load images 130
enemydirectioncal=random(0,7)
if enemydirectioncal=1 or 3 or 5 or 7
enemydirectioncal=enemydirectioncal-1
end if
enemydirection=enemydirectioncal
enemycoorx=128
enemycoory=16
sprite 1,enemycoorx,enemycoory,enemydirection
enemycoorx=sprite x(1)
enemycoory=sprite y(1)

enemymovementx=random(-256,-128)
enemymovementy=random(-16,112)
enemymovementcalx=enemymovementx+enemycoorx
enemymovementcaly=enemymovementy+enemycoory

enemyanimate1=enemydirection
enemyanimate2=enemyanimate1+1

move sprite 1,enemymovementcalx,enemymovementcaly,1
animate sprite 1,-1,enemyanimate1,10 to enemyanimate2,10

draw frame

10-13-2003 16:57

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

i see what your trying to do, but i dont use the built in commands as i find it easier to do it my own way, but from what i know of the command, you could generate a random coordinate, i.e. xmovingtoo=random(0,640) or ymovingtoo=random(0,480), then get your sprites x and y positions and find the difference between the two points

from there you could put something like

Move Sprite spriteNum,xdifference,ydifference,speed

if xdifference=xmovingtoo then generate a new xmovingtoo
and start the whole thing again

aside from moving, i cant off hand think of a particularly good way of animating your sprite according to the direction its heading,
perhaps someone else has more experience with moan commands and could help you more comprehensively.

if you werent using moan, then i could help you out!

thanks
charlie

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.