forums

TNT Basic Forums > Programming Q&A
Moving Sprites By The Tile
< Last Thread     Next Thread >
Author
Thread        Post A Reply

04-21-2003 19:05

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Hi all, I am making a turn, grid - based game. I have a the map tiles set out with white borders and a black interior, so that when all of them are put together on a map it looks like a grid. I have sprites that are formatted so that they fit exacly into each tile, this works Ok. But now I've gotten to a stage where I need to move the sprite from the middle of one tile, to the middle of another, by clicking. I am using the code...

selected = 0
shipselected=false
....
....
....
if mouse button and mouse x<(rsx[selected]+45) and mouse x>(rsx[selected]) and mouse y<(rsy[selected]+45) and mouse y>(rsy[selected]) and shipselected=false
set pen colour black
fill rect 550,180 to 700,200
set pen colour make color (255,0,0)
draw text 405,195,"You have selceted ship " +as string(selected)+ ", a coloniser"
shipselected=true
end if
if mouse button and shipselected=true
canvas 1
sprite off selected
int mousemtilex=(((mouse x/map tile width + (x/map tile width))*map tile width) + 3)
int mousemtiley=(((mouse y/map tile height + (y/map tile height))*map tile height) + 3)
rsx[selected]=mousemtilex
rsy[selected]=mousemtiley
sprite selected,rsx[selected],rsy[selected],14
delay 500
draw frame
shipselected=false
end if
....
....
....
...... to do this. But when I select a sprite (coloniser), It tells me that I have selected it, and what number it is, but when I click on another tile again, the sprite dosn't move. Does anyone know why?

04-21-2003 19:25

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I will give you a tip: print is the BEST debugging command of TNT. Put print everywhere you think it's useful ( but only one at a time ).

Ex.: write « print "Movement called" » in the block where the sprite is supposed to move. If nothing happens when you click, it'll mean the block is never called. If the message pop-ups, it means the block is called but it does not do what you wanted it to do.

04-23-2003 15:53

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Moving Sprites

Ok, thanks Mathew, thats a good suggestion.
But of course, I ran into another problem. I nned to have thie script know the ID of the sprite that I clicked on. There can be over 50 of the same sprites (with ID's 1 - 51), and I need to move only the one that I clicked on. Do you know how I could do this?

04-23-2003 21:57

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

int id=0

id=sprite col ( 5,1 to 50 )

' id will contain the index of the sprite between 1 and 50 that hits sprite 5
' or -1 if no sprite between 1 and 50 hit sprite 5
' hope this helps!
' I will try to pôst soon examples for beginner, because I see
' people have lots of problems learning to program!

04-24-2003 01:28

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Moving

Does sprite col cover clicking?
If it does, which is the part of the code for the mouse?

04-24-2003 02:27

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

'mouse x' and 'mouse y' return the x and y coordinates of the mouse, if thats what you mean.

04-24-2003 02:28

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

what the hell am i talking about?

04-24-2003 12:36

Posted by:
Pat

Click Here to Email Pat   Find more posts by Pat

Make a sprite at the co-ordinates of mouse x/mouse y, then use sprite col.

04-24-2003 19:03

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

..........

Im now using the code:

.....
global int rsx[200]
global int rsy[200]
......
rcshipnum=(rcshipnum + 1)
rsx[rcshipnum]=redplanx+48
rsy[rcshipnum]=redplany+48
set sprite priority rcshipnum,3
sprite rcshipnum,rsx[rcshipnum],rsy[rcshipnum],14
rc=rc-5
canvas 0
set pen colour black
fill rect 405,270 to 600,350
end if
'Move Coloniser

selected = Sprite Col (0,rcshipnum to rcshipnum+49)

if mouse button and mouse x<(rsx[selected]+45) and mouse x>(rsx[selected]) and mouse y<(rsy[selected]+45) and mouse y>(rsy[selected]) and rcselected=false
................

to move one of the sprites. But when I use this code, and I click on a picture on the side, it comes up and says "array index out of bounds on line "if mouse button and mouse x<(rsx[selected]+45) and mouse x>(rsx[selected]) and ........".". Does the picture ID interfere with the sprite col command?

04-24-2003 20:37

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Sprite Col

If there is no sprite collision then sprite col will return -1. You don't appear to be checking for that before using the result to index the array. An index of -1 would cause an array index out of bounds error.

Hope this helps!

Mark

04-25-2003 16:15

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Thanks

Well, thanks eveyone for all your help. I guess I just need more experiance programming before I get too far into this.

04-29-2003 01:24

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Sprite Col

Strange.
When I use sprite collision with my mouse, the beginning screen works exactly as It should (it prints out all the sprite's id's that the mouse collided with). But when I scroll the map over a bit, this ceases to work.
And even more strange is the fact that I figured out that if, in the beginning screen, there is a stationary sprite at, say, location 3,4 , then when I scroll the map over, and then put the mouse where the stationary sprite (at 3,4) used to be........ it tells me that the mouse has collided with the same sprite (which is no longer there).
I have checked my canvases, they seem in order, and there is probably some really obvious explanation for this, but I can't see it. Can any of you?

04-29-2003 22:22

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

It's difficult to answer as I cannot see your code, but I can try:

If you collide with a sprite that is no longer there, it may be because it was covered by another sprite or because it's transparent.

If the cursor does not collide anymore, I need to know what scrolling viewport you use, canvas or map viewport.It may change everything.

05-03-2003 20:04

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Code

Ok, you can find the whole code at:

http://sh2002.tripod.com/wholecode.txt

and the part of it that im having trouble with at:

http://sh2002.tripod.com/probcode.txt

Hope you can follow it.

05-04-2003 14:05

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Did you know about arrays? Instead of declaring

int bluePanet1,bluePanet2,bluePanet3,bluePanet4,bluePanet5

you could declare

int bluePanet[5]


I will try to find the error soon. COuld you specify how many images you had?

05-04-2003 14:13

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I think I found the mistake:

if selected <> -1 and mouse button and mouse x<(rsx[selected]+45) and mouse x>(rsx[selected]) and mouse y<(rsy[selected]+45) and mouse y>(rsy[selected])

Why do you verify the coords? Sprite col does it for you!!! I suggest you use ONLY sprite col.

05-05-2003 18:25

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Sprites

Well, actually this is the first thing I've started, so, no, I didnt know about arrays.

Thanks Mathew, I'll try out your suggestion.

Btw, do you know if there is any way to tint a sprite a certain color, not just make it a solid color?

Thanks

05-05-2003 18:39

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Well...

Well, it still dosnt work, but thanks for trying, I really apriciate it.

Ya, and there are only 23 unique imgaes, in the game at the start there are about 150.

05-05-2003 23:09

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Looking at non-tabbed non-colored context-less code in the boards won't help much. Could you send me the whole thing ? ( If it's not larger than 1 MEG please delete sounds and music ) and please include « a.s.o.n.d » somewhere in the subject line ( I programmed my junk mail filter to accept all mail containing that lne :) )

