looping through a type backwards?
Blitz3D Forums/Blitz3D Programming/looping through a type backwards?
| ||
ok,... stupid question here but I hope someone can help. This is a perfect example why artists should not try to program. By the way,... this is all 2d images, so when I refer to a sprite I mean a bitmap,... not a 3d sprite. I need to be able to loop through a type backwards, or change the way I am doing things. Right now I have several sprites moving around, and each loop through, I check if they are being dragged by the mouse, then draw them at their position. All of this is working great, with one exception. I have them ordered from front to back based on y position, and when I loop through them it is from back to front (the way I want to draw them) The dragging, however grabs the first one found, which can be behind another one. This is very unnatural and I'd like to avoid it. If I could loop through them backwards it would work fine. It would also be nice to be able to draw the sprite and check the drag in the same loop, so I don't have to loop through twice (once each way) any ideas? thanks. |
| ||
look up the BEFORE command. my.token = last token checkclick() for i = 1 to (numoftokens-1) token = before token checkclick() next |
| ||
Yea, that would work fine. I guess what I'm really asking though, is if there is a way to not have to loop through twice? Once to check for dragging, and once to draw the images in the right order, since I need to draw them from back to front, yet check them from front to back. The only thing I've thought of is making an array of types that I can sort through like (pseudocode): for loop = i to count check if dragging object(count-i) draw image object(i) next but that is not very good since the number of objects will be changing a lot. or is looping through twice simply the way to do it? |
| ||
Hard to say without seeing some code. I don't know enough about your situation. |
| ||
To loop backwards:t.mytype=last mytype While t<>null DrawImage t\image,0,0 t=before t Wend |
| ||
you could just set up a pointer and update it whenever you come across one that's under the mouse, then when done looping through, you'll then have the pointer at the last instance of the type. I'm sure that didn't make any sense, so here's some code illustrating it.For t.mytype = Each mytype If MouseOverSprite() Then drag.mytype = t.mytype DrawImage t\image,t\x,t\y Next DragSprite(drag.mytype) |
| ||
Pongo You could also INSERT the sprite BEFORE the previous one upon creation, and thus use FOR...EACH like normal. t.mytype = New mytype Insert t Before First mytype For this.mytype = Each mytype ;do something to 'this' NextThis will insert the Sprite Before the First one in the collection, making it the first one in the collection. |
| ||
Thanks guys,... I think TomToad's idea is what I needed to do. |
| ||
... edit: removed non-working code,... figured out what I was doing wrong. Thanks again for the help above. |