forums

TNT Basic Forums > Programming Q&A
Moving a sprite via keyboard?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

09-09-2005 02:30

Posted by:
hellraiser25

Find more posts by hellraiser25

How do I make a sprite move when I press the down key, up w/ the up key, etc. Thanks to the people who answer my question in advance. All help is appreciated. Sorry for asking a simple question.

09-10-2005 20:25

Posted by:
hellraiser25

Find more posts by hellraiser25

alright i messed around with some of the codes and i finally got my character to move right across the screen with the right button on the keyboard, but i cant figure out how to get the character to move left,down,or up, heres my code that DOES work

Hide Mouse
Graphics Mode 640, 480
Set Pen Colour white
Fill Rect 0,0 to 640,480
Draw Frame
Load Images 128
Sprite 0,0,0,0
Draw Frame
int x=0
repeat
if pressed(124)
Sprite 0,x,0,0
x=x+1
Draw Frame
end if
forever

09-11-2005 01:05

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

You can only move your sprite right because you only have code to make it move right. You have the right idea though. Try this code:

Hide Mouse
Graphics Mode 640, 480
Set Pen Colour white
Fill Rect 0,0 to 640,480
Load Images 128
Sprite 0,0,0,0
Draw Frame

int x=0
int y=0

repeat
if right then x=x+1
if left then x=x-1
if down then y=y+1
if up then y=y-1
Sprite 0,x,y,0
Draw Frame
forever

You only have to draw the frame once each loop, and in your code you're drawing it only when you press a key. In this code, the only thing that happens when you press a key is that the sprite coordinates are moved, the sprite is drawn whether you're moving it or not.

Hope this helps,
MapMaker

09-11-2005 06:26

Posted by:
hellraiser25

Find more posts by hellraiser25

i figured it out before you replied, thanks for the help though, i could use some help on how to check for collisions, i havent started coding it yet though

09-11-2005 19:38

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

To use sprite collision, you use the "sprite col" command. You can input the id numbers of the two spites you're checking for collision, and get a true return, e.g.:

if sprite col(1,2) then print "Collision Detected!"

OR

you can use a base sprite and check for collision with multiple other sprites. Lets say sprite 1 is your guy and 20-30 are bullet sprites. This time, the function will return whichever sprite collsion was true, e.g.:

int a = sprite col(1,20 to 30)
if a <> -1
print "Your man was shot by bullet # " + asstring(a)
gameover()
end if

Hope this helps,
MapMaker

09-11-2005 20:10

Posted by:
hellraiser25

Find more posts by hellraiser25

how would you make the main character(sprite) stop when it touches another sprite o ya one more thing
if graphics mode is 1024x768, what would the coordinates be at the most lower left? thank you for your help

09-11-2005 22:26

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

>>
if graphics mode is 1024x768, what would the coordinates be at the most lower left? thank you for your help
>>

the top left corner is (0,0) so bottom left would be(0,768), top right (1024,0) and bottom right (1024,768)

09-12-2005 23:56

Posted by:
hellraiser25

Find more posts by hellraiser25

can anyone tell me how to make my character stop when it collides with another sprite so it doesnt walk right through it

the reason i asked that was because when i did (0,768) it would show up, that was because my screen res. only goes up to 702 until it runs off the screen, weird, im glad i figured it out though

all help will be appreciated, thx

09-13-2005 01:47

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

That depends on how your sprite is moved. If you're moving it with the code I gave you, you'll have to modify it.

What you want to do is create a variable that is either true or false, if it's true, then your sprite moves, and if its false, it dosnt. Try this code:

Hide Mouse
Graphics Mode 640, 480
Set Pen Colour white
Fill Rect 0,0 to 640,480
Load Images 128
Sprite 0,0,0,0
Draw Frame

int x=0
int y=0
int moving = true
int a = -1

repeat
a = sprite col(1,20 to 30)
if a <> -1 then moving = false

if moving
if right then x=x+1
if left then x=x-1
if down then y=y+1
if up then y=y-1
end if
Sprite 0,x,y,0
Draw Frame
forever

Hope this helps,
MapMaker

09-14-2005 00:13

Posted by:
hellraiser25

Find more posts by hellraiser25

ya, that helped, but i want my character to be able to move again after he stops, not stop and never be able to move again

what would the code be for that, thanks for ur help

09-14-2005 02:56

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

OK.... I think I get it, you just don't want him to be walking through walls and stuff. Well thats a bit more complicated, what you have to do is just not alow him to move a certain direction while he's colliding with the sprite....

It will be hard to custom tailor that code without any knowledge of the game, can I ask for a short description of what's happening?

Thanks,
MapMaker

09-14-2005 02:59

Posted by:
hellraiser25

Find more posts by hellraiser25

its the first area and my character starts in a graveyard, he is able to walk around, but i dont want him to walk through the graves that are placed in the graveyard

that specific enough?

09-14-2005 03:07

Posted by:
hellraiser25

Find more posts by hellraiser25

o ya do you have AIM so i can talk to you quicker instead of waiting for you to post on the forums?

if not, thats ok too

09-14-2005 20:00

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Hmm... I'll have to look at your code so far. I do have AIM but I'm not at home much lately anyways, can you send me your project? Also we can use that instead of these forums as it might require a bit of communication.

Thanks,
MapMaker

09-15-2005 23:08

Posted by:
hellraiser25

Find more posts by hellraiser25

i sent you the email, map maker

09-16-2005 08:45

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

If you were using 'point and click' you could try my breshenham example (in the directory) or the A* (Ay Star) algo for pathfinding (great tutoral here).
When moving with the keyboard you just need to find what tile your character is going to enter if they make the move indicated by the keypress (I assume you are using maps...), if the tile is 'clear' (or passable), allow the move in that direction, if not you don't perform that part of the move. By performing the X and Y checks separately you allow the character to 'slide' along walls if travelling diagonally for example.
To get the tile index, divide the character's 'world x' and 'world y' by the width and height of the map tiles.

Danny (nods)

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.