05-06-2003 22:00

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

OK could you explain me the rules of your game? I'm not sure how to play it.

05-07-2003 01:36

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Game

Well, actually I just started it, so there are really no rules yet.

All you have to do is click the "build" button in the menu to the right, then hit "coloniser" and a red circlish thing will appear one square to the right and one sqare down from the red circle on the map (that has a lighter colored perimiter).

Then click the 'move' button in the menu, and select the first cirlish thing that appeared on the map. (It then shoould say that "you have selected ship 1, a coloniser", in the menu)

I have it programmed so that when you click on another square on the map, the sprite moves to those co - ordinates.

But of course, it dosn't.

Thanks for your help.

05-07-2003 19:48

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

A problem Found

I think I found the problem -- the sprites are on Canvas 0, thats why it says they are still in the same 'spot' even when I have moved the map.

05-07-2003 19:58

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

And now.....

And now that I think about it, is there any way to 'paste' sprites onto a canvas(map) thats not canvas 0 so that they can be scrolled off of the screen and then back on again?
I've been looking over the basic sprite syntax and I can't find anything that tells it what canvas to be put onto. Is this the problem?

05-07-2003 22:48

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

canvas 1
' everything that follows will be put o canvas 1
' but you seem to be using a map viewport
' this means the sprites won't scroll
' maybe look for AsondSoft's map tutorial in the learning section
' of TNT Basic onlne

05-08-2003 02:10

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

If you are using a "map viewport", then anything drawn on the viewport's canvas might dissappear when it scrolls out of view, due to the way map viewports work.

05-09-2003 18:59

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Hmm

I looked at the tutorial Mathew, and the Idea of having the sprite's co-ordinates subtracted from the map viewport variables made it work like I want it to.
For the coloniser.
Not for the planets.
They should be the same, they both work on the same system.
I'll sen you an updated version if you want, I've re - formatted it so its much easier to follow.
Thanks for your help!

05-09-2003 21:53

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

OK, but put some "instructions" or a list of bugs at the top of the code.

05-09-2003 21:53

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I mean, tell me what I'll have to check for.

05-14-2003 15:36

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Sprite does not always move to correct location

I am now using the code

(('if you have clicked somewhere and you have previously selected the ship to move))

if mouse button and rinselected=true

(('The mouse's location by the tile, each tile is 45 x 45 pixels. Variables 'x' and 'y' are the veiwport offset.))

int mousemx=(mouse x + x)
int mousemy=(mouse y + y)
int movenum = 0
int xleg = ((mousemx - (rsx[sselected]))/45)
int yleg = ((mousemy - (rsy[sselected]))/45)
float mpfx = (xleg/100)
float mpfy = (yleg/100)
vbl sync on
while movenum < 100
movenum = movenum + 1
rsx[sselected] = (rsx[sselected] + (mpfx*45))
rsy[sselected] = (rsy[sselected] + (mpfy*45))
sprite sselected,rsx[sselected],rsy[sselected],15
draw frame
wend
vbl sync on

......which moves the sprite (sselected) from where it is (rsx[sselected], rsy[sselected]) to wherever you have clicked, in 100 frames.
And this works.
Most of the time.
Sometimes it dosnt. Would any of you happen to know why?

p.s. The sprite, as you should be able to tell, when it is not moving, is always inside a tile, not overlapping.

05-14-2003 20:38

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I do not see any mistake in that code. The problem may come from another part of the code. I will again ask you to send your project to me.

05-27-2003 01:58

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

.....................................

Hello.
Me again.
More problems.
Not only do the ships move where their not suppost to, but now only sometimes the planets get colonised by colonisers, and only sometimes do the sprites appear in the right place........
I do not think it is a problem with my code. I have had many people look it over and find no problems with it.
Could it be a bug in the compiler?
Until I get this figured out, my project has come to a grinding halt...
If you want to look at it, just give me your e-mail adress and i'll send it ASAP.
Thanks in advance......

05-27-2003 20:25

Posted by:
ERaZer

Click Here to Email ERaZer   Find more posts by ERaZer

I could take a look at it, my mail is theerazer*nospam*@telia.com

Remove the *nospam*.

05-28-2003 03:28

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

I can take a look at it too. My e-mail is edoi@earthlink.net

06-19-2003 02:09

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Sprite Collision

Hmmmm...
Ive been looking over a certain area of the code for the past few days, and I've isolated a 'malfunction' that has to do with sprite collision. The code is simple:

int num = 1
int pc
while num < 50
pc = sprite col(num,1000 to 1005)
print pc
if pc = -1
pc = sprite col(num,2000 to 2200)
end if
if pc <> -1
print "------------------------TAKEN-------------------"
set sprite color pc,make color(255,0,0)
end if
num = num + 1
wend

(This test to see if sprites 1 - 50 have collided with sprites 1000 - 1005 and 2000 to 2200)

When sprite 1 is colliding with sprite 1001 (which it quite evidently is -- they're both stationary and 1 is on top of the other), SOMETIMES it does NOT print "------------------------TAKEN-------------------".

The only thing I can think of is that there is some problem with the sprite col function. Has anyone ever run into ever trouble with anything like this?

06-19-2003 03:23

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

maybe one of your procedures is changing the canvas or something else that might effect the collision detection?

06-19-2003 14:53

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Ahhh.....

Thats an interresting sugestion
Can two sprites from different canvases collide?

06-23-2003 14:37

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

I guess they cant.
But anyways, I got the colonizing thing figured out, so now I'm just left with movement.
Mathew, when I talked to you a while ago you said you had a good moving script, may I have a look at it?

If anyone else has a script that moves a sprite from A to B by clicking on the sprite, and then somewhere else (B), and it moves there, I would be interrested in it as well.

Thanks

06-24-2003 13:23

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

You may calculate the angle between the two sprites . I think it's soemthing like this, but I'm not sure the second line is OK.

int angle=invtan((Y1-Y2)/(X1-X2))
if Y1>Y2 then angle=angle +180

After, you may make the sprite go in the detected angle at every frame:

x=x+(cos(angle)*r)
y=y+(sin(angle)*r)

So you may get something like that ( I'm not sure, please correct any syntaxt error) :


int angle=0,x=100,y=100,status=0,mx,my

while execution

if status=0
wait mouse click
status=1
mx=mouse x
my=mouse y
end if
if status=1
angle=invtan((y-my)/(x-mx))
if y>my then angle=angle +180
status=2
end if
if status=2
x=x+(cos(angle)*2)
y=y+(sin(angle)*2)
if (my/25=y/50) && (mx/25=x/50) then status=1
sprite 1,x,y,0
draw Frame
end if

wend

06-24-2003 20:09

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Hmm

Well, it didn't work, but it might need some adapting. I'll tell you if I get it working. Thanks for helping.

06-25-2003 23:04

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I'm pretty sur the invTan part does not detect the real angle...

06-26-2003 18:19

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Ya, it didnt work unless the x/y coordinates were bigger numbers that the sprite's. :(
Thanks anyways

06-27-2003 14:55

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I think the good formula can be found in "angles" in the downloads section.

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.